简体   繁体   中英

Create a redirect based on applied coupon in WooCommerce order received page

Here is the flow:

  1. Customer adds product to cart.
  2. Customer adds coupon "smile" at checkout.
  3. When customer places order the function will run before the Order Details page loads. Function will check for "smile" coupon and if it has been applied it redirects to a new page where they will be offered additional products for free. If not, then it continues on as normal.

I have been referencing two solutions I found through a Google search similar to parts of my problem. Individually I get them to work but together I cannot seem to get them to work correctly.

Here is my code:

add_action( 'woocommerce_thankyou' , 'sq_checkout_custom_redirect' );

function sq_checkout_custom_redirect($order_id) {

global $woocommerce;
$order = new WC_Order( $order_id );
$coupon_id = 'smile';
$applied_coupon = $woocommerce->cart->applied_coupons;
$url = 'https://site.mysite.org/score-you-win/';

if( $applied_coupon[0] === $coupon_id ) {
    echo "<script type=\"text/javascript\">window.location.replace('".$url."');</script>";
    } else {
    echo '<h3 style="font-size:200px; z-index:30000; color:#000 !important;">Coupon not applied</h3>';
    }
}

No matter what coupon I apply I get the message "Coupon not applied." and no redirect happens.

The two solutions that I am referencing are:

Find applied coupon_id in cart

Redirect with JS

This code runs successfully:

add_action( 'woocommerce_thankyou', function ($order_id){
$order = new WC_Order( $order_id );
$coupon_id = "smile";
$url = 'https://site.mysite.org/score-you-win/';

if ($order->status != 'failed') {
    echo "<script type=\"text/javascript\">window.location.replace('".$url."');</script>";
}
});

And this runs successfully:

function product_checkout_custom_content() {

global $woocommerce;
$coupon_id = 'smile';
$applied_coupon = $woocommerce->cart->applied_coupons;
if( $applied_coupon[0] === $coupon_id ) {
echo '<span style="font-size:200px; z-index:30000; color:#red !important;">We are happy you bought this product =)</span> ';
} else {
    echo '<h3 style="font-size:200px; z-index:30000; color:#000 !important;">Coupon not applied</h3>';
}
} 
add_action( 'woocommerce_thankyou' , 'sq_checkout_custom_redirect' );

Updated: In woocommerce "Order Received" page (thankyou), there is no more WC_Cart object available. Instead you need to target the WC_Order object this way:

add_action( 'woocommerce_thankyou', 'thankyou_custom_redirect', 20, 1 );
function thankyou_custom_redirect( $order_id ) {
    // Your settings below:
    $coupon_id = 'smile';
    $url       = 'https://site.mysite.org/score-you-win/';

    // Get an instance of the WC_order object
    $order = wc_get_order($order_id);
    $found = false;

    // Loop through the order coupon items
    foreach( $order->get_items('coupon') as $coupon_item ){
        if( $coupon_item->get_code() == strtolower($coupon_id) ){
            $found = true; // Coupon is found
            break; // We stop the loop
        }
    }

    if( $found )
        echo "<script type=\"text/javascript\">window.location.replace('".$url."');</script>";
    else
        echo '<h3 style="font-size:200px; z-index:30000; color:#000 !important;">Coupon not applied</h3>';
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM