简体   繁体   中英

Woocommerce get products by specific categories

I'm developing themes for woocommerce, I need help to retrieve information from products by categories, example, I want to display products from 'Shirt' Categories limit by 3 items, here's the code from woo themes that show products by featured products,( i tried to change to display by categories and not work)

<ul class="featured-products products">

                <?php
                $args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
                $i = 0;                 
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); $_product; $i++; 

                if ( function_exists( 'get_product' ) ) {
                    $_product = get_product( $loop->post->ID );
                } else { 
                    $_product = new WC_Product( $loop->post->ID );
                }

                ?>

                        <li class="product <?php if ($i%3==0) echo ' last'; if (($i-1)%3==0) echo ' first'; ?>">
                            <div class="inner">
                            <?php woocommerce_show_product_sale_flash( $post, $_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 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" />'; ?>
                                <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 ); ?>
                            <?php smpl_product_more_details(); ?>
                            </div>
                        </li>

                <?php endwhile; ?>

                </ul>

I'm new for this,

Thanks in advance

To get featured product by a specific category you just need to use wc_get_products with featured set to true and the category you specify. See below code.

<?php

// Get featured products by category. on this case its "shirts" which is the slug of the category.
$query_args = array(
    'featured' => true,  
    'category' => array( 'shirts' ),
);
$products = wc_get_products( $query_args );

You can see the full tutorial here https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/

$prod_categories = array(10, 27);
$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);

Replace the $args with the following code

$args = array(
        'post_type'             => 'product',
        'post_status'           => 'publish',
        'tax_query'             => array(
            array(
                'taxonomy'      => 'product_cat',
                'terms'         => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
                'field'         => 'slug',
                'operator'      => $atts['operator']
            )
        )
    );

You just need to replace the ENTER_CATEGORY word with the category name you want to display.

Let me know if this fulfills your requirements.

Please use below Query

<ul class="products">
<?php
    $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

        <h2>Shoes</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(); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM