14

I have a WooCommerce website and when customer add-to-cart a product, it is get redirected to checkout page, so cart page is not accessible.

I would like to apply coupon via URL (GET) on checkout page, with something like https://example.com/?coupon_code=highfive.

When customer click this URL then the coupon code is stored in browser sessions. Then if he add-to-cart any product then the coupon is applied into checkout page.

Is this possible?

double-beep
  • 5,031
  • 17
  • 33
  • 41
developerme
  • 1,845
  • 2
  • 15
  • 34
  • 10
    This question is absolutely **NOT too broad**… and can be done with few lines of code (5 minutes to be answered max). – LoicTheAztec Jan 12 '18 at 19:45

1 Answers1

25

Update 3: This can be done in a very simple way with the following 2 hooked functions:

  • The first one will catch the coupon code in the Url and will set it in WC_Sessions.
  • The second one will apply the coupon code from session in checkout page.

Here is this code:

add_action('init', 'get_custom_coupon_code_to_session');
function get_custom_coupon_code_to_session(){
    if( isset($_GET['coupon_code']) ){
        // Ensure that customer session is started
        if( isset(WC()->session) && ! WC()->session->has_session() )
            WC()->session->set_customer_session_cookie(true);
            
        // Check and register coupon code in a custom session variable
        $coupon_code = WC()->session->get('coupon_code');
        if(empty($coupon_code)){
            $coupon_code = esc_attr( $_GET['coupon_code'] );
            WC()->session->set( 'coupon_code', $coupon_code ); // Set the coupon code in session
        }
    }
}

add_action( 'woocommerce_before_checkout_form', 'add_discout_to_checkout', 10, 0 );
function add_discout_to_checkout( ) {
    // Set coupon code
    $coupon_code = WC()->session->get('coupon_code');
    if ( ! empty( $coupon_code ) && ! WC()->cart->has_discount( $coupon_code ) ){
        WC()->cart->add_discount( $coupon_code ); // apply the coupon discount
        WC()->session->__unset('coupon_code'); // remove coupon code from session
    }
}

Code goes in function.php file of the active child theme (or active theme). Tested and works

Inspired from this answer code, Lukasz Wiktor has published a plugin: Woo Coupon URL

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • How to remove session value after the coupon applied successfully?? – developerme Jan 12 '18 at 12:17
  • and also when trying this code i getting this error message "Sorry, this coupon is only valid for an initial payment and the cart does not require an initial payment." – developerme Jan 12 '18 at 12:24
  • @developerme Sorry you are right… **apologies…** I have updated my code try it again. Thanks – LoicTheAztec Jan 12 '18 at 13:08
  • Yes its working for me Thank You But when i removing coupon apply filed on the checkout page its not working...form could you please help me for that?? – developerme Jan 12 '18 at 13:21
  • 3
    Inspired by your answer I created a WooCommerce plugin https://wordpress.org/plugins/woo-coupon-url/ – Lukasz Wiktor Mar 16 '19 at 19:22
  • @LoicTheAztec in the source code https://plugins.trac.wordpress.org/browser/woo-coupon-url/trunk/public/class-woo-coupon-url-public.php#L106 – Lukasz Wiktor Mar 16 '19 at 23:14
  • @LukaszWiktor Nice thanks… I have mention your plugin at the end of this thread. – LoicTheAztec Mar 16 '19 at 23:51
  • @LoicTheAztec This is exactly what I was looking for. Thanks a million!!! – Devner Oct 31 '19 at 11:42
  • @LoicTheAztec - Thanks for this. Should it still work on latest version of WooCommerce? I've tried it out, and had no effect. Any thoughts? – inspirednz Aug 25 '22 at 08:56