Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function getCategoryID($woocommerce, $categoryName)
- {
- $paramsCategory = [
- 'search' => "{$categoryName}"
- ];
- $data = [
- 'name' => "{$categoryName}",
- ];
- $category = $woocommerce->get("products/categories", $paramsCategory);
- if (count($category) == 0) {
- $category = $woocommerce->post('products/categories', $data);
- $categoryID = $category->id;
- } else $categoryID = $category[0]->id;
- return $categoryID;
- }
- protected function getProductID($woocommerce, $sku)
- {
- $params = [
- 'sku' => "{$sku}"
- ];
- $product = $woocommerce->get("products", $params);
- if (count($product) == 0) return null; else {
- return $product[0]->id;
- }
- }
- public function exporter()
- {
- $woocommerce = new Client(
- 'http://domain/',
- 'ck_KEY',
- 'cs_KEY',
- [
- 'wp_api' => true,
- 'version' => 'wc/v2',
- ]
- );
- $caregories = [];
- $goods = Products::all();
- $progressBar = new ProgressBar(new ConsoleOutput(), count($goods));
- $progressBar->setFormat('very_verbose');
- foreach ($goods as $goodItem) {
- if (!key_exists($goodItem->category->name, $caregories)) {
- $caregories[$goodItem->category->name] = $this->getCategoryID($woocommerce, $goodItem->category->name);
- }
- $caregoryID = $caregories[$goodItem->category->name];
- $productID = $this->getProductID($woocommerce, "{$goodItem->sku}-{$goodItem->id}");
- $priceRUB = $goodItem->price * 65;
- $data = [
- 'name' => $goodItem->name,
- 'sku' => "{$goodItem->sku}-{$goodItem->id}",
- 'description' => $goodItem->description,
- "type" => "simple",
- "regular_price" => "{$priceRUB}",
- 'categories' =>
- [
- [
- 'id' => $caregoryID
- ],
- ],
- 'stock_quantity' => $goodItem->quantity,
- ];
- try {
- if (is_null($productID)) {
- $woocommerce->post('products', $data);
- } else {
- $woocommerce->put('products/' . $productID, $data);
- }
- } catch (Exception $e) {
- }
- $progressBar->advance();
- }
- $progressBar->finish();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement