1

I am trying to fetch all the products of the woocommerce from the database using php.. But it is not giving me the list of all products

  <select Name='choose'>
  <?php
  $args = array( 'post_type' => 'product' ,'posts_per_page' => 100);
  $loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : 
      $loop->the_post();
      echo '<option selected value="'.the_title().'</option>';
  endwhile;
  ?>
</select>

can anybody please tell me how can I get all the products of woo commerce

Thanks

9
  • where is product_cat ? Commented Oct 15, 2016 at 4:58
  • I want all category products Commented Oct 15, 2016 at 5:11
  • try to get one category first, then get a all category names in array format and pass it Commented Oct 15, 2016 at 5:21
  • also see this .. functionsphp.com/get-list-of-all-woocommerce-products Commented Oct 15, 2016 at 5:21
  • I already try this but it is not working for me @LifeTimeProgrammer.. check out I also ask question regarding this thats why I move to this method stackoverflow.com/questions/40020190/… Commented Oct 15, 2016 at 5:23

3 Answers 3

2

In order to fetch Single product to your single.php page you can make use of the code which i have already suggested over here. Refer here:

https://wordpress.stackexchange.com/questions/240653/how-to-query-single-product-in-woocommerce/240726#240726

In order to fetch All products that you have into your DB you can follow the steps below.

You can fetch the Woo-commerce Products based on the WP_Query also so that it is also saved in the wp_posts table alone. So that we might slightly modify the post type alone for getting the WP_Query to work on.

Step One: First We shall see to the parameters what we have to pass for the WP_Query.

$params = array(
        'posts_per_page' => 5, 
        'post_type' => 'product'
);

Note: As you can see, all we’ve done is added a post_type variable to the array, and set it’s value to "product"; the query will now look for WooCommerce products instead of posts.

Step Two: After that we have to run on with the same structure that the Normal WP_Query behaves.

<?php
$params = array(
        'posts_per_page' => 5, 
        'post_type' => 'product'
);
$wc_query = new WP_Query($params); 
?>
<?php if ($wc_query->have_posts()) :  ?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post();  ?>
<?php the_title(); //Prints the title over Here ?>
<?php //You can add what ever you need from the loop over here ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p>
     <?php _e( 'No Products' );  ?>
</p>
<?php endif; ?>

Explanations

  • An array of parameters for WP_Query to work with is created; to start with it’s just the basic posts, but by adding more specifics to these parameters we can achieve different results for our queries.
  • WP_Query is used to query the parameters created in the first line.
  • The query is checked to see if it returned any results.
  • If there are results, then we iterate over them:
    • First, setting the global variable $post, which ensures that the next function works.
    • Second, setting the the_title() variable, which is responsible for displaying the product title.
  • Then, when the posts are displayed, we return the $post variable to its original state with thewp_reset_postdata function.

Using this link i have studied how to query the WooCommerce Products using WP_Query and I have shared here so that all might get used with this posts.

Happy Coding :)

Sign up to request clarification or add additional context in comments.

Comments

1

Your code is working fine on my machine, However its seems some conflict to your previous wp query. You can also try to get all product like this..

<select Name='choose'>
<?php
$args = array( 'post_type' => 'product' ,'posts_per_page' => 100);
$products = get_posts( $args );
foreach( $products as $product ) : 
  echo '<option selected value="'.$product->post_title.'</option>';
endforeach;
?>
</select>

Comments

0
$this->db->select('*');
        $this->db->from('vm_posts');
        $this->db->join('vm_postmeta', 'vm_postmeta.post_id = vm_posts.id');
        $this->db->join('vm_wc_product_meta_lookup', 'vm_wc_product_meta_lookup.product_id = vm_postmeta.post_id');
        $this->db->join('vm_inventory', 'vm_wc_product_meta_lookup.sku = vm_inventory.product_sku');
        $this->db->join('vm_terms', 'vm_inventory.customer_id = vm_terms.name');
        $this->db->where('post_type','product');
        $query = $this->db->get()->result();

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.