Itβs possible to do this job by adding a discount to the cart based on requirements and subcategory calculations ...
The code:
add_action( 'woocommerce_cart_calculate_fees','table_chairs_cart_discount', 10, 1 );
function table_chairs_cart_discount($cart_object) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$chairs_total = 0;
$table_total = 0;
$discount = 0;
foreach($cart_object->get_cart() as $item_key => $item):
$item_line_total = $item["line_total"];
if(has_term('chairs', 'product_cat', $item['product_id']))
$chairs_total += $item_line_total;
if(has_term('table', 'product_cat', $item['product_id']))
$table_total += $item_line_total;
endforeach;
if( $table_total <= $chairs_total && $chairs_total > 0 )
$discount -= $table_total;
elseif ($chairs_total > 0)
$discount -= $chairs_total;
if ($discount != 0)
$cart_object->add_fee( __( 'Chairs discount', 'woocommerce' ), $discount, false );
}
The code goes in the function.php file of your active child theme (or theme). Or also in any php file plugins.
: