简体   繁体   English

如何在 Woocommerce 中将特定类别的产品页面重定向到购物车页面

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

相关问题 如何将购物车商品产品重定向到Woocommerce中的特定页面? - How can i redirect cart item product to specific page in Woocommerce? WooCommerce重定向到添加到购物车上的产品类别 - WooCommerce redirect to product category on add to cart 如何隐藏添加到购物车按钮特定角色和产品类别 Woocommerce - How to hide add to cart button specific role and product category Woocommerce WooCommerce如何在购物车页面上一次显示产品类别? - WooCommerce how to show product category once on cart page? 在购物车和结帐页面上显示 Woocommerce 产品类别 - Display Woocommerce Product Category on cart and checkout page 为产品类别替换 Woocommerce 单一产品页面上的“添加到购物车”按钮 - Replace Add to cart button on Woocommerce Single Product Pages for a product category 仅显示特定类别的WooCommerce产品页面 - Show WooCommerce product pages for only specific category 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 如果购物车包含特定的 Woocommerce 产品类别,则阻止添加到购物车 - Prevent add to cart if cart contains a specific Woocommerce product category 在Woocommerce产品类别归档页面上禁用“添加到购物车”按钮 - Disable add to cart button on Woocommerce product category archive pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM