In Woocommerce I’m trying to find a way to apply a 10% discount to an entire customer’s order if the weight in the cart is over 100 lbs. I’m partway to achieving this. For the next step, I’m looking for a way to programmatically apply a coupon code via action/hook through functions.php.
It appears that I can use the function woocommerce_ajax_apply_coupon to do this ( http://docs.woothemes.com/wc-apidocs/function-woocommerce_ajax_apply_coupon.html ) but I am unsure of how to use it.
So far I’ve modified cart.php to get the total weight of all the products in the cart, I’ve created a coupon that applies the discount (if entered manually) and I’ve added some code to functions.php to check the weight and display a message to the user.
EDIT: Partial code removed, completed code included in the solution below.
Thanks for the guidance Freney. Here’s the working end result which successfully applies the discount coupon when the condition is met and also removes it when it’s no longer met:
/* Mod: 10% Discount for weight greater than 100 lbs
Works with code added to child theme: woocommerce/cart/cart.php lines 13 - 14: which gets $total_weight of cart:
global $total_weight;
$total_weight = $woocommerce->cart->cart_contents_weight;
*/
add_action('woocommerce_before_cart_table', 'discount_when_weight_greater_than_100');
function discount_when_weight_greater_than_100( ) {
global $woocommerce;
global $total_weight;
if( $total_weight > 100 ) {
$coupon_code = '999';
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>Your order is over 100 lbs so a 10% Discount has been Applied!</strong> Your total order weight is <strong>' . $total_weight . '</strong> lbs.</div>';
}
}
/* Mod: Remove 10% Discount for weight less than or equal to 100 lbs */
add_action('woocommerce_before_cart_table', 'remove_coupon_if_weight_100_or_less');
function remove_coupon_if_weight_100_or_less( ) {
global $woocommerce;
global $total_weight;
if( $total_weight <= 100 ) {
$coupon_code = '999';
$woocommerce->cart->get_applied_coupons();
if (!$woocommerce->cart->remove_coupons( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
$woocommerce->cart->calculate_totals();
}
}
First, create a discount coupon (via http://docs.woothemes.com/document/create-a-coupon-programatically/):
Then apply that coupon to your order:
That last function returns a BOOL value: TRUE if the discount was successful, FALSE if it fails for any one of a variety of reasons.
I used this solution, but it contains a bug as the OP wrote it. If the user skips previewing the cart and goes straight to the checkout form, it does not apply the coupon. Here was my solution.
The following compact code in one hooked function will add a discount (coupon) based on cart total weight displaying a custom message. If customer changes cart items the code will check cart items removing the discount (coupon) if the cart weight is under the defined weight.
The code:
Code goes in function.php file of your active child theme (or active theme). Tested and works.