You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce:针对特定商品ID自动添加/移除购物车优惠券

Solution: Auto Apply/Remove Coupon Based on Specific Cart Product

Got it, let's fix this issue for you. The problem right now is that your coupon stays applied even after removing the target product (ID 1362) from the cart. We need to adjust the logic to only keep the coupon active when product 1362 is present, and remove it immediately when that product is gone—while keeping the coupon applicable to the entire cart, not just the target product.

Step 1: Replace Your Existing Code

Swap out your current bbloomer_apply_... function with this code. Don't forget to replace YOUR_COUPON_CODE with the actual coupon code you're using in WooCommerce:

// Helper function to check if target product is in cart
function has_target_product_in_cart() {
    $target_product_id = 1362;
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Checks both simple and variable products (uses product_id, not variation_id)
        $product_id = $cart_item['product_id'];
        if ( $product_id === $target_product_id ) {
            return true;
        }
    }
    return false;
}

// Manage coupon application/removal based on target product presence
function manage_coupon_for_target_product() {
    $coupon_code = 'YOUR_COUPON_CODE'; // Replace with your actual coupon code
    $cart = WC()->cart;

    // Exit early if cart is empty to avoid unnecessary checks
    if ( $cart->is_empty() ) {
        return;
    }

    $has_target_product = has_target_product_in_cart();
    $is_coupon_applied = in_array( $coupon_code, $cart->get_applied_coupons() );

    // Scenario 1: Target product exists, coupon not applied → apply it
    if ( $has_target_product && !$is_coupon_applied ) {
        $cart->apply_coupon( $coupon_code );
        wc_add_notice( '优惠券已自动应用!', 'success' );
    }
    // Scenario 2: Target product is removed, coupon still applied → remove it
    elseif ( !$has_target_product && $is_coupon_applied ) {
        $cart->remove_coupon( $coupon_code );
        wc_add_notice( '优惠券已移除,因为指定商品不在购物车中。', 'notice' );
    }
}

// Hook into cart and checkout pages to run logic on cart updates
add_action( 'woocommerce_before_cart', 'manage_coupon_for_target_product' );
add_action( 'woocommerce_before_checkout_process', 'manage_coupon_for_target_product' );

Step 2: Configure Your Coupon Correctly

To ensure the coupon works for the entire cart (not just product 1362):

  • Go to WooCommerce → Coupons → Edit your coupon
  • Under the Usage restriction tab, leave the Products field empty (do NOT add product 1362 here)
  • Double-check other restrictions (like minimum order amount, expiration date) don't conflict with your needs

How This Logic Works

  • The helper function has_target_product_in_cart() scans every item in the cart to check for product 1362
  • The main function handles two key scenarios: applying the coupon when the target product is present, and removing it when the product is gone
  • We hook into both the cart page and checkout process to ensure the logic runs whenever the cart is modified (adding/removing items, updating quantities)

Test Cases to Verify

  1. Add product 1362 to cart → coupon auto-applies
  2. Add other products to the same cart → coupon stays active
  3. Remove product 1362 from cart → coupon is automatically removed
  4. Re-add product 1362 → coupon is applied again

内容的提问来源于stack exchange,提问作者Playzare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:26:07