php - Woocommerce - 防止将两个不同类别的商品添加到购物车

标签 php wordpress woocommerce

我想阻止用户一次将不同类别的产品添加到购物车中。 假设如果用户导航到另一个类别并尝试将产品添加到购物车,他们会先清除购物车。 请有人知道我该如何处理这个问题?我是这样的新手。 谢谢

最佳答案

我以前解决过这个问题。 My write-up is here .

它与您的请求略有不同,因为当他们尝试添加第二个类别时不会清除购物车,而是将原始商品保留在购物车中并显示警告。

为了后代:

<?php

// Enforce single parent category items in cart at a time based on first item in cart
function get_product_top_level_category ( $product_id ) {

    $product_terms            =  get_the_terms ( $product_id, 'product_cat' );
    $product_category_term    =  $product_terms[0];
    $product_category_parent  =  $product_terms[0]->parent;

    while ( $product_category_parent  !=  0 ) {
            $product_category_term    =  get_term($product_category_parent, 'product_cat' );
            $product_category_parent  =  $product_category_term->parent;
    }

    return $product_category_term;

}

add_filter ( 'woocommerce_before_cart', 'restrict_cart_to_single_category' );
function restrict_cart_to_single_category() {
        global $woocommerce;
        $cart_contents    =  $woocommerce->cart->get_cart( );
        $cart_item_keys   =  array_keys ( $cart_contents );
        $cart_item_count  =  count ( $cart_item_keys );

        // Do nothing if the cart is empty
        // Do nothing if the cart only has one item
        if ( ! $cart_contents || $cart_item_count == 1 ) {
                return null;
        }

        // Multiple Items in cart
        $first_item                    =  $cart_item_keys[0];
        $first_item_id                 =  $cart_contents[$first_item]['product_id'];
        $first_item_top_category       =  get_product_top_level_category ( $first_item_id );
        $first_item_top_category_term  =  get_term ( $first_item_top_category, 'product_cat' );
        $first_item_top_category_name  =  $first_item_top_category_term->name;

        // Now we check each subsequent items top-level parent category
        foreach ( $cart_item_keys as $key ) {
                if ( $key  ==  $first_item ) {
                        continue;
                }
                else {
                        $product_id            =  $cart_contents[$key]['product_id'];
                        $product_top_category  =  get_product_top_level_category( $product_id );

                        if ( $product_top_category  !=  $first_item_top_category ) {
                                $woocommerce->cart->set_quantity ( $key, 0, true );
                                $mismatched_categories  =  1;
                        }
                }
        }

        // we really only want to display this message once for anyone, including those that have carts already prefilled
        if ( isset ( $mismatched_categories ) ) {
                echo '<p class="woocommerce-error">Only one category allowed in cart at a time.<br />You are currently allowed only <strong>'.$first_item_top_category_name.'</strong> items in your cart.<br />To order a different category empty your cart first.</p>';
        }
}
?>

关于php - Woocommerce - 防止将两个不同类别的商品添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26373256/

相关文章:

Css 下拉在 FTL 中不起作用

php - Laravel 8 路由到 Controller 。 SEO 友好的 URL 结构

php - 为什么这段 PHP 代码在一台机器上运行而在另一台机器上失败?

php - 以纯文本形式生成某个 PHP 类的所有公共(public)函数?

php - Smarty gettext 本地化

php - WooCommerce 密码强度更改为 6 个字符

CSS 菜单事件项颜色

使用 Woocommerce 的 mySQL 查询 - 歧义列

php - 从 WooCommerce 自定义相关产品输出中排除当前产品

php - WooCommerce - 重复订单的不同运输方式