PHP是购物车中商品的最低数量

PHP是购物车中商品的最低数量,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在建立一个咖啡购买网站,我有一个问题,试图确保买家只能完成结帐,如果他们有最低数量的产品 例如,如果购物车中有产品A,则必须至少有产品类别C才能结账 有4个单独的啤酒厂需要具有此功能,它们的最低值不同。我已经搜索了插件,但找不到一个可以满足我需要的插件 任何帮助都将不胜感激 我试过使用这个代码 // Set minimum quantity per product before checking out if(woo_in_cart($5579)) { add_action( 'woocom

我正在建立一个咖啡购买网站,我有一个问题,试图确保买家只能完成结帐,如果他们有最低数量的产品

例如,如果购物车中有产品A,则必须至少有产品类别C才能结账

有4个单独的啤酒厂需要具有此功能,它们的最低值不同。我已经搜索了插件,但找不到一个可以满足我需要的插件

任何帮助都将不胜感激

我试过使用这个代码

// Set minimum quantity per product before checking out
if(woo_in_cart($5579)) {
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_qty_per_product' );
function spyr_set_min_qty_per_product() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {  
    global $woocommerce;

    // Product Id and Min. Quantities per Product
    $product_min_qty = array( 
        array( 'id' => 91, 'min' => 4),
    );

    // Will increment
    $i = 0;
    // Will hold information about products that have not
    // met the minimum order quantity
    $bad_products = array();

    // Loop through the products in the Cart
    foreach( $woocommerce->cart->cart_contents as $product_in_cart ) {
        // Loop through our minimum order quantities per product
        foreach( $product_min_qty as $product_to_test ) {
            // If we can match the product ID to the ID set on the minimum required array
            if( $product_to_test['id'] == $product_in_cart['product_id'] ) {
                // If the quantity required is less than than the quantity in the cart now
                if( $product_in_cart['quantity'] < $product_to_test['min'] ) {
                    // Get the product ID
                    $bad_products[$i]['id'] = $product_in_cart['product_id'];
                    // Get the Product quantity already in the cart for this product
                    $bad_products[$i]['in_cart'] = $product_in_cart['quantity'];
                    // Get the minimum required for this product
                    $bad_products[$i]['min_req'] = $product_to_test['min'];
                }
            }
        }
        // Increment $i
        $i++;
    }

    // Time to build our error message to inform the customer
    // About the minimum quantity per order.
    if( is_array( $bad_products) && count( $bad_products ) > 1 ) {
        // Lets begin building our message
        $message = '<strong>A minimum quantity of the choosen Brewer has not been met. (Kuerig 140 Reqiures A Minimum of 4 Boxes to Order)<br />(Kuerig 150 Reqiures A Minimum of 8 Boxes to Order)<br />(Kuerig K3000SE Reqiures A Minimum of 12 Boxes to Order)<br />(Kuerig Bolt Carafe Reqiures A Minimum of 10 Boxes to Order)</strong><br />';
        foreach( $bad_products as $bad_product ) {
            // Append to the current message
            $message .= get_the_title( $bad_product['id'] ) .' requires a minimum quantity of '
                     . $bad_product['min_req'] 
                     .'. You currently have: '. $bad_product['in_cart'] .'.<br />';
        }
        wc_add_notice( $message, 'error' );
    }
}
}
//结账前设置每个产品的最低数量
如果(在购物车里打球(5579美元)){
添加操作('woocommerce\u check\u cart\u items'、'spyr\u set\u min\u Quantity\u per\u product');
功能spyr\u set\u min\u每个产品的数量(){
//仅在购物车或结帐页面中运行
如果(is_cart()| | is_checkout()){
全球商业;
//每个产品的产品Id和最小数量
$product\U min\U qty=数组(
数组('id'=>91,'min'=>4),
);
//将增加
$i=0;
//将保存有关尚未发布的产品的信息
//满足最低订购量
$bad_products=array();
//循环浏览购物车中的产品
foreach($woocommerce->cart->cart\u内容作为$product\u在\u cart中){
//循环查看每个产品的最低订购量
foreach($product\U min\U数量为$product\U to\U test){
//如果我们可以将产品ID与所需的最小数组上设置的ID匹配
如果($product_to_test['id']==$product_in_cart['product_id'])){
//如果所需数量小于当前购物车中的数量
if($product_in_cart['quantity']<$product_to_test['min'])){
//获取产品ID
$bad_products[$i]['id']=$product_in_cart['product_id'];
//获取此产品的购物车中已存在的产品数量
$bad_products[$i]['in_cart']=$product_in_cart['quantity'];
//获取此产品所需的最小值
$bad_products[$i]['min_req']=$product_to_test['min'];
}
}
}
//增量$i
$i++;
}
//是时候建立我们的错误消息来通知客户了
//关于每个订单的最小数量。
if(is_数组($bad_产品)和计数($bad_产品)>1){
//让我们开始构建我们的信息
$message='未满足所选啤酒厂的最低数量要求。(Kuerig 140要求至少订购4箱)
(Kuerig 150要求至少订购8箱)
(Kuerig K3000SE要求至少订购12箱)
(Kuerig Bolt Carafe要求至少订购10箱)

; foreach($bad_产品作为$bad_产品){ //附加到当前消息 $message.=获取标题($bad_product['id'])。“需要最少数量的” .$bad_产品['min_REQUE'] .。您当前有:'.$bad_product['in_cart']。
; } wc_添加_通知($message,'error'); } } }
请向我们展示您迄今为止的尝试。我认为您需要制作不同的包装,可能需要使用不同的产品(选项)。这样你就避免了基于这种特殊产品的最低复杂量。如果不是的话,这是一个真正的发展。谢谢你的推荐LoicTheAztec。有大约100种不同的咖啡盒供一个人选择。我不认为制作不同的包装是我的目标。行得通吗?三件事,1。
$woocommerce
应该替换为
WC()
和2.什么是
woo_in_cart($5579)
?和
if(woo_in_cart($5579)){
应该在
add_action()中
callback。根据您显示的代码,它将永远不会运行,因为这总是错误的。感谢您的反馈helgatheviking。我对PHP编码没有经验,更不用说设置woocommerce脚本了。5589是咖啡酿酒商的产品id。我尝试使用最小/最大数量,但我没有对任何产品进行分组。每个客户应该能够选择任何组合的咖啡,只要达到最低限度。我做了一些建议的编辑,这次没有破坏网站,但错误警告从未出现。请告诉我们您迄今为止尝试了什么。我认为您需要制作不同的包,可能使用不同的产品(选项)。这样你就避免了基于这种特殊产品的最低复杂量。如果不是的话,这是一个真正的发展。谢谢你的推荐LoicTheAztec。有大约100种不同的咖啡盒供一个人选择。我不认为制作不同的包装是我的目标。行得通吗?三件事,1。
$woocommerce
应该替换为
WC()
和2.什么是
woo_in_cart($5579)
?和
if(woo_in_cart($5579)){
应该在
add_action()中
callback。根据您显示的代码,它将永远不会运行,因为这总是错误的。感谢您的反馈helgatheviking。我对PHP编码没有经验,更不用说设置woocommerce脚本了。5589是咖啡酿酒商的产品id。我尝试使用最小/最大数量,但我没有对任何产品进行分组。每个客户应该能够选择任何组合的咖啡,只要达到最低限度。我做了一些建议的编辑,这次没有破坏网站,但错误警告从未出现