Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batche update prices from CSV #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions php/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require_once 'BaseSample.php';

// Class for running through some example interactions with the
// Products service.
class Products extends BaseSample {

const CHANNEL = 'online';
const BATCH_SIZE = 200;
const NUMBER_LOOPS = 300;
const PRICE_CSV_URL = "https://feeds.pricio.de/subfeeds/preisupdate_f24_google.csv";
const AVAILABILITY_CSV_URL = "https://feeds.pricio.de/subfeeds/preisupdate_f24_google.csv";
public $csvPrices = [];
public $csvAvailabilities = [];

function __construct() {
parent::__construct();
$dataPrice = file_get_contents(self::PRICE_CSV_URL);
$rowsPrice = explode("\n",$dataPrice);
$s = array();
foreach ($rowsPrice as $rowPrice) {
$valPrice = str_getcsv($rowPrice)[0];
$val_arrPrice = explode (";", $valPrice);
if(isset($val_arrPrice[0]) && isset($val_arrPrice[1])){
$s[$val_arrPrice[0]] = $val_arrPrice[1];
}
}
$this->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());
}
}
}
}


}
86 changes: 86 additions & 0 deletions php/script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require_once 'Products.php';

$Products = new Products;

/*
$allProducts = $Products->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));
*/

?>