Home > OS >  Redirect customer to recently acquired product in woocommerce
Redirect customer to recently acquired product in woocommerce

Time:12-07

I'm selling digital goods (videos) that are unlocked after you buy them on my WooCommerce shop.

What I'm trying to do is that after successful purchase completion, the customer is redirected to the recently acquired product. The shop is already configured for redirec them logged-in and knowing that they acquired the product.

There is no cart o quantity, so there is no chance you'll add more than one product.

How can this be possible to make? I want to skip the after checkout page and redirect the customer directly to product they just bought.

Thanks!

CodePudding user response:

There you go:

/***
 * Redirect to custom page when reaching thank you page
 */
add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_action' );
function woocommerce_thankyou_action( int $order_id ) {
    $order            = wc_get_order( $order_id );
    $first_order_item = $order->get_items()[0];
    $first_product_id = $first_order_item->get_product_id();

    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( get_permalink( $first_product_id ) );
        exit;
    }
}

In this example, I use the first order item as reference since you're only talking about one product and not multiple products.

This hook goes inside the functions.php file of your child theme.

CodePudding user response:

Why don't you just pay notice to user?

  • Related