Say you operate a WooCommerce store where you sell products that are shipped in boxes but want customers to make up their own boxes with different products, by default WooCommerce will only allow you to sell products in the the quantities you set the product up with and the customer will be able to check out with any amount of items in the cart.
For instance if you operate a online wine store with WooCommerce and would like to sell your wine by the bottle but only want customers to checkout if they have quantities selected that would make up a whole box, the following tutorial is for you.
With the code below you can setup your products that each product is a single bottle of wine and then force the customer to add multiples of any 6 products to the cart before they would be able to checkout. If the customer for example adds only 5 bottles to the cart and then tries to checkout they will be presented with a message to order in multiples of 6.
You can even take it a step further and only allow the rule to apply to products with a certain shipping class, this will allow you to sell single bottles as well as cases without having the multiples rule apply to the case products.
To force the customer to add multiples of a certain quantity to the cart before being able to checkout add the following code to your theme’s functions.php file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// check that cart items quantities totals are in multiples of 6 | |
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' ); | |
function woocommerce_check_cart_quantities() { | |
$multiples = 6; | |
$total_products = 0; | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$total_products += $values['quantity']; | |
} | |
if ( ( $total_products % $multiples ) > 0 ) | |
wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' ); | |
} | |
// Limit cart items with a certain shipping class to be purchased in multiple only | |
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities_for_class' ); | |
function woocommerce_check_cart_quantities_for_class() { | |
$multiples = 6; | |
$class = 'bottle'; | |
$total_products = 0; | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$product = get_product( $values['product_id'] ); | |
if ( $product->get_shipping_class() == $class ) { | |
$total_products += $values['quantity']; | |
} | |
} | |
if ( ( $total_products % $multiples ) > 0 ) | |
wc_add_notice( sprintf( __('You need to purchase bottles in quantities of %s', 'woocommerce'), $multiples ), 'error' ); | |
} | |
?> |
Awesome! Got this link from Harry @woo.
Once woo confirms logged in user, the first part works great (woocommerce_check_cart_quantities)
But it won’t let me create a quantity of 12 only on those products with shipping class of ‘twelve’. Code below:
add_action (‘init’,’check_user_logged_in’);
function check_user_logged_in(){
if ( is_user_logged_in() ) {
// check that cart items quantities totals are in multiples of 12
add_action( ‘woocommerce_check_cart_items’, ‘woocommerce_check_cart_quantities’ );
function woocommerce_check_cart_quantities() {
global $woocommerce;
$multiples = 12;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$total_products += $values[‘quantity’];
}
if ( ( $total_products % $multiples ) > 0 )
$woocommerce->add_error( sprintf( __(‘You need to buy in quantities of %s products’, ‘woocommerce’), $multiples ) );
}
// Limit cart items with a certain shipping class to be purchased in multiple only
add_action( ‘woocommerce_check_cart_items’, ‘woocommerce_check_cart_quantities_for_class’ );
function woocommerce_check_cart_quantities_for_class() {
global $woocommerce;
$multiples = 12;
$class = ‘twelve’;
$total_products = 0;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$product = get_product( $values[‘product_id’] );
if ( $product->get_shipping_class() == $class ) {
$total_products += $values[‘quantity’];
}
}
if ( ( $total_products % $multiples ) > 0 )
$woocommerce->add_error( sprintf( __(‘You need to purchase bottles in quantities of %s’, ‘woocommerce’), $multiples ) );
}
}
}
Hi.
I changed your code to set the minimum price of the order. But in this way function get the price only after refresh the page. If I use $woocommerce->cart->get_cart_total(), the price is ‘897.00 $’ format. But I need ‘897.00’. And I dont know how I can convert It. All methods give me 0…
Can you help me? please?
add_action( ‘woocommerce_check_cart_items’, ‘woocommerce_check_cart_prices’ );
function woocommerce_check_cart_prices() {
global $woocommerce;
$min_price = 999;
$total_price =$woocommerce->cart->total();
$dif=$min_price-$total_price;
if ( $total_price add_error( sprintf( __(‘Until the minimum order %s $.’, ‘woocommerce’), $dif) );
}
}
Hi Alex, WC()->cart->total should give you the numeric total and not the formatted one like the function.
Hi,
Does this code work with Woocommerce 2.2?
Thank you,
Clare
Hi Gerhard, Do you have a new version of the multiples code for WooCommerce version 2.3.5?
hi, I used your code and it has always worked perfectly but since I updated woocommerce returns me an error in this line
$woocommerce->add_error( sprintf( __(‘La quantità delle bottiglie deve essere di %s o multipli di 6.’, ‘woocommerce’), $multiples ) );
}
do you know what’s wrong?
thank you
ari
Hi Ari, I updated the code to work with latest WC.
Hi,
Thank you so much for this tutorial! I am working on a project right now, a woocommerce website and my client needs the products to be sold in multiples of 3 or multiples of 4 (depending on the product). So we basically they have a product type that sells in packs of 4 and others that sell in packs of 6 and others that are sold at the quantity of one. How can I make this happen ? Thank you in advance. Hoping to get help soon.
Hi Gerhard
This was working perfectly, until I did an update of WooCommerce to the latest version 2.3.5.
Now the Cart shows nothing but a blank page until 6 quantities have been added, and then shows Cart Contents.
Any ideas / fixes? Thanks.
Hi Anthony, I updated the code to work with latest WC.
I can get this to work when the the product has a single shipping class but when I have a product variation and give the variations different shipping classes then it doesn’t appear to work. Any ideas?
awesome! you saved my day ^___^
For some (in my case) it was better to create a plugin, while updating templates could overwrite the functions.php file. Just create a directory named ‘6product’ and within that directory create a file named 6product.php and copy the code into the file. At the start of the file place within the php tags;
Place the map into the plugins directory inside the wp_content directory. Activate your plugin.
0 Pingbacks
Categories
Top Posts & Pages
Instagram
No Instagram images were found.
Get Email Notifications