[英]How to make a redirection to checkout on a custom add to cart button in WooCommerce
I'm using WooCommerce and added the following code to functions.php:我正在使用 WooCommerce 并将以下代码添加到functions.php:
add_filter( 'woocommerce_product_add_to_cart_url', 'custom_fix_for_individual_products', 10, 2 );
function custom_fix_for_individual_products( $add_to_cart_url, $product ){
$add_to_cart_url = wc_get_checkout_url();
return $add_to_cart_url;
}
What I would expect is that when a user adds a product to their basket: this code will redirect the user to the checkout page without actually adding the product to the basket.我期望的是,当用户将产品添加到他们的购物篮时:此代码会将用户重定向到结帐页面,而不会实际将产品添加到购物篮中。 However, it still adds the product to the basket.但是,它仍将产品添加到购物篮中。 What is wrong with this code?这段代码有什么问题?
Background: I'm using Uncaught Error: Call to a member function generate_cart_id() on null answer code but it doesn't work correctly for the above line, which is why I'm now working with only that line.背景:我正在使用Uncaught Error: Call to a member function generate_cart_id() on null应答代码,但对于上述行,它不能正常工作,这就是我现在只使用该行的原因。 There's no error in the server logs.服务器日志中没有错误。
Based on your comment, to get a redirection to checkout page for a custom add to cart button, you could change the link to something like https://example.com/checkout/?add-to-cart=99
where 99
is the product Id that you want to add to cart.根据您的评论,要重定向到自定义添加到购物车按钮的结帐页面,您可以将链接更改为https://example.com/checkout/?add-to-cart=99
其中99
是您要添加到购物车的产品 ID。
So the code for your custom add to cart button link will be like (where 99
is the product Id) :因此,您的自定义添加到购物车按钮链接的代码将类似于(其中99
是产品 ID) :
$product_id = 99;
$url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.