最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - minimum order item for certain products exempting 2 other products (in bulk)

programmeradmin1浏览0评论

I have a client that sells books and all their books (except for 2 products, the show special) can be purchased in assorted lots of 12 only. I have gotten a script that allows me state that 12 lots is required but I need to exclude the 2 specials products from the rule.

Here is the script

// 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' );
        }
    }
}

I will be most grateful to have this sorted by able developers here.

Thank you in advance.

I have a client that sells books and all their books (except for 2 products, the show special) can be purchased in assorted lots of 12 only. I have gotten a script that allows me state that 12 lots is required but I need to exclude the 2 specials products from the rule.

Here is the script

// 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' );
        }
    }
}

I will be most grateful to have this sorted by able developers here.

Thank you in advance.

Share Improve this question edited Oct 8, 2020 at 8:05 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 8, 2020 at 3:03 Adeola AzibaAdeola Aziba 136 bronze badges 8
  • Need some clarifications. What should happens if cart contains a special product and less than 12 regular products (lets say 1 and 11)? Should that special product considered as twelfth product or not? – Ivan Shatsky Commented Oct 8, 2020 at 10:09
  • @IvanShatsky, thank you for replying. I just want to be able to checkout the special products without the compulsory of 12 products per cart. – Adeola Aziba Commented Oct 9, 2020 at 11:05
  • So if the cart contains at least one of the special products it doen't matter how many regular products (if any) are in the cart? Lets say 1 special and 0 regular, 1 special and 1 regular, and so on, the user still should be able to checkout. Am I understand correctly? – Ivan Shatsky Commented Oct 9, 2020 at 13:17
  • And one more question - how those special products should be identified? Would it be acceptible to you to have a line like $special_products = array( <id1>, <id2>, ... ); within your function so product identification will occur by the hardcoded list of their IDs? – Ivan Shatsky Commented Oct 9, 2020 at 13:21
  • One of the other options is to define some custom shipping class and assign that class to those special products. That way you'll be able to manage those products via admin interface without the need to alter PHP code if you would need to make some changes to those rules. – Ivan Shatsky Commented Oct 9, 2020 at 14:59
 |  Show 3 more comments

1 Answer 1

Reset to default 1

Try this one (replace $special_products = array( <id1>, <id2>, ... ); with your list of product IDs):

// 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 replace $cart_num_products = WC()->cart->cart_contents_count; with $cart_num_products = WC()->cart->get_cart_contents_count(); because the first form is deprecated since WooCommerce 3.0. global $woocommerce; is also not needed anymore, nowadays any access to global $woocommerce object should be done via WC() function.

发布评论

评论列表(0)

  1. 暂无评论