Last active
December 3, 2018 11:24
-
-
Save Adam-Mould/74649594442a3b0112c2134a22061bd5 to your computer and use it in GitHub Desktop.
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
function apply_global_coupon() { | |
global $woocommerce; | |
$all_coupons = get_all_store_coupons(); | |
$coupon_code = 'Global'; | |
if ( in_array( $coupon_code, $all_coupons) ) { | |
if ( WC()->cart->has_discount( $coupon_code ) ) return; | |
WC()->cart->add_discount( $coupon_code ); | |
wc_print_notices(); | |
} | |
} | |
add_action( 'woocommerce_before_cart', 'apply_global_coupon' ); | |
function get_all_store_coupons() { | |
$args = array( | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'asc', | |
'post_type' => 'shop_coupon', | |
'post_status' => 'publish', | |
); | |
$coupons = get_posts( $args ); | |
$coupon_names = array(); | |
foreach ( $coupons as $coupon ) { | |
// Get the name for each coupon post | |
$coupon_name = $coupon->post_title; | |
array_push( $coupon_names, $coupon_name ); | |
} | |
return $coupon_names; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment