Skip to content

Instantly share code, notes, and snippets.

@manojmohangit
Created March 2, 2018 05:40
Show Gist options
  • Save manojmohangit/bf758a1b053a66374a60dcced3377c61 to your computer and use it in GitHub Desktop.
Save manojmohangit/bf758a1b053a66374a60dcced3377c61 to your computer and use it in GitHub Desktop.
Create Virtual Coupon in Woocommerce to have custom discount on the cart
// add the below code to your theme's functions.php
/*'woocommerce_get_shop_coupon_data' is used to add virtual coupon*/
add_filter('woocommerce_get_shop_coupon_data', 'addVirtualCouponDiscount', 10, 2);
add_action('woocommerce_cart_calculate_fees', 'addCoupontoCart');
function addVirtualCouponDiscount($data, $coupon_code) {
if($coupon_code == "virtual_coupon") {
$coupon = array(
"discount_type"=>"percent",
"amount"=>"30",
"individual_use" => false
);
return $coupon;
}
}
function addCoupontoCart() {
$coupon_code = apply_filters('woocommerce_coupon_code', "virtual_coupon");
//code to create virtual coupon
$coupon = new WC_Coupon($coupon_code);
if(!WC()->cart->has_discount($coupon_code)) {
WC()->cart->add_discount($coupon_code);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment