Add Cart Item meta data based on other items

I use the following function to show the discount in the cart if a product in the cart is on sale:

function my_custom_show_sale_price_at_cart( $old_display, $cart_item,
$cart_item_key ) {
/** @var WC_Product $product */
$product = $cart_item[‘data’];
if ( $product ) {
return $product->get_price_html();
return $old_display;
add_filter( ‘woocommerce_cart_item_price’, ‘my_custom_show_sale_price_at_cart’, 10, 3 );

as shown here:

This works fine. However, I have another function which checks for different conditions and gives discounts if those conditions are met. The function is hooked to ‘woocommerce_before_calculate_totals’ hook. As the function is giving discounts “on the fly”, the cart items are technically not “on sale” and then the above function does not show the discount for items dynamically “discounted”, only those on sale in WooCommerce.

Therefore, I am looking for a way to handle this. My best idea is to create some custom meta data for each cart item if the cart item is dynamically discounted. However, I can only find examples of editing the meta data based on the individual cart_item and not on the whole cart.

Any ideas?

Read more here:: Add Cart Item meta data based on other items

Leave a Reply

Your email address will not be published. Required fields are marked *