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:
- if there is nothing in the cart when the audience visits the website, the coupon will not be applied.
- If there was something added and stayed there (because of a cookie) then the coupon code is applied perfectly.
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?