WooCommerce - 按标签和类别分类的相关产品 [英] WooCommerce - related products by tags and categories

查看:19
本文介绍了WooCommerce - 按标签和类别分类的相关产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据标签在我网站的每个产品页面中显示 8 个相关产品".但如果结果少于 8 个,则用相同类别的产品填补空白.

I want to display 8 "related products" in every product page of my site based on tags. But if there are less than 8 results fill the gaps with products in the same Categories.

这是我用于仅显示与标签相关的产品的代码 (functions.php):

Here is code that I'm using for showing only tag-related products (functions.php):

//New "Related Products" function for WooCommerce
function get_related_custom( $id, $limit = 5 ) {
global $woocommerce;

// Related products are found from category and tag
$tags_array = array(0);
$cats_array = array(0);

// Get tags
$terms = wp_get_post_terms($id, 'product_tag');
foreach ( $terms as $term ) $tags_array[] = $term->term_id;

// Get categories (removed / commented)
/*
$terms = wp_get_post_terms($id, 'product_cat');
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
 */

// Don't bother if none are set
if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();

// Meta query
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();

// Get the posts
$related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array(
    'orderby'        => 'rand',
    'posts_per_page' => $limit,
    'post_type'      => 'product',
    'fields'         => 'ids',
    'meta_query'     => $meta_query,
    'tax_query'      => array(
        'relation'      => 'OR',
        array(
            'taxonomy'     => 'product_cat',
            'field'        => 'id',
            'terms'        => $cats_array
        ),
        array(
            'taxonomy'     => 'product_tag',
            'field'        => 'id',
            'terms'        => $tags_array
        )
    )
) ) );
$related_posts = array_diff( $related_posts, array( $id ));
return $related_posts;
}
add_action('init','get_related_custom');

推荐答案

您编写的函数现已停止使用 (在 GitHub 中查看此内容)

The function you wrote is now discontinued (see this in GitHub)

(作为我们可以在这里阅读),您可以在wp-content/themes/theme-name/的functions.php文件中添加一个两个片段.

(As we can read here), you can add one of two snippets in functions.php file in wp-content/themes/theme-name/.

如果您想按标签隐藏相关产品,请添加:

If you want to hide related products by tag, add this:

/**
 * Does not filter related products by tag
 */
add_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );

或者添加这个,如果你想按类别隐藏相关产品:

or add this, if you want to hide related products by category:

/**
 * Does not filter related products by category
 */
add_filter( 'woocommerce_product_related_posts_relate_by_category', '__return_false' );

此后,您可能需要清除瞬态以查看结果(或等待其到期).

After this, you might need to clear your transients to see a result (or wait for their expiration).

如果您添加两个片段(如在另一个答案中),您的相关产品将为空,因为它们不会从标签和类别中填充

If you add both snippets (as in the other answer), your related products would be empty, because they won't be populated from tags and from categories

这篇关于WooCommerce - 按标签和类别分类的相关产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆