Created
March 2, 2018 05:40
-
-
Save manojmohangit/bf758a1b053a66374a60dcced3377c61 to your computer and use it in GitHub Desktop.
Create Virtual Coupon in Woocommerce to have custom discount on the cart
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
// 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