When the product subtotal is more than 3000, it has to be another price.
I added it as a meta field to products. On Stack Overflow, a long time ago someone advised me the filter for a special price called woocommerce_cart_item_name
I modified it a little for my purpose:
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
$threshold = 3000; // Change price if > 3000
$_product = $cart_item['data'];
$meta = $_product->get_meta( '_opt_price_var');
$price = $_product->get_regular_price();
$subtotal = $cart_item['quantity'] * $price;
if ( $subtotal > $threshold && ( $meta )) {
$_product->set_price( $meta );
}
echo $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
The special price is showing only in the cart form, but not in cart totals and not in the checkout, in totals and in checkout the price is still regular, the main calculator doesn’t take it from this filter.
How can I fix it? How to include it in the main calculator too?
I tried different hooks that can be responsive for the calculator – both add_action
and add_filter
, with and without 10, 2
– it isn’t it.
add_action('woocommerce_before_shipping_calculator', 'kia_add_subtitle_to_cart_product',10,2);
add_action('woocommerce_before_cart_totals', 'kia_add_subtitle_to_cart_product', 10, 2 );
2
Answers
The
woocommerce_cart_item_name
hook is being misused in your code attempt. A filter hook should always return something compared to the use of echo.You should use the
woocommerce_before_calculate_totals
hook insteadSo you get: