【问题标题】:Woocommerce Random UpsellsWoocommerce 随机追加销售
【发布时间】:2014-02-17 18:01:19
【问题描述】:

Woocommerce

除了为每个产品手动添加追加销售外,有没有办法显示同一产品类别中的随机追加销售产品?

提前非常感谢...

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    嘿,我遇到了这个线程,试图弄清楚如何做类似的事情。我使用@ViszinisA 提供的大部分查询来调整 up-sells.php 模板。

    up-sells.php 从第 23 行开始:

    $cats = get_the_terms($product->id, 'product_cat');
    if (!count($cats)) {
        return;
    }
    $args = array(
        'post_type'           => 'product',
        'ignore_sticky_posts' => 1,
        'no_found_rows'       => 1,
        'posts_per_page'      => 5,
        'orderby'             => 'rand',
        'post__not_in'        => array( $product->id ),
        'meta_query'          => $meta_query,
        'tax_query'           => array(
                                    array(
                                        'taxonomy' => 'product_cat',
                                        'field' => 'id',
                                        'terms' => $cats[0]->term_id
                                    )
                                )
    );
    $prodQuery = new WP_Query( $args );
    
    if ( $prodQuery->have_posts() ) {
        $postProds = $prodQuery->posts;
    ?>
    
        <section class="up-sells upsells products">
    
            <h2><?php esc_html_e( 'You may also like&hellip;', 'woocommerce' ) ?></h2>
            <br>
            <?php woocommerce_product_loop_start(); ?>
    
                <?php foreach ( $postProds as $postProd ) : ?>
    
                    <?php
                        // $post_object = get_post( $upsell->get_id() );
                        setup_postdata( $GLOBALS['post'] =& $postProd );
                        wc_get_template_part( 'content', 'product' ); ?>
    
                <?php endforeach; ?>
    
            <?php woocommerce_product_loop_end(); ?>
    
        </section>
    
    <?php
    }
    

    它应该在“wp_reset_postdata();”之前的第 69 行(ish)结束。确保用上面的代码替换这些行之间的内容,而不仅仅是添加它。

    记住:此代码将使您设置的任何手动追加销售都不会显示在单个产品页面上。相反,它从当前产品的术语数组中的第一个类别中抽取 5 个随机产品。如果该类别中的产品少于 5 个,则会显示所有产品。

    确保在编辑之前将 up-sells.php 复制到您的子主题目录中。

    此代码已在 3.0.0 版本上进行了测试并且可以正常工作。快乐编码!

    【讨论】:

      【解决方案2】:

      只需修改模板/woocommerce/single-product/up-sells.php。

      $cats = get_the_terms($product->id, 'product_cat');
      if (!count($cats)) {
          return;
      }
      $args = array(
          'post_type'           => 'product',
          'ignore_sticky_posts' => 1,
          'no_found_rows'       => 1,
          'posts_per_page'      => $posts_per_page,
          'orderby'             => 'rand',
          'post__not_in'        => array( $product->id ),
          'meta_query'          => $meta_query,
          'tax_query' => array(
              array(
                  'taxonomy' => 'product_cat',
                  'field' => 'id',
                  'terms' => $cats->term_id
              )
          )
      );
      $products = new WP_Query( $args );
      

      【讨论】:

      • 嗨...你有机会发布整个页面吗?因为我已经用已经存在的东西替换了它并且它没有显示任何东西。
      • 如果您要替换位于插件文件夹中的原始模板,那么这是错误的。您应该将它们复制到您的主题中并在那里进行编辑。这里是原文:github.com/woothemes/woocommerce/blob/master/templates/… 把明显的部分换掉就行了。
      • 除了产品之外不显示任何东西......即使我手动进行了一些追加销售也不显示。
      • 将模板代码粘贴到 pastebin 或 smth 中并分享。
      • 如果产品没有任何追加销售,则退出脚本。你的产品有加售吗?耶稣。删除第 14 行和第 16 行。
      猜你喜欢
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 2018-01-22
      • 2017-02-17
      相关资源
      最近更新 更多