如果购物车包含特定类别(WooCommerce),则阻止添加到购物车

问题描述 投票:0回答:1

我一直在尝试调整在此线程中找到的代码以跨多个类别工作。

我设置了多个类别来反映日期:市场 1、市场 2、市场 3...

  • 如果购物车中有市场 1,则无法添加市场 2 或 3。
  • 如果购物车中有市场 2,则无法添加市场 1 或 3。
  • 如果购物车中有市场 3,则无法添加市场 1 或 2。

我改编的代码有时会起作用,但并非总是有效:

//*** Prevent mixture of market dates in same cart ***//
function dont_add_market_to_cart_containing_other_market($validation, $product_id) {

// Set flag false until we find a product in cat market (_1,_2_3...)
    $cart_has_market = false;

// Set $cat_check true if a cart item is in market cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

        if ( has_term('market-1', 'product_cat', $product->id) || has_term('market-2', 'product_cat', $product->id) || has_term('market-3', 'product_cat', $product->id) ) {
            $cart_has_market = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_market = false;
    if ( has_term('market-1', 'product_cat', $product_id) ||  has_term('market-2', 'product_cat', $product_id) || has_term('market-3', 'product_cat', $product_id) ) {
        $product_is_market = true;
    }

// Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains market date 1 and product to be added is not market, display error message and return false.
        if ($cart_has_market && !$product_is_market) {
            wc_add_notice('Sorry, you can only purchase indivifual market dates on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
        // If cart contains a product that is not market and product to be added is market, display error message and return false.
        elseif (!$cart_has_market && $product_is_market) {
            wc_add_notice('Sorry, you can only purchase indivifual market dates on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_market_to_cart_containing_other', 10, 2);
php wordpress woocommerce
1个回答
0
投票

您的代码尝试中存在一些错误和复杂性。请尝试以下方法:

add_filter('woocommerce_add_to_cart_validation', 'prevent_different_market_categories_in_cart', 10, 2);
function prevent_different_market_categories_in_cart( $passed, $product_id ) {
    // Exit if cart is empty
    if ( WC()->cart->is_empty() ) {
        return $passed;
    }
    // Here define your categories
    $categories = array('market-1', 'market-2', 'market-3');
    $taxonomy = 'product_cat'; // Product category taxonomy
    $categories_found = array(); // Initializing

    // Check the current product
    foreach( $categories as $category ) {
        if ( has_term( $category, 'product_cat', $product_id ) ) {
            $categories_found[$category] = $category;
        }
    }
    
    // Check cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        foreach( $categories as $category ) {
            if ( has_term( $category, $taxonomy, $cart_item['product_id']) ) {
                $categories_found[$category] = $category;
                break;
            }
        }
    }

    // If there is more than 1 different market categories found, avoid add to cart
    if ( count($categories_found) > 1 ) {
        wc_add_notice( __('Sorry, you can only purchase indivifual market dates on their own. To purchase this product, please checkout your current cart or empty your cart and try again'), 'error');
        $passed = false;
    }
    return $passed;
}

应该可以。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.