douyue2313 2018-03-03 17:28
浏览 50
已采纳

WooCommerce - 在单个产品页面上创建自定义产品循环

I am developing a WooCommerce theme, and on single product page I want to display a carousel of products from same vendor category.

I have the following category structure:

All products

  • product category no 1
  • product category no 2
  • product cat ...

Vendors

  • vendor name 1
  • vendor name 2
  • vendor name ...

While viewing product from category no 1 I would like to display a carousel off other products from same vendor name category if product is assigned to such child category.

In my example Vendors and All products are parent categories on the same level.

Doing a custom product loop is quiet easy, but to be honest I have no idea how I should handle the logic behind finding the right child category. I just can't imagine it , and that's why I can't came up with the right solution.

Each product can be assigned only to one vendor name, and both vendors and products are regular woocommerce categories here.

For the time being I have the following code

wooctrl_same_vendor_products('vendors');

function wooctrl_same_vendor_products($parent_cat_name){

  $parentCat = get_term_by('name', $parent_cat_name, 'product_cat');

  $product_cat_ID = $parentCat->term_id;
  $args = array(
    'hierarchical' => 1,
    'show_option_none' => '',
    'hide_empty' => 0,
    'parent' => $product_cat_ID,
    'taxonomy' => 'product_cat'
  );
  $subcats = get_categories($args);

  foreach ($subcats as $subcat) {
    //check if product is inside ?
  }
}

Should I first create an array of all Vendors child categories and the search for product there or is there a cleaner way to solve it directly from the $product variable?

  • 写回答

2条回答 默认 最新

  • douluo2930 2018-03-05 13:02
    关注

    After reading Andrew comment and reading the docs provided there ( docs I never seen before, so thanks Andrew! Those gave me a lot ) I decided to filter products by the first part of SKU code, as I found it fastest. It's easier to manage the output content from WP admin panel too.

    Here's my code updated with WooCommerce 3 functions. Is it better than the previous solution? What do you think?

    function wooctrl_display_vendor_products_bySKU($number_of_products_displayed, $ordered_by) {
    global $product;
    $currentSku = $product->get_sku();
    $groupSku = explode('-', $currentSku)[0];
    
    $args = array(
        'sku' => $groupSku,
        'post_type' => 'product',
        'posts_per_page' => $number_of_products_displayed,
        'orderby' => $ordered_by,
        'exclude' => array($product->get_id())
    );
    $query = new WC_Product_Query($args);
    $sku_products = $query->get_products();
    //print_r($products);
    if ($sku_products) : ?>
    
    
        <div class="wrapper">
            <h1 class="section-header"> <?php echo __('Same vendor products', 'genesis-wooctrl') ?> </h1>
    
    
            <?php foreach ($sku_products as $sku_product) : ?>
    
                <?php
                $post_object = get_post($sku_product->get_id());
                setup_postdata($GLOBALS['post'] =& $post_object);
                wc_get_template_part('content', 'product'); ?>
    
            <?php endforeach; ?>
    
    
        </div>
    
    
    <?php endif;
    wp_reset_postdata();
    }
    

    And for those, who are seeking realiable source of function, here are the URL's that helped me solving this issue a lot:

    https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/

    https://github.com/woocommerce/woocommerce/wiki/Product-Data-Schema

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器