简体   繁体   English

如何在 WooCommerce 中的自定义添加到购物车按钮上重定向到结帐

[英]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.

相关问题 WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout Woocommerce添加到购物车按钮重定向到结帐 - Woocommerce add to cart button redirect to checkout 从woocommerce删除添加到购物车按钮,查看购物车,结帐页面 - Removing Add to cart button, View cart, Checkout pages from woocommerce 如何在自定义页面(不是商店/产品页面)上添加到购物车按钮 Woocommerce - How to make add to cart button on custom page (not shop/product page) Woocommerce 将WooCommerce上的添加到购物车按钮替换为自定义按钮 - Replace add to cart button with a custom button on WooCommerce WooCommerce 3中定义的产品类别的自定义添加到购物车重定向 - custom add to cart redirection for a defined product category in WooCommerce 3 WooCommerce 将多个产品的自定义重定向添加到购物车 - WooCommerce Add to Cart custom redirection for multiple products Variations WooCommerce 添加到购物车针对特定产品 ID 的自定义重定向 - WooCommerce Add to Cart custom redirection for specific product IDs 如何用购物车图标和购物车图标以及自定义文本替换Woocommerce的“添加到购物车”按钮? - How to replace Woocommerce “Add to Cart” Button with a Cart Icon and Cart Icon along with custom text? 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM