skip to Main Content

I use WooCommerce Tiered Pricing Table plugin and I want to be able to see which one of them purchased by this plugin in admin panel (details of order information).
I have modify this code but not working and only show No.
Please check it and help me.

Any help is appreciated.

/*Track purchases made through WooCommerce Tiered Pricing Table plugin.*/
function track_purchases_from_tiered_pricing_table($order_id) {
    // Get the order object
    $order = wc_get_order($order_id);
    
    // Loop through the order items
    foreach ($order->get_items() as $item_id => $item) {
        // Get the product object
        $product = $item->get_product();
        
        // Check if the Tiered Pricing Table plugin file is being loaded
        if (class_exists('Tier_Pricing_Table')) {
            // Update the order meta to indicate the purchase from the plugin
            update_post_meta($order_id, '_purchased_from_tiered_pricing_table', 'yes');
            break; // Exit the loop if a product from the plugin is found
        }
    }
}
add_action('woocommerce_checkout_order_processed', 'track_purchases_from_tiered_pricing_table');

 //Display the purchased from Tiered Pricing Table plugin information in the admin panel
function display_purchased_from_tiered_pricing_table_info($order) {
    // Get the order ID
    $order_id = $order->get_id();
    
    // Get the purchased from Tiered Pricing Table plugin meta value
    $purchased_from_tiered_pricing_table = get_post_meta($order_id, '_purchased_from_tiered_pricing_table', true);
    
    if ($purchased_from_tiered_pricing_table === 'yes') {
        echo '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>Yes</p>';
    } else {
        echo '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>No</p>';
    }
}
add_action('woocommerce_admin_order_data_after_billing_address', 'display_purchased_from_tiered_pricing_table_info');

2

Answers


  1. The following, will add a custom field to the order if an item is tiered priced, and will display it in Admin order pages:

    // Track purchases made through WooCommerce Tiered Pricing Table plugin.
    add_action( 'woocommerce_checkout_create_order', 'track_purchases_from_tiered_pricing_table', 10, 2 );
    function track_purchases_from_tiered_pricing_table( $order, $data ) {
        $has_tiered_pricing = false; // Initializing
        
        // Loop through the cart items
        foreach ( WC()->cart->get_cart() as $item ) {
            // Loop  through extra meta keys from the product
            foreach ( wp_list_pluck( $item['data']->get_meta_data(), 'key' ) as $key ) {
                // Check for a meta_key that has 'tiered_pric' substring
                if ( strpos($key, 'tiered_pric') !== false ) {
                    $has_tiered_pricing = true;
                    break;
                }
            }
            // If an tiered priced item is found, flag the order
            if ( $has_tiered_pricing ) {
                $order->update_meta_data( '_has_tiered_pricing', '1' );
                break;
            }
        }
    }
    
     // Display the purchased from Tiered Pricing Table plugin information in the admin panel
    add_action('woocommerce_admin_order_data_after_billing_address', 'display_purchased_from_tiered_pricing_table_info');
    function display_purchased_from_tiered_pricing_table_info($order) {
        // Display if the order has tiered priced items
        printf( '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>%s</p>', 
             $order->get_meta( '_has_tiered_pricing' ) ? 'Yes' : 'No' 
        );
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    Addition: Targeting discounted tiered price items only

    If you only want to add this order metadata, for items that have "tiered prices calculated" (so a discounted tiered price), use instead the following:

    // Track purchases made through WooCommerce Tiered Pricing Table plugin.
    add_action( 'woocommerce_checkout_create_order', 'track_purchases_from_tiered_pricing_table', 10, 2 );
    function track_purchases_from_tiered_pricing_table( $order, $data ) {
        $has_tiered_pricing = false; // Initializing
        
        // Loop through the cart items
        foreach ( WC()->cart->get_cart() as $item ) {
            // Loop  through extra meta key /value pairs from the product
            foreach ( wp_list_pluck( $item['data']->get_meta_data(), 'value', 'key' ) as $key => $value ) {
                // Check for 'tiered_pricing_cart_price_calculated' meta key  
                if ( $key === 'tiered_pricing_cart_price_calculated' && $value === 'yes' ) {
                    $has_tiered_pricing = true;
                    break;
                }
            }
            // If an tiered priced item is found, flag the order
            if ( $has_tiered_pricing ) {
                $order->update_meta_data( '_has_tiered_pricing', '1' );
                break;
            }
        }
    }
    
     // Display the purchased from Tiered Pricing Table plugin information in the admin panel
    add_action('woocommerce_admin_order_data_after_billing_address', 'display_purchased_from_tiered_pricing_table_info');
    function display_purchased_from_tiered_pricing_table_info($order) {
        // Display if the order has tiered priced items
        printf( '<p><strong>Purchased from Tiered Pricing Table plugin: </strong>%s</p>', 
             $order->get_meta( '_has_tiered_pricing' ) ? 'Yes' : 'No' 
        );
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here


    Addition 2:

    How to see the cart items (product) protected extra metadata (key / value pairs).

    This plugin generates extra metadata specifically for cart items (product).

    The following code will display that extra protected metadata in the bottom of cart and checkout pages (only for admins), so you will be able to use the correct keys / values that you need to make my code working (accordingly to your product settings with this plugin):

    // Displaying cart items (product) extra protected metadata in the bottom of cart and checkout pages
    add_action('woocommerce_after_cart', 'display_product_extra_metadata_for_admins'); // cart
    add_action('woocommerce_after_checkout_form', 'display_product_extra_metadata_for_admins'); // checkout
    function display_product_extra_metadata_for_admins() {
        if( ! current_user_can('administrator') ) return; // Only admins
    
        echo '<pre>';
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $item ) {
            echo '<p>Name: '.$item['data']->get_name() . ' | Quantity:' .  $item['quantity'] . '</p>';
            print_r( wp_list_pluck( $item['data']->get_meta_data(), 'value', 'key' ) ); 
            echo '<br>';
        }
        echo '</pre>';
    }
    

    Once done, you can just remove it, or disable it.

    Login or Signup to reply.
  2. The code you provided seems to be on the right track. However, there are a few things we can check to troubleshoot the issue. Here are a few suggestions:

    Make sure that you have correctly installed and activated the WooCommerce Tiered Pricing Table plugin.

    Double-check that the class name ‘Tier_Pricing_Table’ is correct and matches the class used by the plugin. This is important for the condition class_exists(‘Tier_Pricing_Table’) to work properly.

    Ensure that the code snippet is placed within the active theme’s functions.php file or a custom plugin file.

    Verify that the WooCommerce hooks ‘woocommerce_checkout_order_processed’ and ‘woocommerce_admin_order_data_after_billing_address’ are supported by your WooCommerce version.

    If you have checked all of these aspects and the code is still not working as expected, please provide any error messages or specific details about the issue you are encountering. That information will be helpful in diagnosing the problem further.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search