Как изменить общую сумму корзины с помощью фильтра или крючка – WooCommerce

Хорошо, поэтому я пытаюсь написать фильтр или крючок, который изменит общую сумму корзины покупок в зависимости от общего количества товаров в корзине покупок. Например, если в корзине есть три элемента, я хочу вычесть двадцать долларов из общего заказа. Ниже приведен код, который я имею до сих пор, любая помощь очень ценится!

add_filter('woocommerce_cart_contents_total', 'bundle_deals'); function bundle_deals( $cart_contents_total, $cart_contents_count) { global $woocommerce; if ($woocommerce->cart->get_cart()->cart_contents_count <= 3) { $cart_contents_total = $woocommerce->cart->get_cart()->cart_contents_total - 20.00; } return $cart_contents_total; } 

Общая сумма может быть изменена с последующего крючка.

 add_action('woocommerce_calculate_totals', array($this, 'calculate_totals'), 10, 1); function calculate_totals($totals){ //your code } 
 function woocommerce_cart_subtotal( $cart_subtotal, $compound, $obj ){ $t = 0; foreach ( $obj->cart_contents as $key => $product ) : $product_price = $product['line_total']; foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : $product_price = 19;//wpq_9522_discount( $product['line_total'], $coupon->discount_type, $coupon->amount ); endforeach; $t += $product_price; endforeach; return $cart_subtotal;//( $t > 0 ) ? sprintf( '<s>%s</s> %s', $cart_subtotal, wc_price( $t ) ) : $cart_subtotal ; } add_filter( 'woocommerce_cart_subtotal', 'woocommerce_cart_subtotal', 99, 3 );