dow72046 2018-04-13 23:04
浏览 33
已采纳

相关产品Woocommerce 3.0+来自邮政作者

I am trying to display the related product which is related to the post author since I only want to show products or the product that was created by the same user

global $post;
$authorid = $post->post_author;

      foreach ( $related_products as $related_product ) :       
      $post_object = get_post( $related_product->$authorid );
      setup_postdata( $GLOBALS['post'] =& $post_object );
      wc_get_template_part( 'content', 'product' ); 
      endforeach; 

however it doesn't show anything

  • 写回答

1条回答 默认 最新

  • douzhi7082 2018-04-14 05:36
    关注

    You could do something like this which will remove any products not created by the product's author from the related products array. Place this code in your functions.php file.

    add_filter('woocommerce_related_products', 'show_related_products_post_author', 10, 3 );
    
    function show_related_products_post_author( $related_posts, $product_id, $filters ) {
        $author_id = get_post_field( 'post_author', $product_id );
    
        $query = new WC_Product_Query( array(
            'limit' => -1,
            'return' => 'ids',
        ) );
    
        $products = $query->get_products();
    
        foreach( $products as $loop_product_id ) {
            $product_author_id = get_post_field( 'post_author', $loop_product_id );
    
            if( $product_author_id !== $author_id ) {
                $key = array_search($loop_product_id, $related_posts);
                unset($related_posts[$key]);
            }
        }
    
        return $related_posts;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置