【问题标题】:Get WooCommerce products from specific category从特定类别获取 WooCommerce 产品
【发布时间】:2021-11-07 07:44:54
【问题描述】:

我正在尝试插入 WooCommerce 元素以将特定类别中的“产品”(课程)显示到页面中。

我还必须为这个特定类别隐藏这些产品,它按预期工作。我刚刚在functions.php中添加了一个过滤器,是吗:

/*
 * Exclude "packages" category from "Archive Products" on page 811
 * Ref. URL: https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
 */
function exclude_category_archive_products( $q ) {
   $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'packages' ),
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}
if( $current_post_id != "811" ) {
    add_filter( 'woocommerce_product_query', 'exclude_category_archive_products' );
}
/* END Exclude "packages" category from "Archive Products" on page 811 */

我一直在寻找实现相反目标的方法,但我没有找到任何“从今年开始或接近尾声”的东西。我尝试使用“IN”或“=”运算符,但没有用(它显示所有内容):

/*
 * Display "packages" category only
 */
function show_only_category_in_page( $q ) {
    var_dump("It reaches the function");
    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'packages' ),
           'operator' => '='
    );

    $q->set( 'tax_query', $tax_query );
}
if( $current_post_id == "811" ) {
    var_dump("It reaches the page");
    add_filter( 'woocommerce_product_query', 'show_only_category_in_page' );
}
/*  END Display "packages" category only */

前面的代码只写了string(23) "It gets reachs the page"。我做错了什么?

【问题讨论】:

    标签: php wordpress woocommerce elementor lms


    【解决方案1】:

    你应该试试这个,因为你可以从特定类别中获得产品

    解决方案 1:

    $prod_categories = array(1, 2,3);
    $product_args = array(
      'numberposts' => $limit,
      'post_status' => array('publish', 'pending', 'private', 'draft'),
      'post_type' => array('product', 'product_variation'),
      'orderby' => 'ID',
      'suppress_filters' => false,
      'order' => 'ASC',
      'offset' => 0
    );
    
    if (!empty($prod_categories)) {
       $product_args['tax_query'] = array(
          array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $prod_categories,
            'operator' => 'IN',
       ));
     }
    
     $products = get_posts($product_args);
    

    1、2、3 是您的类别 ID。

    解决方案 2

    创建自定义页面并显示特定类别的产品:

    <?php
    /**
    * Template Name: Courses template
    *
    */
    defined( 'ABSPATH' ) || exit;
    
    
    get_header();
    ?>
        <div id="content" class="content" role="main">
    
            <ul class="products">
                <?php
                    $args = array( 
                        'post_type' => 'product', 
                        'posts_per_page' => -1, 
                        'product_cat' => 'clothing', // Category slug "clothing"
                        'orderby' => 'rand' 
                    );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
                
                        <h2>Courses</h2>
                
                            <li class="product">    
                
                                <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                
                                    <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
                
                                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
                
                                    <h3><?php the_title(); ?></h3>
                
                                    <span class="price"><?php echo $product->get_price_html(); ?></span>                    
                
                                </a>
                
                                <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                
                            </li>
                
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
            </ul>
        </div><!-- #content -->
    
    <?php get_footer(); ?>
    

    步骤:

    1. 转到您的活动主题创建一个新页面,如 course-tpl.php
    2. 将上述代码复制并粘贴到您的自定义页面 course-tpl.php 中,并将类别 slug“服装”更改/替换为“您的类别 slug”并保存。
    3. 打开仪表板 - 转到页面和“添加新”页面,输入页面标题并分配“课程模板”并保存
    4. 打开新页面
    5. 相应地更新/管理页面 CSS 和 HTML。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 2018-11-09
      相关资源
      最近更新 更多