【问题标题】:WooCommerce - related products by tags and categoriesWooCommerce - 按标签和类别分类的相关产品
【发布时间】:2015-06-29 17:54:46
【问题描述】:

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

这是我用于仅显示与标签相关的产品的代码 (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');

【问题讨论】:

  • 创建 2 个查询,一个基于标签,一个基于类别,然后使用 post_count 检查帖子数,if( $category_query->post_count > 8 ) ......

标签: php wordpress woocommerce related-content


【解决方案1】:

你写的函数现在已经停止了(see this in GitHub

(As we can read here),您可以在 wp-content/themes/theme-name 的 functions.php 文件中添加 一个 两个 sn-ps /

如果要通过标签隐藏相关产品,请添加:

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

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

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

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

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

【讨论】:

  • 必须清除瞬变才能看到变化。谢谢
  • @MohamedOmar 您的评论让我免于浪费大量时间,谢谢!
【解决方案2】:

wp-content/themes/your-theme-name/ 中打开您的 functions.php 文件,并在文件末尾添加以下代码:

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

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

【讨论】:

    【解决方案3】:

    有一个漂亮的免费插件可以满足您的要求:https://wordpress.org/plugins/woo-related-products-refresh-on-reload/

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 1970-01-01
      • 2018-01-23
      • 2018-06-08
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多