【问题标题】:Show attribute values on Woocommerce category archive pages在 Woocommerce 类别存档页面上显示属性值
【发布时间】:2017-11-21 16:49:08
【问题描述】:

在 Wordpress + Woocommerce 网站中,我有一个带有一些自定义链接的菜单,这些链接使用高级查询分类法调用特定的商店页面 URL。

这些是我用于这些自定义菜单条目的示例链接:

https://example.com/product-category/cat1/?pa_attrib1=value1&pa_attrib2=value3
https://example.com/product-category/cat2/?pa_attrib1=value2&pa_attrib3=value4`
https://example.com/product-category/cat2/?pa_attrib1=value5&pa_attrib4=value5,value7,value8,value9

通过这种方式,我可以为各个商店部门配置“直接”条目。

现在我需要在商店和存档页面中以类似的方式显示在 url 中设置的属性的名称和值

类别名称 - 属性 1 名称:value1 - 属性 2 名称:value2, ...

我想我需要在functions.php中使用钩子woocommerce_archive_description,但我不知道如何回忆和显示我需要的值

add_action ( 'woocommerce_archive_description', 'show_term_description', 20 );
function show_term_description() {
   echo term_description( $term_id, $taxonomy ); // print Category Name
   // ...
   // ...
}

谢谢

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    这可以通过在挂钩函数中插入以下注释代码来完成:

    add_action ( 'woocommerce_archive_description', 'show_term_description', 20 );
    function show_term_description() {
        // Only for product category archive pages
        if( ! is_product_category()) return;
    
        global $wp;
    
        // Get the requested Url category/subcategories
        $request = $wp->request;
        // Set them in an array
        $request = explode( '/', $request );
    
        // Remove 'product-category' from the array
        if( 'product-category' == $request[0] )
            unset($request[0]);
    
        // Starting html formatting
        $html = '<p><strong>';
    
        // The main category and sub-categories names
        foreach( $request as $category ){
            // Get the category name
            $category_term = get_term_by( 'slug', $category, 'product_cat' );
            // Set category and subcategories in an array
            $categories_name[] = $category_term->name;
        }
        // Formatting the category/subcategories in an html string
        $html .= implode( '</strong> - <strong>', $categories_name );
    
        // The product attributes names and related values
        if( ! empty($_GET) ){
            $html .= '</strong> - <strong>'; // Formatting html
            $count = 0; // Initializing the counter
            $attr_count = count($_GET); // get the length of the array
            // Loop through the attribute/values from the $_GET
            foreach($_GET as $taxonomy => $values ){
                $count++;
                $terms_names = array(); // Initializing
    
                // If the taxonomy doesn't exist,
                if( ! taxonomy_exists( $taxonomy ) ){
                    continue; // We go to next attribute
                }
    
                // Set the attribute terms in an array
                $term_slugs = explode( ',', $values );
                // Loop through the attribute term
                foreach( $term_slugs as $term_slug ){
                    // Get the WP_Term object
                    $term = get_term_by( 'slug', $term_slug, $taxonomy );
                    if( ! term_exists( $term->term_id, $taxonomy ) )
                    continue; // We go to next term
    
                    // Set The term name in an array
                    $terms_names[] = $term->name;
                }
                // If there is no corresponding terms for the taxonomy
                if( sizeof($terms_names) == 0 ){
                    continue; // We go to next attribute
                }
    
                // Add the attribute label name to the output
                if( $count > 1 ) $html .= ' - ';
                $html .= wc_attribute_label( $taxonomy );
                // Formatting the term names in a string and add them to the output
                $html .= ':</strong> ' . implode( ', ', $terms_names) . '<strong>';
            }
        }
        // Outputing The html
        echo $html.'</p>';
        echo $attr_count .' | '.$count;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且可以工作……

    【讨论】:

    • 你好,我看到这段代码不再工作了:是否有可能最后的 WooCommerce 版本已经破坏了它?
    • @Giovanni 可能是...此代码已用于 woocommerce 的早期版本,并且运行良好。我会尝试在最后一个版本的 Woocommerce 中找出问题所在。
    • @Giovanni 我已经在 Woocommerce 最新版本 3.4.2 中测试了该代码,它仍然像以前一样正常工作。 现在我进行了更新,以确保产品属性存在,并且这些产品属性的属性术语存在,避免在 Url 参数不匹配时出现输出错误。
    • 谢谢。这很奇怪,因为在我的网站上不起作用。我检查得更好。
    猜你喜欢
    • 1970-01-01
    • 2019-01-26
    • 2016-08-22
    • 2019-04-29
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多