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
The following, will add a custom field to the order if an item is tiered priced, and will display it in Admin order pages:
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:
Code goes in function.php file of your active child theme (or active theme). Tested and works.
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):
Once done, you can just remove it, or disable it.
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.