Skip to content

Instantly share code, notes, and snippets.

@Adam-Mould
Last active December 3, 2018 11:24
Show Gist options
  • Save Adam-Mould/74649594442a3b0112c2134a22061bd5 to your computer and use it in GitHub Desktop.
Save Adam-Mould/74649594442a3b0112c2134a22061bd5 to your computer and use it in GitHub Desktop.
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