admin管理员组

文章数量:1327102

A user clicks a link to select the type of product they want so I do some work to compute the products to add using code like this:

WC()->cart->add_to_cart( 6842,1 );

then i try to go directly to checkout:

wp_safe_redirect( wc_get_checkout_url() );

But it does not add the product to the cart. But if I skip the redirect it works, but doesn't go where I want.

What am I doing wrong?

A user clicks a link to select the type of product they want so I do some work to compute the products to add using code like this:

WC()->cart->add_to_cart( 6842,1 );

then i try to go directly to checkout:

wp_safe_redirect( wc_get_checkout_url() );

But it does not add the product to the cart. But if I skip the redirect it works, but doesn't go where I want.

What am I doing wrong?

Share Improve this question edited Aug 6, 2020 at 13:23 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Aug 6, 2020 at 2:43 Ryan ScottRyan Scott 1
Add a comment  | 

1 Answer 1

Reset to default 0

You need to save the cart contents before redirect

WC()->session->save_data();

本文标签: Woocommerce addtocart() then wpsaferedirect() fails