Codexshaper / laravel-woocommerce

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

i want to update a long list of products at the same time #45

Open badreddine5 opened 3 years ago

badreddine5 commented 3 years ago

first of all, thank you very much for this package you really helped us a lot.

well i want to update a list of products, and i think "Product::update" changes only one by one and that will cause a lot of requests to the serve.. is there a possibility to push a whole array to the server using update Thank you in advance

maab16 commented 3 years ago

@badreddine5,

Thanks again for creating another issue. You can use the batch method to do it. It helps to create, update, and delete several items. Please follow the official documentation for laravel-woocommerce. Here is the link https://codexshaper.github.io/docs/laravel-woocommerce/#batch-product

In your case, you can use like below example

@badreddine5,

Thanks again for creating another issue. You can use the batch method to do it. It helps to create, update, and delete several items. Please follow the official documentation for laravel-woocommerce. Here is the link https://codexshaper.github.io/docs/laravel-woocommerce/#batch-product

In your case, you can use like below example

$data = [
    'create' => [],
    'update' => [
        [
            'id' => 799,
            'default_attributes' => [
                [
                    'id' => 6,
                    'name' => 'Color,
                    'option' => 'Green'
                ],
                [
                    'id' => 0,
                    'name' => 'Size',
                    'option' => 'M'
                ]
            ]
        ],
        [
            'id' => 811,
            'name' => 'Woo Product Update',
        ]
    ],
    'delete' => []
];

$batch = Product::batch($data);

Hope it helps you. If you think this package helpful then please don't forget to press the Star (It helps contributor to contribute more) button and share it with your friends, blogs, social media, or anywhere so that people get help.

badreddine5 commented 3 years ago

hello, thank you for your help. now i'm getting a new error "Unable to accept more than 100 items for this request. " I changed the maximum in Woocommerce to 2000, but still when i try to update i get that error. Any idea?

maab16 commented 3 years ago

@badreddine5

Wocommerce limit 100 products for batch to avoid kill longer request. You can read this issue https://github.com/woocommerce/woocommerce/issues/7915

I think the best solution is to fetch all products and split it by 100 then call requests one by one.

mcpuishor commented 3 years ago

I had a similar problem to solve when I had to migrate 20K users together with their orders from one website to Woo.

First of all, I would advise to use queued jobs to dispatch such large numbers of batched requests (creates, updates etc). Laravel has a great system to handle queues, make good use of that. This would allow the requests not to run into max_execution_time issues in php.

Second, make good use of chunking of large datasets in Laravel. [https://laravel.com/docs/8.x/eloquent#chunking-results] to dispatch the jobs.

This kind of approach will allow you to have a fast execution of your request to Laravel, and leave the actual interaction with Woo to be done in the background.