最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Woocommerce add_to_cart() then wp_safe_redirect() fails

programmeradmin1浏览0评论

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();
发布评论

评论列表(0)

  1. 暂无评论