Josh
Josh

Reputation: 1

Pass query parameter into WooCommerce checkout upon redirect

Feel like I must be missing something pretty obvious here – looking for any help you can provide.

Helpful Background:

Scenario:

My shop page has a custom query parameter in the url (e.g. ?model=macbook). I'm hoping to pass that query parameter & value on to the redirected checkout url as well. So that it would read – example.com/checkout/?model=macbook.

I am currently using the following code in my child theme's functions.php. This handles the redirect to checkout add-to-cart action no problem but my php noobie-ness cannot figure out how to properly use the add_query_arg to complete the checkout url.

    // Checkout Redirect
    add_filter('woocommerce_add_to_cart_redirect', 'this_add_to_cart_redirect');
    function this_add_to_cart_redirect() {
     global $woocommerce;
     $checkout_url = wc_get_checkout_url();
     return $checkout_url;
    }

Any and all help appreciated.

Upvotes: 0

Views: 1620

Answers (1)

Mr. Jo
Mr. Jo

Reputation: 5261

There you go:

// Checkout Redirect
add_filter( 'woocommerce_add_to_cart_redirect', 'this_add_to_cart_redirect' );
function this_add_to_cart_redirect() {
    global $woocommerce;

    $checkout_url = add_query_arg( [
        'model' => 'macbook',
    ], wc_get_checkout_url() );

    return $checkout_url;
}

Upvotes: 1

Related Questions