In Woocommerce, I would like to limit the number of coupons allowed at checkout so customer can only use 2 coupons in total, but no more.
I plan to have the coupons usable in conjunction so they can combine any 2 but I do not want them to be able to use an unlimited number of coupons together.
How to limit the number of applied coupons in Woocommerce?
To limit the number of allowed applied coupons, you will use the following that will output an error notice avoiding coupous to be applied when the limit is reached:
add_action( 'woocommerce_applied_coupon', 'custom_applied_coupon', 10, 1 );
function custom_applied_coupon( $coupon_code ){
$applied_coupons = WC()->cart->get_applied_coupons();
$coupons_allowed_max = 2; // <=== 2 coupons allowed max
if( sizeof($applied_coupons) > $coupons_allowed_max ) {
WC()->cart->remove_coupon( $coupon_code );
wc_clear_notices();
wc_print_notice(__("You can only have two applied coupons max…", "woocommerce"), 'error');
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related
I am using Woocommerce set minimum order for a specific user role answer code and it works like a charm!
Though I would like to only have a minimum order amount if the product(s) placed in the shopping cart are not in stock (backorder). If the product(s) in the shopping cart are in stock, there should be no minimum order amount. Could someone help me out?
To make the code work only when there is backordered items, you need to include in the code a check for backordered items as follows:
add_action( 'woocommerce_check_cart_items', 'set_min_total_per_user_role' );
function set_min_total_per_user_role() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Set minimum cart total (by user role)
$minimum_cart_total = current_user_can('company') ? 250 : 100;
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
$has_backordered_items = false;
// Check for backordered cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$has_backordered_items = true;
break; // stop the loop
}
}
// Add an error notice is cart total is less than the minimum required
if( $has_backordered_items && $total <= $minimum_cart_total ) {
// Display our error message
wc_add_notice( sprintf( '<strong>Dear customer, minimum order of %s is required to make a purchase on your site.</strong> <br>
Your actual cart amount is: %s',
wc_price($minimum_cart_total),
wc_price($total)
), 'error' );
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.
Based on: Woocommerce set minimum order for a specific user role
I have one little problem that dont know how to fix myself. I want to use this logic into my Woocommerce store just for one product.
I have use link like this to autmatically apply coupon code and add to cart:
https://testsite.com/checkout/?add-to-cart=Product_ID&quantity=1&coupon=Coupon_Code
and this seems to work, when quantity is 1. But i want discount (30%) that is automatically placed when click on direct link from before, to make dynamic, for ex:
Increase quantity to 2 in cart page, and coupon automatically to calculate 2 x 30$ = 60$ discount,
buy 3 from same product, and calculate 3 X 30$ = 90$ coupon discount
and so on..
I searched, and found this usefull thread, but there situation is little different then mine.
How can I make to have a specific coupon? Some advice or start point. Thanks
This is possible with that 2 steps:
1) Add a unique coupon with:
In General settings > Type = Fixed Product discount
In General settings > Amount = 30
In Usage restrictions > Products ==> set your desired product(s)
2) Add this code (where you will set your coupon code in the function (in lowercase)):
add_filter( 'woocommerce_coupon_get_discount_amount', 'custom_coupon_get_discount_amount', 10, 5 );
function custom_coupon_get_discount_amount( $rounded_discount, $discounting_amount, $cart_item, $single, $coupon ){
## ---- Your settings ---- ##
// Related coupons codes to be defined in this array (you can set many)
$coupon_codes = array('30perqty');
## ------ The code ------- ##
if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) && $cart_item['quantity'] > 1 ) {
if( in_array( $cart_item['product_id'], $coupon->get_product_ids() ) ){
$discount = (float) $coupon->get_amount() * (int) $cart_item['quantity'];
$round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
}
}
return $rounded_discount;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
In woocommerce, I am trying to find a way to only allow a product to be added a to cart only when a specific cart total amount is reached.
Example: We want to sell a bumper sticker for $1, but only if a user already has $25 worth of other products already in the cart. This is similar to Amazon's "add on" feature. However I can't find a similar WooCommerce plugin or function.
I have tried yet some code without success… Any help will be appreciated.
Can be done with a custom function hooked in woocommerce_add_to_cart_validation filter hook, where you will define:
a product Id (or multiple product Ids).
the threshold cart amount to be reached.
It will avoid for those defined products to be added to cart (displaying a custom notice) until that specific cart amount is reached.
The code:
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 );
function wc_add_on_feature( $passed, $product_id, $quantity ) {
// HERE define one or many products IDs in this array
$products_ids = array( 37, 27 );
// HERE define the minimal cart amount that need to be reached
$amount_threshold = 25;
// Total amount of items in the cart after discounts
$cart_amount = WC()->cart->get_cart_contents_total();
// The condition
if( $cart_amount < $amount_threshold && in_array( $product_id, $products_ids ) ){
$passed = false;
$text_notice = __( "Cart amount need to be up to $25 in order to add this product", "woocommerce" );
wc_add_notice( $text_notice, 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
Is there a way or filter to disable selective payment methods if cart quantity increase more than "X number of items" example "15"?
I know we can limit max number of quantity before adding to cart but I want to disable some payment methods only.
Thanks
You can use a custom function hooked in woocommerce_available_payment_gateways filter hook. You will have to set inside it your quantity limit and your payment methods slugs.
Here is that code:
add_filter('woocommerce_available_payment_gateways', 'unsetting_payment_gateway', 10, 1);
function unsetting_payment_gateway( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// HERE Define the limit of quantity item
$qty_limit = 15;
$limit_reached = false;
// Iterating through each items in cart
foreach(WC()->cart->get_cart() as $cart_item){
if($cart_item['quantity'] > $qty_limit ){
$limit_reached = true;
break;
}
}
if($limit_reached){
// HERE set the slug of your payment method
unset($available_gateways['cod']);
unset($available_gateways['bacs']);
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works on WooCommerce version 2.6 and 3+.
You can specify in the payment condition that if the basket number exceeds your chosen amount (e.g., 15), then the payment method will not be displayed in the auction.
I am running into issues with the cart total only displaying 0
Essentially what I am trying to do is only accept a deposit total of a certain amount after all cart items have been added to the carts subtotal.
So for example if the customer adds $100 worth of items, they would only pay $10 initially or (10%) of the subtotal as the total value.
I took the code from here: Change total and tax_total Woocommerce and customize it this way:
add_action('woocommerce_cart_total', 'calculate_totals', 10, 1);
function calculate_totals($wc_price){
$new_total = ($wc_price*0.10);
return wc_price($new_total);
}
But the total amount shows 0.00 when that code is enabled. If removed the code, I get the standard total.
I also could not find on the woocommerce site where the full api is listed, only generic articles related to how to create a plugin.
Any help or a point in the right direction would be great.
This does not answer this question. Loic's does. This is another way of doing it to show a line item of 10% off:
function prefix_add_discount_line( $cart ) {
$discount = $cart->subtotal * 0.1;
$cart->add_fee( __( 'Down Payment', 'yourtext-domain' ) , -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
Since Woocommerce 3.2+
it does not work anymore with the new Class WC_Cart_Totals ...
New answer: Change Cart total using Hooks in Woocommerce 3.2+
First woocommerce_cart_total hook is a filter hook, not an action hook. Also as wc_price argument in woocommerce_cart_total is the formatted price, you will not be able to increase it by 10%. That's why it returns zero.
Before Woocommerce v3.2 it works as some WC_Cart properties can be accessed directly
You should better use a custom function hooked in woocommerce_calculate_totals action hook this way:
// Tested and works for WooCommerce versions 2.6.x, 3.0.x and 3.1.x
add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( !WC()->cart->is_empty() ):
## Displayed subtotal (+10%)
// $cart_object->subtotal *= 1.1;
## Displayed TOTAL (+10%)
// $cart_object->total *= 1.1;
## Displayed TOTAL CART CONTENT (+10%)
$cart_object->cart_contents_total *= 1.1;
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Is also possible to use WC_cart add_fee() method in this hook, or use it separately like in Cristina answer.