Prevent add to cart if cart contains a specific category (WooCommerce)

限于喜欢 提交于 2021-02-16 21:24:46

问题


I have a store containing 5 categories. It's multivendor and due to shipping complications, I can only sell products from one specific category ('paint') when they are alone in the cart. So I need to prevent add to cart of any non-paint products when the cart contains paint, and display an error message, and I need to prevent paint products being added to a cart containing any other categories, and disaply an error message.

I've tried to cobble together this code from snippets I've found lying around on Stackoverflow and elsewhere. The logic seems to work in my brain, but when I try to implement the code (through functions.php) it prevents any product from being added to the cart unless the cart is empty.

Here's my code:

//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other() {

// Set flag false until we find a product in cat paint
$cat_check = false;

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

    $product = $cart_item['data'];

    if ( has_term( 'paint', 'product_cat', $product->id ) ) {
                $cat_check = true;
        // break because we only need one "true" to matter here
        break;
        }
}
// Return true if cart empty
if ( WC()->cart->get_cart_contents_count() == 0 ) {
    return true;
    }
    // If cart contains paint and product to be added is not paint, display error message and return false.
    elseif ( $cat_check && get_queried_object()->term_id != '245778522') {
        wc_add_notice( 'Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again' , 'error' );
        return false;
    }
    // If cart contains a product that is not paint and product to be added is paint, display error message and return false.
    elseif (!$cat_check && get_queried_object()->term_id = '245778522') {
        wc_add_notice( 'Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again' , 'error' );
        return false;
    }
    // Otherwise, return true.
    return true;
}

add_filter( 'woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other' );

回答1:


//*** Prevent mixture of paint and other prods in same cart ***//
function dont_add_paint_to_cart_containing_other($validation, $product_id) {

// Set flag false until we find a product in cat paint
    $cart_has_paint = false;

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

        $product = $cart_item['data'];

        if (has_term('paint', 'product_cat', $product->id)) {
            $cart_has_paint = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_paint = false;
    if (has_term('paint', 'product_cat', $product_id)) {
        $product_is_paint = true;
    }

// Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains paint and product to be added is not paint, display error message and return false.
        if ($cart_has_paint && !$product_is_paint) {
            wc_add_notice('Sorry, you can only purchase paint products 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 paint and product to be added is paint, display error message and return false.
        elseif (!$cart_has_paint && $product_is_paint) {
            wc_add_notice('Sorry, you can only purchase paint products 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_paint_to_cart_containing_other', 10, 2);

Change to this




回答2:


// If cart contains a product that is not paint and product to be added is paint, display error message and return false.
    elseif (!$cat_check && get_queried_object()->term_id = '245778522') {
        wc_add_notice( 'Sorry, you can only purchase paint products on their own. To purchase this product, please checkout your current cart or empty your cart and try again' , 'error' );
        return false;
    }

Change this.. your if condition needs '=='



来源:https://stackoverflow.com/questions/45189297/prevent-add-to-cart-if-cart-contains-a-specific-category-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!