在 Woocommerce 类别存档页面上显示属性值

Show attribute values on Woocommerce category archive pages

在 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

通过这种方式,我可以配置 "direct" 个条目到各个商店部门。

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

Category Name - Attribute 1 name: value1 - Atttribute 2 Name: 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
   // ...
   // ...
}

谢谢

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

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 文件或任何插件文件。

经过测试并有效……