简体   繁体   中英

How to write a “update or create” function via REST API on WooCommerce?

I am trying to create an "update or create" method, which loops through all products on an external database and checks if the ID of each product matches any ID on the WooCommerce database. If yes, then update it, if not then create a new entry in the Products database on WooCommerce. I have made it in such a way that both databases have the same products, except one product, which I removed from WC for the purpose of testing. It seems like everything is working up until the point where I find the missing product (you will see it before the last commentary being logged on the console via echo"

I've tried to move the if statement elsewhere, but only on that line does it find my missing product via the echo script tag

//Loop through all existing products on woocommerce
 foreach ($articles as &$article){ 
 for($x=0; $x<sizeof($articles); $x++){
 $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,
    ];
//loop through all products on the external DB
  foreach ($wooExistingProducts as &$product){
     for($y=0; $y<sizeof($wooExistingProducts); $y++){
//if the product id from the external DB matches the dbOneID from an existing product on woocommerce
         if($articles[$x]->Artikelnummer==$wooExistingProducts[$y]->meta_data[0]->value){
//update that product
             $wooCommerceClient->put('products/'.urlencode($product->meta_data[0]->value), $wooTemplate);
            break;
        }
    }
//HERE I FIND MY MISSING PRODUCT although it complains that it's trying to get the property of meta_data and of value of a non-object
    if($articles[$x]->id!=$wooExistingProducts[$y]->meta_data[0]->value){echo "<script>console.log(".json_encode($articles[$x]).");</script>";}
//    $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,
//      ];
// array_push($productsForWoo, $wooTemplate);
// //HERE I SEPARATE THEM INTO BATCHES TO AVOID TIMEOUT (15s on WC)
// if(sizeof($productsForWoo) == 10){
//     $data = [
//         'create' => $productsForWoo,
//     ];
//     $wooCommerceClient->post('products/batch', $data);
//     $productsForWoo = [];
// }
//}
////As there is a big chance that the last batch will have less than 10 products, push them as well to wooCommerce as a last batch
// $data = [
// 'create' => $productsForWoo,
// ];
// $wooCommerceClient->post('products/batch', $data);

I should be able to push the missing product into the database but if I uncomment the last part and include it inside the if, instead of the echo script tag, it creates an empty product with no name and no template on WooCommerce.

Apparently the whole problem was laying in the nested loop. I managed to solve it using the answer from another post: If statement in a nested loop logic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM