I have a plugin that sends an advocates referral coupon code to e-mails that they enter. When the audience receives this email I'd like to create a flow where they can click on 'SHOP NOW' in the e-mail and the coupon will be automatically added.
As of now, for the link under the 'SHOP NOW' button I've entered the following:
websitename.biz/cart__trashed?code=DISCOUNTCODE
To handle $code
I've put this in my functions.php file:
add_action('woocommerce_before_cart', 'discount');
function discount( ) {
global $woocommerce;
$code= $_GET["code"];
if(!empty($code)){
if($woocommerce->cart->add_discount($code)){
echo '<div class="woocommerce_message"><strong>Applied coupon!</strong></div>';
}
}
}
The problem I'm facing here is:
I believe its because the cart is empty, the code does not work.
Just want the code to be applied when the audience clicks the link.
How can I make this working?
The 'Coupon URL' in WooCommerce is automatically generated for you, so you don't need to enter anything here. If you want to use something other than your coupon code itself at the end of the URL, then you can type it in the 'Code URL Override' box.
General section. By default, WooCommerce comes with three discount types: Percentage discounts apply a percentage discount to the entire cart. Fixed cart discounts apply a fixed amount discount to the entire cart. Fixed product discounts apply a fixed amount discount to selected products only.
The correct way to do it should be to:
You can set any existing coupon code from any Url (like shop page, other archives pages, products pages, my account pages, or any existing pages) adding to this existing url:
?code=DISCOUNTCODE
at the end (whereDISCOUNTCODE
is your coupon code name).
Here is the code:
// Set coupon code as custom data in cart session
add_action('wp_loaded', 'add_coupon_code_to_cart_session');
function add_coupon_code_to_cart_session() {
// Exit if no code in URL or if the coupon code is already set cart session
if( empty( $_GET["code"] ) || WC()->session->get( 'custom_discount' ) ) return;
if( ! WC()->session->get( 'custom_discount' ) ) {
$coupon_code = esc_attr($_GET["code"]);
WC()->session->set( 'custom_discount', $coupon_code );
// If there is an existing non empty cart active session we apply the coupon
if( ! WC()->cart->is_empty() ){
WC()->cart->add_discount( $coupon_code );
}
}
}
// Add coupon code when a product is added to cart once
add_action('woocommerce_add_to_cart', 'add_coupon_code_to_cart', 10, 6 );
function add_coupon_code_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
$coupon_code = WC()->session->get( 'custom_discount' );
$applied_coupons = WC()->session->get('applied_coupons');
if( empty($coupon_code) || in_array( $coupon_code, $applied_coupons ) ) return;
WC()->cart->add_discount( $coupon_code );
}
// Remove coupon code when user empty his cart
add_action('woocommerce_cart_item_removed', 'check_coupon_code_cart_items_removed', 10, 6 );
function check_coupon_code_cart_items_removed( $cart_item_key, $cart ){
$coupon_code = WC()->session->get( 'custom_discount' );
if( $cart->has_discount( $coupon_code ) && $cart->is_empty() );
$cart->remove_coupon( $coupon_code );
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
This is tested and works
use below code to make the coupan valid in any situation
add_filter('woocommerce_coupon_is_valid','coupon_always_valid',10,1);
function coupon_always_valid($valid){
$valid = true;
return $valid ;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With