Plugin Author
Mewz
(@mewz)
Hi @westham00,
In the latest version 2.0.1 you’d do something like this:
$total_qty = $cart_item['quantity'] * mewz_wcas_get_product_multiplier($cart_item['data']);
Thank you for the quick response? Did you just add this in the recent update last night? Impressive if so!
And now i’m finally trying to get the same value and add it to my order. I don’t seem to be able to retrieve it in the same way. Could you guide me where i’ve gone wrong. Thanks.
/**
* Add TTootal quantity to the product column in WooCommerce order details.
.
*/
function add_total_qty_to_product_column($item_name, $item, $order_id) {
// Debug....
error_log('Item Name: ' . $item_name);
// Get Product object
$product = $item->get_product();
// Debug
error_log('Product Object: ' . print_r($product, true));
// Calculate total_qty
$total_qty = $item->get_quantity() * mewz_wcas_get_product_multiplier($product);
// Debug
error_log('Total Qty: ' . $total_qty);
// Append total quantity to item name
$total_qty_display = sprintf('<br><small><strong>Total Qty:</strong> %d</small>', $total_qty);
return $item_name . $total_qty_display;
}
add_filter('woocommerce_order_item_name', 'add_total_qty_to_product_column', 10, 3);
// CSS
function custom_order_item_column_css() {
echo '<style>
.woocommerce-order-items .order-item .item-name small {
display: block;
font-size: 0.9em;
color: #555;
}
</style>';
}
add_action('admin_head', 'custom_order_item_column_css');
Plugin Author
Mewz
(@mewz)
Did you just add this in the recent update last night?
Well no, this function was already available in v2.0.0. I just made the 2nd parameter optional so it’s easier to use.
Your code works for me. But keep in mind there are many different ways to show custom metadata on order items. For example, your current code will show on frontend order details, but not in the backend when editing an order.
You have to take a different approach if you want this to show on admin orders, frontend orders, or both. There’s also the decision to be made whether to show it dynamically, or add it as real order item metadata when the order is placed/created.
If your code isn’t even working on frontend order details, then it could be that you’re using “any” variations which requires a different approach, or perhaps the specific product variation doesn’t exist anymore. In the latter case the product type will be variable instead of variation. Adding order item metadata when the order is placed helps to avoid issues like this.