PHP Woocommerce购物车中的最低产品数量

时间:2016-07-05 14:25:21

标签: php wordpress woocommerce

我正在建立一个咖啡购买网站,我遇到的问题是确保买家只有在产品数量最少的情况下才能完成结账。

例如,如果产品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' );
    }
}
}

0 个答案:

没有答案