繁体   English   中英

如何通过 REST API 在 ZB6BC1DC92696EDCB7DB7CFFD51FZ 上编写“更新或创建” function?

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

我正在尝试创建一个“更新或创建”方法,该方法遍历外部数据库上的所有产品,并检查每个产品的 ID 是否与 WooCommerce 数据库上的任何 ID 匹配。 如果是,则更新它,如果不是,则在 WooCommerce 上的 Products 数据库中创建一个新条目。 我以这样一种方式使两个数据库具有相同的产品,除了一个产品,我出于测试目的从 WC 中删除了该产品。 似乎一切都在正常工作,直到我找到丢失的产品(您将在最后一条评论通过 echo 登录控制台之前看到它”

我试图将 if 语句移到其他地方,但只有在该行上,它才能通过 echo script 标签找到我丢失的产品

//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);

我应该能够将丢失的产品推送到数据库中,但是如果我取消注释最后一部分并将其包含在 if 中,而不是 echo 脚本标记中,它会在 WooCommerce 上创建一个没有名称且没有模板的空产品。

显然,整个问题都在嵌套循环中。 我设法使用另一篇文章的答案来解决它: 嵌套循环逻辑中的 If 语句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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