php - WooCommerce: Product Loop by Vendor?

644
I managed to get the store names of all of the current vendors to display in a <ul> tag but it seems that when I loop to get the products, all of the products registered gets displayed under the store name.  

How would I go about getting the products that are unique to the store name? 

This is a WordPress, woocommerce and dokan project.

User Story;

As a user, I'll see the vendor's store name and will be able to see all of the vendor's products underneath the vendor's store name.

<div id="dokan-seller-listing-wrap">
  <div >
    <?php if ( $sellers['users'] ) : ?>
      <ul >
        <?php
        foreach ( $sellers['users'] as $seller ) {
          $store_info_author   = dokan_get_store_info( $post->post_author );
          $store_info       = dokan_get_store_info( $seller->ID );
          $store_name       = isset( $store_info['store_name'] ) ? esc_html( $store_info['store_name'] ) : __( 'N/A', 'dokan-lite' );
          $store_url       = dokan_get_store_url( $seller->ID );
          $featured_seller    = get_user_meta( $seller->ID, 'dokan_feature_seller', true );

          ?>
    <div >
      <div >
      <div >
          <h2><a href="<?php echo esc_attr($store_url); ?>"><?php echo esc_html($store_name); ?></a></h2>
      </div>
      </ul>
        <li >
          <?php
          $args = array(
               'post_type' => 'product', 
               'posts_per_page' => 12
                  );

                    $loop = new WP_Query( $args );
                    if ($sellers['users']) {
                      while ( $loop->have_posts() ) : $loop->the_post();
                        wc_get_template_part( 'content','product' );
                      endwhile;
                    } else {
                      echo __( 'No products found' );
                    }
                    wp_reset_postdata();
                  ?>
        </li><!--/.products-->

          <?php } ?>
      </div>          
    </div>      
  </div>
</div>
944

Answer

Solution:

You need to specify author in your WP_Query arguments something like given below

$args = array(
 'author'  => $seller->ID,
 'post_type' => 'product', 
 'posts_per_page' => 12
);

 $loop = new WP_Query( $args );

 while ( $loop->have_posts() ) : $loop->the_post();
   wc_get_template_part( 'content','product' );
 endwhile;

 wp_reset_postdata();

People are also looking for solutions to the problem: Is there any issue about use ftp functions in PHP running over windows task scheduler?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.