wordpress Woocommerce -不添加到购物车产品从另一个类别

velaa5lx  于 2023-08-03  发布在  WordPress
关注(0)|答案(2)|浏览(119)

我有几个类别,我正试图使它,使一些类别不能被添加到购物沿着其他人。(添加时,应有一个指向产品的重定向,并显示错误消息)。
我找到了帮助我here的代码,但我无法正确编辑它,使规则适用于其他类别
问题是这样的下面的类别可以添加到购物车之间的奶酪奶酪集面包面包店牛奶而下面的类别不应该与上面的类别和之间的glamp马foodtruck其他农场混合
来自链路的原始代码

<?php
//*** 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);
?>

字符串
和返工,但不工作^)当“允许”类别在购物车,我添加任何“禁止”类别其工作,但当我尝试添加产品从glamp和马不工作?两个产品都去购物车和d规则不工作

<?php
//*** Prevent mixture of certain categories in same cart ***//
function dont_add_categories_to_cart_containing_other($validation, $product_id) {
    // Set flag false until we find a product in prohibited categories
    $cart_has_prohibited = false;
    // Prohibited categories
    $prohibited_categories = array(
        'glamp',
        'horse',
        'foodtruck',
        'other',
        'farm'
    );
    // Allowed categories
    $allowed_categories = array(
        'cheese',
        'cheese-sets',
        'bread',
        'bakery',
        'milk'
    );

    // Set $cat_check true if a cart item is in prohibited categories
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $product = $cart_item['data'];
        $product_categories = wp_get_post_terms($product->id, 'product_cat', array("fields" => "slugs"));
        $intersect_categories = array_intersect($prohibited_categories, $product_categories);

        if (!empty($intersect_categories)) {
            $cart_has_prohibited = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));
    $intersect_categories = array_intersect($prohibited_categories, $product_categories);
    $product_is_prohibited = !empty($intersect_categories);

    // Return true if cart empty
    if (!WC()->cart->is_empty()) {
        // If cart contains prohibited categories and product to be added is not prohibited, display error message and return false.
        if ($cart_has_prohibited && !$product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please checkout your current cart or empty your cart and try again.', 'error');
            $validation = false;
        }
        // If cart contains a product that is not prohibited and product to be added is from the prohibited categories, display error message and return false.
        elseif (!$cart_has_prohibited && $product_is_prohibited) {
            wc_add_notice('Sorry, you can only purchase products from the allowed categories. 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_categories_to_cart_containing_other', 10, 2);
?>


帮助解决问题
谢啦,谢啦

mnemlml8

mnemlml81#

基于上述讨论。我添加了逻辑。试试下面的代码。

//*** Prevent mixture of certain categories in same cart ***//
function dont_add_categories_to_cart_containing_other($passed_validation, $product_id) {
    // Get the product categories
    $product_categories = wp_get_post_terms($product_id, 'product_cat', array("fields" => "slugs"));

    // Prohibited categories
    $prohibited_categories = array(
        'glamp',
        'horse',
        'foodtruck',
        'other',
        'farm'
    );

    // Allowed categories
    $allowed_categories = array(
        'cheese',
        'cheese-sets',
        'bread',
        'bakery',
        'milk'
    );

    // Check if the product is in a prohibited category
    $product_is_prohibited = !empty(array_intersect($prohibited_categories, $product_categories));

    // Check if the product is in an allowed category
    $product_is_allowed = !empty(array_intersect($allowed_categories, $product_categories));

    // Check if there are any prohibited products in the cart
    $cart_has_prohibited = false;
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $cart_product_id = $cart_item['product_id'];
        $cart_product_categories = wp_get_post_terms($cart_product_id, 'product_cat', array("fields" => "slugs"));

        if (!empty(array_intersect($prohibited_categories, $cart_product_categories))) {
            $cart_has_prohibited = true;
            break;
        }
    }

    // Check if the cart has any allowed products
    $cart_has_allowed = false;
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        $cart_product_id = $cart_item['product_id'];
        $cart_product_categories = wp_get_post_terms($cart_product_id, 'product_cat', array("fields" => "slugs"));

        if (!empty(array_intersect($allowed_categories, $cart_product_categories))) {
            $cart_has_allowed = true;
            break;
        }
    }

    // Return true if cart is empty
    if (WC()->cart->is_empty()) {
        return $passed_validation;
    }
    

    // If cart contains prohibited products and the product to be added is not prohibited, display error message and return false.
    if ($cart_has_prohibited && !$product_is_prohibited) {
        wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please review your current cart or empty your cart and try again.', 'error');
        return false;
    }

    // If cart contains prohibited products and the product to be added is prohibited, display error message and return false.
    if ($cart_has_prohibited && $product_is_prohibited) {
        wc_add_notice('Sorry, you can only purchase products from the allowed categories. Please review your current cart or empty your cart and try again.', 'error');
        return false;
    }

    // If cart contains an allowed product and the product to be added is prohibited, display error message and return false.
    if ($cart_has_allowed && $product_is_prohibited) {
        wc_add_notice('Sorry, you can only purchase one product from the prohibited categories. Please review your current cart or empty your cart and try again.', 'error');
        return false;
    }

    /*// If cart contains an allowed product and the product to be added is also allowed, display error message and return false.
    if ($cart_has_allowed && $product_is_allowed) {
        wc_add_notice('Sorry, you can only purchase one product from each category. Please review your current cart or empty your cart and try again.', 'error');
        return false;
    }*/

    return $passed_validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_categories_to_cart_containing_other', 10, 2);

字符串

34gzjxbg

34gzjxbg2#

Bhautik感谢你的帮助,你的问题和你的帮助比以往任何时候都更激励我。我找到了另一个最终适合我的选择。

<?php
    function custom_add_to_cart_validation( $passed, $product_id, $quantity ) {
        // Get the product categories
        $product_categories = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) );
    
        // Creating a list of prohibited combinations of categories
        $prohibited_combinations = array(
            array( 'category-c', 'category-a' ),
            array( 'category-c', 'category-b' ),
            array( 'category-d', 'category-a' ),
            array( 'category-d', 'category-b' ),
        );
    
        // Check whether the selected product is in the basket
        if ( WC()->cart->get_cart_contents_count() > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                $cart_item_product_id = $cart_item['product_id'];
                $cart_item_product_categories = wp_get_post_terms( $cart_item_product_id, 'product_cat', array( 'fields' => 'slugs' ) );
    
                // Checking prohibited combinations
                foreach ( $prohibited_combinations as $combination ) {
                    if ( in_array( $combination[0], $product_categories ) && in_array( $combination[1], $cart_item_product_categories ) ) {
                        // If a forbidden combination is found, we output an error message
                        wc_add_notice( 'You cannot add products from these categories together to the cart.', 'error' );
                        return false;
                    }
                }
            }
        }
        
        return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'custom_add_to_cart_validation', 10, 3 );
?>

字符串

相关问题