Main menu
WalkswithMeAPI PHPGoogle Content API for Shopping V1.6

Google Content API for Shopping V1.6

Google Content API for shopping provides a much better options over the XML Feed or Excel sheet on Google Merchant center. When we compare to XML feed a single update to the product is need to update the entire file and it gives much load to the user.

Content API for shopping is playing an important role here, without creating and submitting a feed to Google Merchant Center we can setup once rest of the things doing on your shopping cart store backend codes. There are many plugins available for adding your products to Google store using Feed XML method, its complicated and more step process. So a simple Content API for shopping enables you to do the things on your store easily.

This article is aimed on Google Content API for shopping version 1.6 , now V1.6 is depreciated but available for few years. Google already provided an upgraded version 2 for content API .

This article will explain all main features of adding/editing/deleting of your Google store products.

For using the Google Content API for shopping you should have a Google merchant account and online store.

Official PHP code library can be found here and the documentation can be found here

Google Content API for Shopping

Google Content API for Shopping

Once you signup with Google Merchant center you will get a Merchant ID first you have to authenticate the API with Google.


require_once('GShoppingContent.php');
$creds = Credentials::get();
// Create a client for our merchant and log in
$client = new GSC_Client($creds["merchantId"]);
$client->login($creds["email"], $creds["password"]);
class Credentials {
public static function get() {
return array(
"merchantId" => "xxxxx",
"email" => "test@gmail.com",
"password" => "pass",
);
}
}

Adding a single product to Google store using Content API for shopping.


$product = new GSC_Product();
$product->setTitle('Camera');
$product->setDescription('A great compact body to make it easy to get a great shot everytime.');
$link = 'https://www.walkswithme.net/product-url';
$product->setProductLink($link);
$product->setSKU('123456');
$product->setImageLink('https://www.walkswithme.net/product-image.jpg');
$product->setTargetCountry('US');
$product->setContentLanguage('en');
$product->setBrand('Acme');
$product->setCondition('new');
$product->setGtin('00013803105384');
$product->setColor('black');
$product->setAvailability('in stock');
$productType = 'Cameras & Optics > Cameras > Digital Cameras';
$product->setProductType($productType);
$product->setPrice('25.00', 'usd');
$product->setShippingWeight('0.1', 'lb');
$country = 'US';
$region = 'MA';
$price = '5.95';
$priceUnit = 'usd';
$service = 'Speedy Shipping - Ground';
$product->addShipping($country, $region, $price, $priceUnit, $service);
$country = 'US';
$region = 'CA';
$rate = '8.25';
$ship = 'true';
$product->addTax($country, $region, $rate, $ship);
$entry = $client->insertProduct($product);
echo('Inserted: ' . $entry->getTitle() . "\n");

Edit a single product using Content API for shopping.


$id = '123456';
$country = 'US';
$language = 'en';
$retrievedProduct = $client->getProduct($id, $country, $language);
$retrievedProduct->setPrice("200.50", "usd");
$updatedEntry = $client->updateProduct($retrievedProduct);
echo 'Updated: ' . $updatedEntry->getTitle() . "\n";
echo 'Price: ' . $updatedEntry->getPrice() . "\n\n";

Remove a single product using Content API for shopping.


$id = '123457';
$country = 'US';
$language = 'en';
$proudct = $client->getProduct($id, $country, $language);
$res = $client->deleteProduct($proudct);
echo "Delete succeeded\n";

The Google Content API for shopping allows to process in batch you can add/edit/remove more than one products in a single request. It will reduce the server load.

Adding Products using batch mode in Content API for shopping.


$batch = new GSC_ProductList();
$product->setTitle('Camera -1 ');
$product->setDescription('A great compact body to make it easy to get a great shot everytime.');
$link = 'https://www.walkswithme.net/product-url';
$product->setProductLink($link);
$product->setSKU('123457');
$product->setImageLink('https://www.walkswithme.net/image.jpg');
$product->setTargetCountry('US');
$product->setContentLanguage('en');
$product->setBrand('Acme');
$product->setCondition('new');
$product->setGtin('00013803105384');
$product->setColor('black');
$product->setAvailability('in stock');
$productType = 'Cameras & Optics > Cameras > Digital Cameras';
$product->setProductType($productType);
$product->setPrice('30.00', 'usd');
$product->setShippingWeight('0.1', 'lb');
$country = 'US';
$region = 'MA';
$price = '5.95';
$priceUnit = 'usd';
$service = 'Speedy Shipping - Ground';
$product->addShipping($country, $region, $price, $priceUnit, $service);
$country = 'US';
$region = 'CA';
$rate = '8.25';
$ship = 'true';
$product->addTax($country, $region, $rate, $ship);
$product->setBatchOperation("insert");
$batch->addEntry($product);
$product->setTitle('Camera -2 ');
$product->setDescription('A great compact body to make it easy to get a great shot everytime.');
$link = 'https://www.walkswithme.net/product-url-1';
$product->setProductLink($link);
$product->setSKU('123458');
$product->setImageLink('https://www.walkswithme.net/image.jpg');
$product->setTargetCountry('US');
$product->setContentLanguage('en');
$product->setBrand('Acme');
$product->setCondition('new');
$product->setGtin('00013803105384');
$product->setColor('black');
$product->setAvailability('in stock');
$productType = 'Cameras & Optics > Cameras > Digital Cameras';
$product->setProductType($productType);
$product->setPrice('40.00', 'usd');
$product->setShippingWeight('0.1', 'lb');
$country = 'US';
$region = 'MA';
$price = '5.95';
$priceUnit = 'usd';
$service = 'Speedy Shipping - Ground';
$product->addShipping($country, $region, $price, $priceUnit, $service);
$country = 'US';
$region = 'CA';
$rate = '8.25';
$ship = 'true';
$product->addTax($country, $region, $rate, $ship);
$product->setBatchOperation("insert");
$batch->addEntry($product);
// Finally send the data to the API
$feed = $client->batch($batch);
$products = $feed->getProducts();
$operation = $products[0];
echo('Inserted: ' . $operation->getTitle() . "\n");
echo('Status: ' . $operation->getBatchStatus() . "\n");

Multiple Product update using batch mode in Content API for shopping.


$batch = new GSC_ProductList();
$product = new GSC_Product();
$product    =    $client->getProduct('123457', 'US', 'en');
$product->setPrice("500", "usd");
$product->setBatchOperation("update");
$batch->addEntry($product);
$product    =    $client->getProduct('123458', 'US', 'en');
$product->setPrice("300", "usd");
$product->setBatchOperation("update");
$batch->addEntry($product);
// Finally send the data to the API
$feed = $client->batch($batch);
$products = $feed->getProducts();
echo '<pre/>';
print_r($products);

Multiple Product Removal using batch mode in Content API for shopping.


$batch = new GSC_ProductList();
$product = new GSC_Product();
$product    =    $client->getProduct('123457', 'US', 'en');
$product->setBatchId('123457');
$product->setBatchOperation("delete");
$batch->addEntry($product);
$product    =    $client->getProduct('123458', 'US', 'en');
$product->setBatchOperation("delete");
$product->setBatchId('123458');
$batch->addEntry($product);
// Finally send the data to the API
$feed = $client->batch($batch);

Google Content API for shopping will help you guys to create an extension for Virtuemart,Woocommerce or magneto or any other cart. stay tune to get Content API for shopping version 2 with Oauth method.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

FacebookTwitterGoogle+RSS