[英]custom add to cart redirection for a defined product category in WooCommerce 3
On a WooCommerce website I would like to be redirect customers on checkout page just after a product is added to cart. 在WooCommerce网站上,我想在产品添加到购物车后立即在结帐页面重定向客户。 However we want it to be for that specific product category instead of all the add to cart buttons. 但是我们希望它适用于特定的产品类别,而不是所有添加到购物车按钮。
I have tried to look up past solutions but they either just crash my site when I update the functions.php
or they are outdated and don't work anymore. 我试图查找过去的解决方案,但他们要么在我更新functions.php
时崩溃我的网站,要么他们已经过时而且不再工作了。
I really appreciate any help. 我非常感谢任何帮助。
Updated: Add to cart redirection will not work with ajax add to cart on shop and archives pages. 更新:添加到购物车重定向将无法使用ajax添加到商店和档案页面上的购物车。
You will have to choose between that 2 options for shop and archives pages: 您必须在商店和档案页面的2个选项之间进行选择 :
This 2nd option seems to be the best: 第二个选项似乎是最好的:
// Replacing add to cart button link on shop and archives
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if( $product->is_type( 'variable-subscription' ) || $product->is_type( 'variable' ) ) return $button;
$button_text = __( 'View product', 'woocommerce' );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
THE CUSTOM CONDITIONAL REDIRECTION (For a defined product category): 定制条件重定向 (对于定义的产品类别):
Now, you can use a custom function hooked in woocommerce_add_to_cart_redirect
filter hook, that will redirect customer when a product is added to cart (for a defined product category ) : 现在,您可以使用挂钩在woocommerce_add_to_cart_redirect
过滤器挂钩中的自定义函数,该函数会在将产品添加到购物车时重定向客户(对于已定义的产品类别 ) :
add_filter( 'woocommerce_add_to_cart_redirect', 'add_to_cart_checkout_redirection', 99, 1 );
function add_to_cart_checkout_redirection( $url ) {
// HERE define your category
$category = 'clothing';
if ( ! isset( $_REQUEST['add-to-cart'] ) ) return $url;
// Get the product id when it's available (on add to cart click)
$product_id = absint( $_REQUEST['add-to-cart'] );
// Checkout redirection url only for your defined product category
if( has_term( $category, 'product_cat', $product_id ) ){
// Clear add to cart notice (with the cart link).
wc_clear_notices();
$url = wc_get_checkout_url();
}
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Code is tested on Woocommerce 3+ and works 代码在Woocommerce 3+上测试并且有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.