skip to Main Content

Cant find full loop example on the web with wc_get_template_part() and wc_get_products(), so looking for help here:

            global $woocommerce;
            global $product;
            $args = array(
                'limit' => 15,
                'category' => array('printers', 'laptop')
            );
            $query_cats = wc_get_products($args);

            foreach ($query_cats as $query_cat) {
               
                echo $query_cat->get_id();
                echo $query_cat->get_title();
                // echo "<pre>";
                // var_dump($query_cat);
                wc_get_template_part('content', 'product');
            }
            
            ?>

Titles and ids are displayed, var_dump also, bu wc_get_template_part – no. I have add_theme_support('woocommerce'); and also body_class();

2

Answers


  1. WooCommerce content-product.php template only works only with standard loop(with instance of Wp_Query). May be following solution can help:

    $args = array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'tax_query'     => [
          array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => ['printers', 'laptop'],
          )
        ],
    );
    
    $product = new WP_Query( $args );
    
    while ( $product->have_posts() ) {
        $product->the_post();
    
        wc_get_template_part( 'content', 'product' );
    
    }
    wp_reset_postdata();
    

    Thanks

    Login or Signup to reply.
  2. Yes, it can be done (2022 update)

    If you want to avoid native WP_Query or using shortcodes, this actually can be done using wc_get_products().

    You just need to setup and reset postdata within your foreach loop and setup WooCommerce loop properly.

    global $post; // Do not forget this!
    
    $args = array(
        'limit' => 15,
        'category' => array('printers', 'laptop')
    );
    $products = wc_get_products( $args );
    
    // Set loop properties
    wc_set_loop_prop('columns', 5);
    
    // Start custom WC loop
    woocommerce_product_loop_start();
    foreach( $products as $product ) {
    
        // Setup postdata
        $post = get_post( $product->get_id() );
        setup_postdata( $post );
    
        // Get template part
        wc_get_template_part( 'content', 'product' );
    }
    
    // End loop and reset postdata
    woocommerce_product_loop_end();
    wp_reset_postdata();
    

    Important note: this will only work if queried products are within any HTML element with the woocommerce class (otherwise WooCommerce CSS won’t load for your products). In some templates, the woocommerce class is already part of the DOM (e.g. <body> element), but if it isn’t, wrap your loop within an element as such:

    <div class="woocommerce my-products-loop">
        <?php // Your loop goes here ?>
    </div>
    

    TIP: You can set various loop properties using wc_set_loop_prop in the "Set loop properties" part of the code

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search