2
如果購物車中包含特定類別的產品,我試圖自動添加優惠券代碼。顯示折扣金額後,必須在合計中更新價格。根據產品類別自動添加WooCommerce優惠券代碼
但我無法看到總數的任何變化,請幫助我。
我的代碼:
add_action('wc_cart_product_subtotal' , 'getsubtotalc');
function getsubtotalc ($product_subtotal, $_product, $quantity, $object) {
if(is_cart() || is_checkout()) {
global $woocommerce, $product;
global $total_qty;
/*$coupon_code = 'drawer';
if ($woocommerce->cart->has_discount($coupon_code)) return;*/
foreach ($woocommerce->cart->cart_contents as $product) {
if(has_term(array('t-shirts-d','socks-d','joggers-d','boxers-d'), 'product_cat', $cart_item['product_id'])){
$coupon_code = 'drawer';
if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>The number of Product in your order is greater than 10 so a 10% Discount has been Applied!</strong>
</div>';
}
}
}
}
感謝
這工作完美,並感謝使代碼看起來乾淨。 – SandeepTete