【问题标题】:Woocommerce: How to automatically redirect to the single product if there is only one product on a category page?Woocommerce:如果类别页面上只有一个产品,如何自动重定向到单个产品?
【发布时间】:2017-09-29 03:23:10
【问题描述】:

当类别中只有一个产品时,我试图跳过类别存档页面并直接进入单个产品页面。

这是我到目前为止的代码,(不工作):

add_action( 'template_redirect', 'woo_redirect_single_product_cat', 10 );

function woo_redirect_single_product_cat() {

global $wp_query, $wp;;

if  ( is_post_type_archive( 'product' ) && 1 === $wp_query->found_posts  ) {

    $product = wc_get_product($wp_query->post->ID);

       if ( $product && $product->is_visible() ) {
            wp_safe_redirect( get_permalink( $product->id ), 302 );
            exit;

       }
}
}

此代码仿照 Woocommerce 中的单一搜索重定向:

https://github.com/woothemes/woocommerce/blob/fce8dc0868439474c24f7317af50ce7627f0d1c1/includes/wc-template-functions.php#L43

谢谢!

【问题讨论】:

    标签: redirect woocommerce


    【解决方案1】:

    我想通了,但不是专门针对 Woocommerce 的:(编辑:现在是。)

    /* Redirect if there is only one product in the category or tag, or anywhere... */
    
    function redirect_to_single_post(){
    global $wp_query;
    if( (is_product_category() || is_product_tag()) && $wp_query->post_count == 1 )//Woo single only
    //if( is_archive() && $wp_query->post_count == 1 )//redirect all single posts 
    {
        the_post();
        $post_url = get_permalink();
        wp_safe_redirect($post_url , 302 );
    exit;
    }
    } 
    add_action('template_redirect', 'redirect_to_single_post');
    

    在某种程度上,这甚至更好,因为它也重定向单个标签。

    【讨论】:

    • 专门对 WooCommerece 使用此条件,is_product_category() 用于产品类别,is_product_tag()用于产品标签:if( (is_product_category() || is_product_tag()) && $wp_query->post_count == 1 ){…
    • 谢谢,这行得通!编辑答案以包含您的条件。
    • 这段代码在 WooCommerce 4.4 上对我很有效,谢谢
    • 谢谢,它在 Woocommerce 5.5.2 上仍然适用于我!
    【解决方案2】:

    将此代码添加到您的functions.php 中即可。!

    add_action( 'template_redirect', 'woocom_redirect_if_single_product_in_category', 10 );
    function woocom_redirect_if_single_product_in_category() {
        if ( is_product_category() ){
            $cat_name   =   sanitize_title( woocommerce_page_title( false ) );
            $args   =   array(
                    "post_type"         => "product",
                    "posts_per_page"    =>  -1,
                    "product_cat"       => $cat_name
            );
            $posts  = new WP_Query( $args );
            if( count( $posts->posts ) == 1 ){
                $one_post_ID    =   $posts->posts;
                $ID =   $one_post_ID[0]->ID;
                wp_redirect( get_permalink( $ID ) );
            }
        }
    }
    

    【讨论】:

    • 嗨,您的代码将所有类别页面重定向到单个产品页面。
    • @AJD 已更新。!请立即检查!
    【解决方案3】:

    我尝试了这个修复,它奏效了!只有1个打嗝。我的其余页面显示左侧边栏,但是一旦我添加了这个:

    // WooCommerce Fix - Redirects to page if product is equal to one
    add_action( 'template_redirect',
    'woocom_redirect_if_single_product_in_category', 10 ); function
    woocom_redirect_if_single_product_in_category() {
        if ( is_product_category() ){
            $cat_name   =   sanitize_title( woocommerce_page_title( false ) );
            $args   =   array(
                    "post_type"         => "product",
                    "posts_per_page"    =>  -1,
                    "product_cat"       => $cat_name
            );
            $posts  = new WP_Query( $args );
            if( count( $posts->posts ) == 1 ){
                $one_post_ID    =   $posts->posts;
                $ID =   $one_post_ID[0]->ID;
                wp_redirect( get_permalink( $ID ) );
            }
        } }
    

    它使我的模板显示了一个被推入页脚的损坏的侧边栏,而其他单个产品页面则正确显示。脚本中可能有一个调用错误配置了侧边栏位置,它可能调用了默认的单个产品页面模板。如果有人对此有解决方案,将不胜感激,因为它有点超出我的能力。

    【讨论】:

    • 我建议尝试接受的答案版本。我不知道您使用的那个是否正常工作。
    • 是的,我试过了,它起作用了,问题是一个未封闭的 php 标签包裹了整个函数文件和产品本身的一个额外的 div。 _( ' _ ' ) _/ 第一个答案+1。
    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多