So I want to connect an external database to the woocommerce database in order to export the products from there and import them into woocommerce. Of course, I would first like to check if the products don’t already exist and if so update them instead of creating duplicates. I believe the algorithm below works, but it needs final testings. I can unfortunately not run the tests without being able to get all products from woocommerce (currently only getting 10).
Isn’t there a way to use Automattic from WooCommerce and get all the products instead of 10? (I would prefer not to loop through the pages, if possible).
Any input on the other parts of the code would also be very appreciated.
I have seen people using wc_get_products but I have no idea how to integrate it in my code.
use GuzzleHttp;
use AutomatticWooCommerce;
//Connect to DB1 (external database), get all products and save them in $articles (done)
....
$articles = json_decode($res->getBody());
//-----------Connect to DB2(WooCommerce), get all products and save them in $wooExistingProducts
...
$wooExistingProducts = $wooCommerceClient->get('products');
//GETS ME ONLY 10. Baseurl is "http://localhost:8000"
//HERE I WANT TO INITIALIZE AN "UPDATE OR CREATE" METHOD BETWEEN THE 2 Databases
//loop through each product on the external DB and initialize the template
foreach ($articles as &$article){
$wooTemplate = [
'meta_data' => [
["key" => 'DbOneID',
"value" => $article->product
]
],
'name' => $article->name,
'description' => $article->description,
'categories_name' => $article->category,
'weight' => $article->size,
'regular_price' => $article->price,
'search' => $article->search,
'shipping_class_id' => $article->shippingid,
'stock_quantity' => $article->itemsonstock,
'date_modified' => $article->modified,
];
//check if there are existing products on WooCommerce and if yes
if(!$wooExistingProducts!=1){
//Loop through all existing products on woocommerce
for($x=0; $x<sizeof($wooExistingProducts); $x++){
//loop through all products on the external DB
for($y=0; $y<sizeof($articles); $y++){
//if the product id from the external DB matches the DbOneID from an existing product on woocommerce
if($wooExistingProducts[$x]->meta_data[0]->value==$articles[$y]->id){
//update that product
$wooCommerceClient->put('products/'.urlencode($articles[$y]->id), $wooTemplate);
}
//if the products don't match, take the product from the external DB, using the wooTemplate, and push it to the array productsForWoo.
else{
array_push($productsForWoo, $wooTemplate);
if(sizeof($productsForWoo) == 20){
$data = [
'create' => $productsForWoo,
];
/Push the productsForWoo array to wooCommerce as a batch, limiting each process to maximum 20 products (to avoid the 15s timeout)
$wooCommerceClient->post('products/batch', $data);
/reset the array so we can get all products, without duplicates
$productsForWoo = [];
}
}
}
}
}
//if there are no products on woocommerce, simply push them all in batches of 20 to the WC database
else{
array_push($productsForWoo, $wooTemplate);
if(sizeof($productsForWoo) == 20){
$data = [
'create' => $productsForWoo,
];
$wooCommerceClient->post('products/batch', $data);
$productsForWoo = [];
}
}
}
//As there is a big chance that the last batch will have less than 20 products, push them as well to wooCommerce as a last batch
$data = [
'create' => $productsForWoo,
];
$wooCommerceClient->post('products/batch', $data);
I expect the $wooExistingProducts to get me an array of all existing entries in the Products database so I could use it to loop through it
2
Answers
should work for you.
According to its source on
https://github.com/woocommerce/wc-api-php/blob/master/src/WooCommerce/Client.php#L80
And WooCommerce API params listed here:
https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products
EDIT:
If using bigger limit doesn’t work for you you can use paging, as explained in other thread:
https://stackoverflow.com/a/51555088/2129097
I had this problem also. So I create code for this.
First, create a PHP file in the wp directory and add this code.
This will get the count of products. You can increase the value of post_pre_page
For API request use this code
API Only allow 100 products pre query. So you have to loop it.