Advertisement
Guest User

Untitled

a guest
Jun 16th, 2018
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1.    protected function getCategoryID($woocommerce, $categoryName)
  2.     {
  3.         $paramsCategory = [
  4.             'search' => "{$categoryName}"
  5.         ];
  6.  
  7.         $data = [
  8.             'name' => "{$categoryName}",
  9.         ];
  10.  
  11.         $category = $woocommerce->get("products/categories", $paramsCategory);
  12.         if (count($category) == 0) {
  13.             $category = $woocommerce->post('products/categories', $data);
  14.             $categoryID = $category->id;
  15.         } else $categoryID = $category[0]->id;
  16.         return $categoryID;
  17.     }
  18.  
  19.     protected function getProductID($woocommerce, $sku)
  20.     {
  21.         $params = [
  22.             'sku' => "{$sku}"
  23.         ];
  24.  
  25.         $product = $woocommerce->get("products", $params);
  26.         if (count($product) == 0) return null; else {
  27.             return $product[0]->id;
  28.         }
  29.     }
  30.  
  31.     public function exporter()
  32.     {
  33.  
  34.         $woocommerce = new Client(
  35.             'http://domain/',
  36.             'ck_KEY',
  37.             'cs_KEY',
  38.             [
  39.                 'wp_api' => true,
  40.                 'version' => 'wc/v2',
  41.             ]
  42.         );
  43.  
  44.  
  45.         $caregories = [];
  46.  
  47.         $goods = Products::all();
  48.  
  49.         $progressBar = new ProgressBar(new ConsoleOutput(), count($goods));
  50.         $progressBar->setFormat('very_verbose');
  51.  
  52.  
  53.         foreach ($goods as $goodItem) {
  54.  
  55.             if (!key_exists($goodItem->category->name, $caregories)) {
  56.                 $caregories[$goodItem->category->name] = $this->getCategoryID($woocommerce, $goodItem->category->name);
  57.             }
  58.             $caregoryID = $caregories[$goodItem->category->name];
  59.             $productID = $this->getProductID($woocommerce, "{$goodItem->sku}-{$goodItem->id}");
  60.  
  61.             $priceRUB = $goodItem->price * 65;
  62.  
  63.             $data = [
  64.                 'name' => $goodItem->name,
  65.                 'sku' => "{$goodItem->sku}-{$goodItem->id}",
  66.                 'description' => $goodItem->description,
  67.                 "type" => "simple",
  68.                 "regular_price" => "{$priceRUB}",
  69.                 'categories' =>
  70.                     [
  71.                         [
  72.                             'id' => $caregoryID
  73.                         ],
  74.                     ],
  75.                 'stock_quantity' => $goodItem->quantity,
  76.  
  77.             ];
  78.             try {
  79.                 if (is_null($productID)) {
  80.                     $woocommerce->post('products', $data);
  81.                 } else {
  82.                     $woocommerce->put('products/' . $productID, $data);
  83.                 }
  84.             } catch (Exception $e) {
  85.             }
  86.             $progressBar->advance();
  87.         }
  88.         $progressBar->finish();
  89.  
  90.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement