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

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条)

报告相同问题?

悬赏问题

  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 数电几道习题,写出作答过程,ai一律不采用
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52
  • ¥100 只改动本课件的 cal_portfolio_weight_series(decision_date), 跑完本课件。设计一个信息比率尽量高的策略。
  • ¥20 如何在visual studio 2022中添加ImageMagick库
  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频