当前位置: 首页 > 知识库问答 >
问题:

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

孙泳
2023-03-14

我正在开发一个WooCommerce主题,在单个产品页面上,我想显示来自同一供应商类别的产品旋转木马。

我有以下类别结构:

所有产品

  • 1号产品类别
  • 第2类产品
  • 产品类别

供应商

  • 供应商名称1
  • 供应商名称2
  • 供应商名称

在查看类别1中的产品时,如果将产品分配给此类子类别,则我希望显示来自同一供应商名称类别的其他产品的转盘。

在我的示例中,供应商和所有产品都是同一级别的父类别。

做一个定制的产品循环非常简单,但老实说,我不知道如何处理寻找正确子类别背后的逻辑。我只是无法想象,这就是为什么我不能想出正确的解决方案。

每个产品只能分配给一个供应商名称,并且供应商和产品都是这里的常规商业类别。

目前我有以下代码

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 ?
  }
}

我应该首先创建一个包含所有供应商子类别和产品搜索的数组,还是有一种更干净的方法直接从$product变量中解决它?

共有2个答案

葛驰
2023-03-14

在阅读了Andrew comment和那里提供的文档(我以前从未见过这些文档,所以感谢Andrew!它们给了我很多东西)之后,我决定按照SKU代码的第一部分来过滤产品,因为我发现这是最快的。从WP管理面板管理输出内容也更容易。

这是我用WooCommerce 3函数更新的代码。它比以前的解决方案好吗?你怎么认为?

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();
}

对于那些正在寻找可实现的功能源的用户,以下是帮助我解决此问题的URL:

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

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

齐鸿光
2023-03-14

我得到了这个工作,但我认为这是安静肮脏的解决方案。你能看看代码,告诉我如何以“良好的练习”的方式做到这一点吗?

因此,在第一步a中,我准备了供应商主类别下所有儿童类别的列表:

wooctrl_vendor_cats('vendors');  // <-- main category slug

function wooctrl_vendor_cats($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) {
    //print_r($subcat->slug);
    check_if_in_vendor($subcat->slug); // <-- here I am passing each slug in order to compare it with products categories
}
}

这是一个函数,我在其中交叉引用产品类别和供应商类别,以找到匹配项,然后通过它循环显示其中的所有产品:

function check_if_in_vendor($cat_slug){
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( $cat_slug, $categories ) ) {

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 10,
        'product_cat' => $cat_slug,
        'orderby' => 'rand',
        'post__not_in' => array( $post->ID )
    );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();  ?>



        <li class="product">

            <h3><?php the_title(); ?></h3>

        </li>

    <?php endwhile; ?>
    <?php wp_reset_query();
}
} 
 类似资料:
  • 我正在工作一个WordPress网站,与WooCommerce功能。 我使用以下代码为Product页面后端的Product Data框创建了2个自定义字段: 参照,我想创建一个函数,当选中此复选框时,它将在相关产品的页面上创建一个自定义文本框。然后,潜在客户可以使用此自定义文本框输入一段文本,他们希望将其打印到页面的产品中。 有没有人知道,为了实现上述目标,我需要输入哪些额外的代码?

  • 我有一家woocommerce商店,想定制我的产品。用户直接访问类别页面,无店铺存档页面。 你有没有什么好的方法来定制订单?我试着使用插件,并给那些产品一个编号。。。但一切都没有奏效。 问候

  • 在函数的单个产品页面中获取产品的SKU时,我遇到了很多麻烦。php。我有多个单一产品页面,我希望根据产品显示不同的文本。我已经创建了一个子主题,我正在处理函数。php文件。我不熟悉wordpress和编辑主题,所以我还不太了解操作顺序。我能够让代码循环运行,并为所有产品提供所有SKU,但这与我所处的实际页面无关。 我试过很多东西。常见的解决方案似乎是: 但那不起作用。由于某些原因,$producs

  • 我在WooCommerce单一产品页面上创建了六个自定义属性,其中包含一个自定义函数。它们包括:图标、标签和术语。 基于用每个答案代码的自定义图像替换WooCommerce属性标签,我使用了以下函数(使用自定义HTML标记,与WooCommerce product-attributes.php模板中的标记不同): 该函数工作正常,但存在一个问题:如果特定属性的项不止一个(例如颜色:红色、蓝色、绿色

  • 我似乎在为木业的某些东西而苦苦挣扎。 我已经创建了一个单一的产品页面模板,当前显示产品的特色图像在页面顶部的横幅。 但我想我得先找到分类?我不确定从这里到哪里去。 如果有任何帮助,我们将不胜感激。