Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 限制购物车中允许的不同产品的数量_Php_Wordpress_Woocommerce_Product_Cart - Fatal编程技术网

Php 限制购物车中允许的不同产品的数量

Php 限制购物车中允许的不同产品的数量,php,wordpress,woocommerce,product,cart,Php,Wordpress,Woocommerce,Product,Cart,使用answer,我可以在我的商店的购物车页面中限制产品的总数量。但我需要限制购物车中不同物品的数量,但数量不受限制,并限制产品总价格 例如,我的购物车中有10件商品,但总产品数量为50件,总产品价格金额为5000件。 如果我在购物车10中设置不同的产品数量,总价格为5000,则我的客户无法将另一个产品添加到购物车中 如何才能做到这一点?您可以使用以下方法限制允许添加到购物车中的不同产品的数量: // Checking and validating when products are added

使用answer,我可以在我的商店的购物车页面中限制产品的总数量。但我需要限制购物车中不同物品的数量,但数量不受限制,并限制产品总价格

例如,我的购物车中有10件商品,但总产品数量为50件,总产品价格金额为5000件。 如果我在购物车10中设置不同的产品数量,总价格为5000,则我的客户无法将另一个产品添加到购物车中


如何才能做到这一点?

您可以使用以下方法限制允许添加到购物车中的不同产品的数量:

// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_n_products_allowed_in_cart', 10, 3 );
function only_n_products_allowed_in_cart( $passed, $product_id, $quantity ) {
    $items_limit = 10;
    $total_count = count( WC()->cart->get_cart() ) + 1;

    if( $total_count > $items_limit ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( sprintf( __( "You can’t have more than %s different products in cart", "woocommerce" ), $items_limit ), "error" );
    }
    return $passed;
}

代码进入活动子主题(或活动主题)的functions.php文件。

您可以使用以下方法限制允许添加到购物车的不同产品的数量:

// Checking and validating when products are added to cart
add_filter( 'woocommerce_add_to_cart_validation', 'only_n_products_allowed_in_cart', 10, 3 );
function only_n_products_allowed_in_cart( $passed, $product_id, $quantity ) {
    $items_limit = 10;
    $total_count = count( WC()->cart->get_cart() ) + 1;

    if( $total_count > $items_limit ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( sprintf( __( "You can’t have more than %s different products in cart", "woocommerce" ), $items_limit ), "error" );
    }
    return $passed;
}

代码进入活动子主题(或活动主题)的functions.php文件。

我收到一些错误“您试图保存的代码段在第11行产生了致命错误:语法错误,意外“;”,预期“)”。@LoicTheAztecit工作正常,thx。有可能限制购物车的总数量吗?@MusaBaltacı你试过什么?尝试执行一些代码尝试,如果您有任何问题,请提出一个新问题,提供您的代码尝试、详细信息和解释。我收到一些错误“您试图保存的代码段在第11行产生了致命错误:语法错误,意外“;”,预期“)”@LoicTheAztecit工作正常,谢谢。有可能限制购物车的总数量吗?@MusaBaltacı你试过什么?试着做一些代码尝试,如果你有任何问题,问一个新的问题,提供你的代码尝试,细节和解释。