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

WooCommerce中指定产品分类禁用加入购物车按钮技术问询

Solution: Disable Cross-Category Add to Cart Buttons in WooCommerce Subscriptions

Hey there! Let’s work through how to disable "Add to Cart" buttons for one product category when a user has selected the other (Juices vs Cleans) in your WooCommerce subscription setup. This involves tracking the user’s selected category server-side and modifying product purchasability based on that choice.

Step 1: Store the User’s Selected Category in WooCommerce Session

First, we need to save the user’s chosen category to their WooCommerce session when they submit their subscription selection form. Add this code to your theme’s functions.php file (or a custom plugin):

// Save selected subscription category to WooCommerce session
add_action('template_redirect', 'store_selected_category_to_session');
function store_selected_category_to_session() {
    // Check if the form was submitted with a valid category
    if (isset($_POST['selected_category']) && !empty($_POST['selected_category'])) {
        $category_slug = sanitize_text_field($_POST['selected_category']);
        // Verify the category exists in WooCommerce
        $category = get_term_by('slug', $category_slug, 'product_cat');
        
        if ($category) {
            WC()->session->set('selected_subscription_category', $category_slug);
        }
    }
}

Step 2: Disable Add to Cart for Conflicting Categories

Next, we’ll modify two key WooCommerce filters to disable the "Add to Cart" button and mark conflicting products as non-purchasable. Add this to your functions.php as well:

// Customize Add to Cart text and disable purchasability for conflicting categories
add_filter('woocommerce_product_add_to_cart_text', 'disable_conflicting_category_add_to_cart', 10, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'disable_conflicting_category_add_to_cart', 10, 2);
add_filter('woocommerce_is_purchasable', 'disable_conflicting_category_purchasable', 10, 2);

function disable_conflicting_category_add_to_cart($button_text, $product) {
    $selected_category = WC()->session->get('selected_subscription_category');
    // If no category is selected, return default button text
    if (!$selected_category) return $button_text;

    // Get slugs of categories assigned to the current product
    $product_category_slugs = wp_get_post_terms(
        $product->get_id(), 
        'product_cat', 
        array('fields' => 'slugs')
    );

    // Define which categories conflict with each other
    $conflict_map = array(
        'juices' => 'cleans',
        'cleans' => 'juices'
    );

    // Check if the product belongs to the conflicting category
    if (isset($conflict_map[$selected_category]) && in_array($conflict_map[$selected_category], $product_category_slugs)) {
        return __('Category Selected - Add to Cart Disabled', 'woocommerce');
    }

    return $button_text;
}

function disable_conflicting_category_purchasable($is_purchasable, $product) {
    $selected_category = WC()->session->get('selected_subscription_category');
    if (!$selected_category) return $is_purchasable;

    $product_category_slugs = wp_get_post_terms(
        $product->get_id(), 
        'product_cat', 
        array('fields' => 'slugs')
    );

    $conflict_map = array(
        'juices' => 'cleans',
        'cleans' => 'juices'
    );

    if (isset($conflict_map[$selected_category]) && in_array($conflict_map[$selected_category], $product_category_slugs)) {
        return false;
    }

    return $is_purchasable;
}

Step 3: Update Your Subscription Selection Form

Make sure your user selection form submits the chosen category to trigger the session save. Here’s an example form you can use (adjust the action URL to match your shop/category page):

<form method="post" action="<?php echo esc_url(get_permalink(get_option('woocommerce_shop_page_id'))); ?>">
    <div class="subscription-selection">
        <label for="selected_category">Choose Your Subscription Category:</label>
        <select id="selected_category" name="selected_category" required>
            <option value="juices">Juices</option>
            <option value="cleans">Cleans</option>
        </select>

        <!-- Add your subscription days and other fields here -->
        <label for="subscription_days">Subscription Duration (Days):</label>
        <input type="number" id="subscription_days" name="subscription_days" min="7" max="30" required>

        <button type="submit" class="button">Proceed to Shop</button>
    </div>
</form>

Step 4: Clear Session When Cart is Emptied

To reset the selected category when the user clears their cart, add this code to functions.php:

// Clear selected category session when cart is emptied
add_action('woocommerce_cart_emptied', 'clear_selected_category_session');
function clear_selected_category_session() {
    WC()->session->__unset('selected_subscription_category');
}

Important Notes:

  • Replace 'juices' and 'cleans' with your actual product category slugs (check WooCommerce > Products > Categories to find them).
  • Test this flow thoroughly: submit the selection form, navigate to both category pages, and confirm only the selected category’s products have active "Add to Cart" buttons.
  • If you’re using a page builder, adjust the form code to fit your builder’s structure.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:32:09