Created
April 13, 2015 23:41
-
-
Save michaelgiles/90ae5277c3f470b304ce to your computer and use it in GitHub Desktop.
Woocommerce Apply Coupon Code Automatically
This file contains hidden or 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
/* 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment