kloon / WooCommerce-REST-API-Client-Library

A PHP wrapper for the WooCommerce REST API
GNU General Public License v3.0
356 stars 175 forks source link

and you can filter products by type of category ? #195

Open eros2187 opened 8 years ago

eros2187 commented 8 years ago

hi I'm using this library to my project I want to know if and can filter The list of products by category? that syntax must utilize ?? thank you

you can have an example? thank you

vsinghp commented 8 years ago

i want to know how filter product by categories ??? if you can have an example tell me

eros2187 commented 8 years ago

when you receive an array of products from api woocommerce how can I do to filter the products according to a category? how you should write in php

exsample

<?php

$cat='potatoes'; print_r( $client->products->get(filter[category]' => $cat) ); ?>

it's possible?? you can do something like the current api permit? otherwise how can I do to implement this feature? it would be helpful certainly

Can you explain how?

thanks for your time giuseppe

jaythegeek commented 8 years ago

Unfortunately this is not currently possible. WP Query does not facilitate orderby category.

The approach I would suggest would be to pull each category separately loop through them and then sort by title ASC but this very heavy on the server depending on store size etc

$categories = get_categories( array ('orderby' => 'name', 'order' => 'asc' ) );

foreach ($categories as $category){

echo "Category is: $category->name
";

$catPosts = new WP_Query( array ( 'category_name' => $category->slug, 'orderby' => 'title' ) );

if ( $catPosts->have_posts() ){

   while ( $catPost->have_posts() ){
      $catPost->the_post();
      echo "<a href='the_permalink()'>the_title()</a>";
   }

   echo "<p><a href='/category/$category->slug'>More...</a></p>";

}//end if

} //end foreach

wp_reset_postdata();