[英]How to Redirect specific category product pages to cart page in Woocommerce
i want to redirect users when they click single page product directly to cart without going to product description page for only one category products.我想在用户单击单页产品时直接将其重定向到购物车,而无需仅转到一个类别产品的产品描述页面。
i used this code:我使用了这个代码:
add_action( 'wp', 'ts_redirect_product_pages', 99 );
function ts_redirect_product_pages() {
if ( is_product() ) {
wp_safe_redirect( home_url('/cart/'));
exit;
}
}
it's redirecting to the cart page but all the other product categories also redirecting i only want one product category to be redirected, please guide me how to do this它重定向到购物车页面,但所有其他产品类别也重定向我只希望重定向一个产品类别,请指导我如何执行此操作
Better to use template_redirect
hook.最好使用template_redirect
钩子。 To target specific product category(ies), you can use WordPress has_term()
conditional function.要定位特定的产品类别,您可以使用 WordPress has_term()
条件函数。 For cart url, is better to use WooCommerce function wc_get_cart_url()
...对于购物车 url,最好使用 WooCommerce 函数wc_get_cart_url()
...
So in the code below define your product category(ies) term(s) name(s), slug(s) or Id(s):因此,在下面的代码中定义您的产品类别术语名称、slug(s) 或 Id(s):
add_action( 'template_redirect', 'ts_redirect_product_pages' );
function ts_redirect_product_pages() {
$categories = array('my-category-1', 'my-category-2');
if ( is_product() && has_term( $categories, 'product_cat' ) ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
Code goes in functions.php file of the active child theme (or active theme) .代码位于活动子主题(或活动主题)的functions.php 文件中。 It should work.它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.