laravel-woocommerce icon indicating copy to clipboard operation
laravel-woocommerce copied to clipboard

Paginate for Term::all($attribute_id)

Open nikuzz opened this issue 4 years ago • 12 comments

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?

nikuzz avatar Oct 19 '20 19:10 nikuzz

Hello @nikuzz,

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

maab16 avatar Oct 27 '20 10:10 maab16

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

maab16 avatar Oct 28 '20 12:10 maab16

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?

Tchayo avatar Nov 12 '20 09:11 Tchayo

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

  1. Using withOriginal
Term::withOriginal()->get()
  1. Using toArray
Term::all()->toArray()

maab16 avatar Nov 12 '20 09:11 maab16

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.

Tchayo avatar Nov 12 '20 09:11 Tchayo

Try

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

OR

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

Is it working?

maab16 avatar Nov 12 '20 09:11 maab16

Tried both but getting a similar result.

Tchayo avatar Nov 12 '20 10:11 Tchayo

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'];

maab16 avatar Nov 12 '20 10:11 maab16

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

Tchayo avatar Nov 12 '20 10:11 Tchayo

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.

morvy avatar Nov 22 '20 00:11 morvy

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 = [])

gdevlugt avatar Nov 29 '20 15:11 gdevlugt

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

morvy avatar Nov 30 '20 12:11 morvy