某些产品的最低订购量,但不包括其他两种产品(批量)

时间:2020-10-08 作者:Adeola Adeoti

我有一个卖书的客户,他们所有的书(除了2种产品,特价)只能以12本的组合购买。我得到了一个脚本,允许我声明需要12个批次,但我需要从规则中排除这2个特价产品。

这是剧本

// Set a minimum number of products requirement before checking out
add_action( \'woocommerce_check_cart_items\', \'spyr_set_min_num_products\' );
function spyr_set_min_num_products() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set the minimum number of products before checking out
        $minimum_num_products = 12;
        // Get the Cart\'s total number of products
        $cart_num_products = WC()->cart->cart_contents_count;

        // Compare values and add an error is Cart\'s total number of products
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 20 products is required before checking out. (Cont. below)
        // Current number of items in the cart: 6   
        if( $cart_num_products < $minimum_num_products ) {
            // Display our error message
            wc_add_notice( sprintf( \'<strong>A Minimum of %s products is required before checking out.</strong>\' 
                . \'<br />Current number of items in the cart: %s.\',
                $minimum_num_products,
                $cart_num_products ),
            \'error\' );
        }
    }
}
我将非常感谢有能力的开发人员在这里整理这一点。

提前谢谢你。

1 个回复
最合适的回答,由SO网友:Ivan Shatsky 整理而成

试试这个(更换$special_products = array( <id1>, <id2>, ... ); 使用您的产品ID列表):

// Set a minimum number of products requirement before checking out
add_action( \'woocommerce_check_cart_items\', \'spyr_set_min_num_products\' );
function spyr_set_min_num_products() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        // List of "special" products IDs
        $special_products = array( <id1>, <id2>, ... );

        // Get an array of product IDs
        $products = array_column( WC()->cart->get_cart(), \'product_id\' );
        if ( $products ) {
            foreach ( $products as $product ) {
                // Return immediately if a "special" product ID found
                // (therefore skipping product count check)
                if ( in_array( $product, $special_products ) ) return;
            }
        }

        // Set the minimum number of products before checking out
        $minimum_num_products = 12;
        // Get the Cart\'s total number of products
        $cart_num_products = WC()->cart->get_cart_contents_count();

        // Compare values and add an error is Cart\'s total number of products
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of 20 products is required before checking out. (Cont. below)
        // Current number of items in the cart: 6   
        if( $cart_num_products < $minimum_num_products ) {
            // Display our error message
            wc_add_notice( sprintf( \'<strong>A Minimum of %s products is required before checking out.</strong>\' 
                . \'<br />Current number of items in the cart: %s.\',
                $minimum_num_products,
                $cart_num_products ),
            \'error\' );
        }
    }
}
I替换$cart_num_products = WC()->cart->cart_contents_count; 具有$cart_num_products = WC()->cart->get_cart_contents_count(); 因为自WooCommerce 3.0以来,第一个表单已被弃用。global $woocommerce; 现在也不需要了$woocommerce 对象应通过WC() 作用

相关推荐