Codexshaper / laravel-woocommerce

WooCommerce Rest API for Laravel
MIT License
198 stars 57 forks source link

Paginate for Term::all($attribute_id) #50

Open nikuzz opened 4 years ago

nikuzz commented 4 years ago

Hi, I'm trying to get all the terms of an attribute, but with the pagination of Wordpress I can't reach the goal with the following simple statement:

$myterms=Term::all($attribute_id);

I try with the following:

$next_page = null;
do {
    $myterms = WooCommerce::all("products/attributes/$attribute_id/terms", ['per_page' => 100, 'page'=> $next_page]);
    $next_page = WooCommerce::next();
    // do what you want with $myterms
} while ($next_page > 0);

Maybe there is a more elegant way?

maab16 commented 4 years ago

Hello @nikuzz,

Currently some sub-model likeTerm not working paginate like Product::paginate(). I'm a little bit busy. I'll add it soon. Thanks for the patient.

maab16 commented 4 years ago

Good news. New version (v3.0) released. Now you can use pagination for all. Also you can use laravel collection (filter, map, all of the method of collection and lazy collection).

If you are still facing same issue then reply with details. Thanks

Tchayo commented 4 years ago

Hello, pagination does work but it returns objects instead of arrays. How do i force it to return data array, with a seperate pagination object?

maab16 commented 4 years ago

From version 3, By default return collection. You can return array in two ways

  1. Using withOriginal
    Term::withOriginal()->get()
  2. Using toArray
    Term::all()->toArray()
Tchayo commented 4 years ago

this is what im trying to do

Category::paginate();

this is what i get

{ "0":{ "id":19, "name":"Accessories", "slug":"accessories", "parent":16, "description":"" }, "1":{ "id":67, "name":"Accessories", "slug":"accessories-men", "parent":48, "description":"" }, "2":{ "id":68, "name":"Backpacks", "slug":"backpacks", "parent":48, "description":"", }, "pagination":{ "total_results":18, "total_pages":2, "current_page":1, "previous_page":null, "next_page":2, "first_page":1, "last_page":18 } }

which is an object of the data i am fetching with indexes as keys and a pagination object.

maab16 commented 4 years ago

Try

$categories = Category::paginate()->toArray();

OR

$categories = Category::withOriginal()->paginate();

Is it working?

Tchayo commented 4 years ago

Tried both but getting a similar result.

maab16 commented 4 years ago

I just fixed it. Just reinstall the package or update it to v3.0.1 or later. Now data and meta are separated.

$results = Category::paginate();
$categories = $results['data'];
$pagination = $results['meta'];
Tchayo commented 4 years ago

That solved it for me. Thanks for the quick assist.

morvy commented 4 years ago

Looks like there is a bug in the pagination:

$result = Category::paginate()->toArray();

the above returns correct totals and pagination data:

'meta' => 
    array (size=7)
      'total_results' => int 85
      'total_pages' => int 9
      'current_page' => int 1
      'previous_page' => null
      'next_page' => int 2
      'first_page' => int 1
      'last_page' => int 85

It also returns 10 items which is correct.

But adding ANY parameter will break the pagination:

$options = [
    'page' => 1
];
$result = Category::paginate($options)->toArray();

the above returns:

'meta' => 
    array (size=7)
      'total_results' => int 85
      'total_pages' => int 85
      'current_page' => int 1
      'previous_page' => null
      'next_page' => int 2
      'first_page' => int 1
      'last_page' => int 85

Plus it only returns first item from all 85 items, doesn't matter if I set page to 1 or 5 or 9 ... or even per_page to 100.

PS: WooCommerce:all works OK, so I'm using it as a workaround for this scenario.

gdevlugt commented 3 years ago

But adding ANY parameter will break the pagination:

$options = [
    'page' => 1
];
$result = Category::paginate($options)->toArray();

This is because the paginate method has three (default) arguments, and the $options argument comes last.

protected function paginate($per_page = 10, $current_page = 1, $options = [])
morvy commented 3 years ago

That's correct & thank you, I should've checked the code first as I haven't seen these arguments in the docs