【问题标题】:Woocommerce: How to show Product Attribute name on title when in a category page and "filtering" products via '?pa_attribute=' on address barWoocommerce:如何在类别页面中的标题上显示产品属性名称并通过地址栏上的“?pa_attribute =”“过滤”产品
【发布时间】:2018-10-19 13:04:39
【问题描述】:

所以我有一个产品属性(例如:颜色),我可以通过在地址中使用例如 ?pa_color=red 来仅显示具有该属性的类别中的产品吧,所以如果我去 '*www.mysite.com/product-category/t-shirts/?pa_color=red*' 我只会有红色作为属性的 T 恤。这行得通。
问题是,当我进行“过滤”时,页面标题(在产品列表上方)仍然只显示 “T 恤”,它没有指定它只显示红色 T 恤和它可能会使客户感到困惑。我需要的是让它显示类似“T-shirts (Color:Red)”的东西。
我知道我可以使用侧边栏小部件来过滤属性,然后小部件将显示正在过滤的内容,但在这种特定情况下是一个非常小的设计,没有侧边栏或任何过滤,我需要它显示在标题中。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
    add_filter( 'woocommerce_page_title', 'custom_woocommerce_page_title', 15, 1 );
    
    function custom_woocommerce_page_title( $page_title ) {
        if ( is_archive() ) {
            $exists_attr = false;
    
            foreach ( $_GET as $index => $value ) {
                if ( substr( $index, 0, 3 ) === 'pa_' ) {
                    $attr_id = wc_attribute_taxonomy_id_by_name( $index );
    
                    if ( $attr_id === 0 ) {
                        continue;
                    }
    
                    if ( ! $exists_attr ) {
                        $exists_attr = true;
    
                        $page_title .= ' (';
                    } else {
                        $page_title .= ', ';
                    }
    
                    $page_title .= wc_attribute_label( $index ) . ': ' . esc_html( $value );
                }
            }
    
            if ( $exists_attr ) {
                $page_title .= ')';
            }
        }
    
        return $page_title;
    }
    

    【讨论】:

    • 非常好。谢谢!我刚刚更改了以下行,因此我可以获得属性“name”而不是“slug”: FROM:$page_title .= wc_attribute_label( $index ) . ': ' . esc_html( $value ); TO:$term = get_term_by( 'slug', esc_html( $value ), $index ); $page_title .= wc_attribute_label( $index ) . ': ' . $term->name; 再次感谢。
    猜你喜欢
    • 2019-04-29
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多