From d3a28940acfb722f1af61deae3df67b8bd740794 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 13 Feb 2021 11:17:03 +0100 Subject: [PATCH] batche update prices from CSV --- php/Products.php | 136 +++++++++++++++++++++++++++++++++++++++++++++++ php/script.php | 86 ++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 php/Products.php create mode 100644 php/script.php diff --git a/php/Products.php b/php/Products.php new file mode 100644 index 0000000..8bd7c5b --- /dev/null +++ b/php/Products.php @@ -0,0 +1,136 @@ +csvPrices = $s; + + + $dataAvailability = file_get_contents(self::AVAILABILITY_CSV_URL); + $rowsAvailability = explode("\n",$dataAvailability); + $s = array(); + foreach ($rowsAvailability as $rowAvailability) { + $valAvailability = str_getcsv($rowAvailability)[0]; + $val_arrAvailability = explode (";", $valAvailability); + if(isset($val_arrAvailability[0]) && isset($val_arrAvailability[1])){ + $s[$val_arrAvailability[0]] = $val_arrAvailability[1]; + } + } + $this->csvAvailabilities = $s; + + + + } + + public function run(){ + return 0; + } + public function getProduct($offerId, $contentLanguage, $targetCountry) { + $productId = $this->buildProductId($offerId, $contentLanguage, $targetCountry); + $product = $this->session->service->products->get($this->session->merchantId, $productId); + return $product; + } + + public function buildProductId($offerId, $contentLanguage, $targetCountry) { + return sprintf('%s:%s:%s:%s', self::CHANNEL, $contentLanguage, $targetCountry, $offerId); + } + + public function updateProduct(Google_Service_ShoppingContent_Product $product) { + $response = $this->session->service->products->insert($this->session->merchantId, $product); + return $response; + } + + public function getAllProducts(){ + $allProducts = []; + $parameters = ['maxResults' => self::BATCH_SIZE - 1]; + $products = $this->session->service->products->listProducts( + $this->session->merchantId, $parameters); + $count = 0; + while (!empty($products->getResources()) && $count++ < self::NUMBER_LOOPS) { + foreach ($products->getResources() as $product) { + $allProducts[] = $product; + } + if (empty($products->getNextPageToken())) { + break; + } + $parameters['pageToken'] = $products->nextPageToken; + $products = $this->session->service->products->listProducts( + $this->session->merchantId, $parameters); + } + return $allProducts; + } + public function insertProductBatch($products) { + $entries = []; + + foreach ($products as $key => $product) { + $entry = + new Google_Service_ShoppingContent_ProductsCustomBatchRequestEntry(); + $entry->setMethod('insert'); + $entry->setBatchId($key); + $entry->setProduct($product); + $entry->setMerchantId($this->session->merchantId); + + $entries[] = $entry; + } + + $batchRequest = + new Google_Service_ShoppingContent_ProductsCustomBatchRequest(); + $batchRequest->setEntries($entries); + + $batchResponse = + $this->session->service->products->custombatch($batchRequest); + + printf("Inserted %d products.\n", count($batchResponse->entries)); + + foreach ($batchResponse->entries as $entry) { + if (empty($entry->getErrors())) { + $product = $entry->getProduct(); + } else { + print ("There were errors inserting a product:\n"); + foreach ($entry->getErrors()->getErrors() as $error) { + printf("\t%s\n", $error->getMessage()); + } + } + } + } + + +} diff --git a/php/script.php b/php/script.php new file mode 100644 index 0000000..34c2235 --- /dev/null +++ b/php/script.php @@ -0,0 +1,86 @@ +getAllProducts(); +$i=1; +foreach ($allProducts as $key => $product) { + printf ("%s : %s \n", $i, $product->getOfferId()); + $i++; +} +*/ + +print_r($Products->csvAvailabilities); +$productsFMC = $Products->getAllProducts(); // Products from Merchant Center +$productsToUpdateSP = []; +$productsToUpdateP = []; +foreach ($productsFMC as $key => $product) { + $offerId = $product->getOfferId(); + if(isset($Products->csvPrices[$offerId])){ + $priceValue = $Products->csvPrices[$offerId]; + if($product->getSalePrice()){ + if($priceValue != $product->getSalePrice()->getValue()){ + $price = new Google_Service_ShoppingContent_Price(); + $price->setValue($priceValue); + $price->setCurrency($product->getSalePrice()->getCurrency()); + $product->setSalePrice($price); + unset($product->source); + $productsToUpdateSP[] = $product; + } + }else{ + if($priceValue != $product->getPrice()->getValue()){ + $price = new Google_Service_ShoppingContent_Price(); + $price->setValue($priceValue); + $price->setCurrency($product->getPrice()->getCurrency()); + $product->setPrice($price); + unset($product->source); + $productsToUpdateP[] = $product; + } + } + } +} + + +$i=1; +printf ("----------------- Products to update SP: -----------------\n"); +foreach($productsToUpdateSP as $productToUpdate){ + printf ("%s : %s : %s \n", $i, $productToUpdate->getOfferId(), $productToUpdate->getSalePrice()->getValue()); + $i++; +} +printf ("----------------- Products to update P: -----------------\n"); +foreach($productsToUpdateP as $productToUpdate){ + printf ("%s : %s : %s \n", $i, $productToUpdate->getOfferId(), $productToUpdate->getPrice()->getValue()); + $i++; +} + +$Products->insertProductBatch($productsToUpdateP); +$Products->insertProductBatch($productsToUpdateSP); +/* +$product = $Products->getProduct(27103, 'de', 'AT'); +print_r($product); +$product->setId($Products->buildProductId(27103, 'de', 'DE')); +$product->setTargetCountry('DE'); +$product->getShipping()[0]->setCountry('DE'); +$product->setLink(str_replace('86702', '86706', $product->getLink())); +unset($product->source); +print_r($Products->updateProduct($product)); + + +$product = $Products->getProduct(92210, 'de', 'AT'); +print_r($product); +$product->setId($Products->buildProductId(92210, 'de', 'DE')); +$product->setTargetCountry('DE'); +$product->getShipping()[0]->setCountry('DE'); +$product->setLink(str_replace('86702', '86706', $product->getLink())); +unset($product->source); +print_r($Products->updateProduct($product)); +*/ + +?>