Codexshaper / laravel-woocommerce

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

Only get 10 registers #53

Open arpanet4 opened 3 years ago

arpanet4 commented 3 years ago

Hello, in the last version only get 10 registers.

For example in: $orders = Order::all();

Thanks.

maab16 commented 3 years ago

Hello @arpanet4,

Thanks for creating the issue. By default woocommerce return 10 results per page. You can change the default option by several ways

  1. Using Woocommerce facade
$options = [
    'per_page' => 50 // Or your desire number
];

$orders = Woocommerce::all('orders', $options);
  1. Using Order facade
$options = [
    'per_page' => 50 // Or your desire number
];

$orders = Order::all($options);
  1. Using the paginate method
$per_page = 50; // Or your desire number
$current_page = 1; // By default 1. That reurns first 1 to 50. If 2 then return 51 to 100 and vice versa

$orders = Order::paginate($per_page, $current_page);

I hope it solves your issues. If you need further query just send details here.

CRMSoluciones commented 3 years ago

I want to know in this case: i get the registers by this way : $results= WCCustomer::all(['per_page'=>100]); In this case, apparently only it possible get 100 records, if I put a number greater than 100 it shows the next error: Invalid parameter (s): per_page [rest_invalid_param] How can i get all the records?

vlkf commented 2 years ago

I want to know in this case: i get the registers by this way : $results= WCCustomer::all(['per_page'=>100]); In this case, apparently only it possible get 100 records, if I put a number greater than 100 it shows the next error: Invalid parameter (s): per_page [rest_invalid_param] How can i get all the records?

Any solution here?

maab16 commented 2 years ago

Hello @CRMSoluciones @vlkf,

Where do you get WCCustomer? Please check the original doc https://woocommerce.github.io/woocommerce-rest-api-docs/#order-properties

vlkf commented 2 years ago

Hello @CRMSoluciones @vlkf,

Where do you get WCCustomer? Please check the original doc https://woocommerce.github.io/woocommerce-rest-api-docs/#order-properties

It's not working with Product facade also. Product::all(['per_page'=> 200]); Gives the error -> Exception (1) Error: Invalid parameter(s): per_page [rest_invalid_param]

EazyServer commented 2 years ago

Hello @CRMSoluciones @vlkf, Where do you get WCCustomer? Please check the original doc https://woocommerce.github.io/woocommerce-rest-api-docs/#order-properties

It's not working with Product facade also. Product::all(['per_page'=> 200]); Gives the error -> Exception (1) Error: Invalid parameter(s): per_page [rest_invalid_param]

I am having the same issue! So I kept digging and found the reason why it's failing!

Screenshot 2022-08-02 at 14 09 42

I wish the maintainer show this message instead of just:

Exception (1) Error: Invalid parameter(s): per_page [rest_invalid_param]

EazyServer commented 2 years ago

You can override it on the API server side:

add_action( 'rest_product_query', function( $params ) { if ( isset( $params ) AND isset( $params[ 'posts_per_page' ] ) ) { $params[ 'posts_per_page' ] = "200"; } return $params; });

However, this will most likely degrade the responsiveness of the remote WC API server! especially if used as normal website. Maybe best bet is to use pagination for > 100 products/orders!

maab16 commented 2 years ago

Thanks, @EazyServer for commenting. You are right the per_page value must be between 1 to 100. You must allow your wordpress server so that it can support greater than 100.

https://github.com/woocommerce/wc-api-php/issues/136

vlad88sv commented 1 year ago

Sorry to ask but shouldn't this Product::all() method auto-paginate?

paintface commented 5 days ago

I've just come accross the same need to get "all" categories and this helped me solve my problem. I didn't want to change Wordpress REST API defaults.

Appreciate the method below doesn't use the Order facade/model in the thread but the principle applies nevertheless.

Hopefully helps the next person!

protected function getWooCommerceCategories(): array
{
    $categories = [];
    $currentPage = 1;

    try {
        do {
            $response = WooCommerce::all('products/categories', [
                'per_page' => 50,
                'page' => $currentPage,
            ]);

            $categories = array_merge($categories, $response);
            $currentPage++;
        } while (! empty($response));

    } catch (\Exception $e) {
        Log::error($e->getMessage());
    }

    return $categories;
}