0

Hello I would like to set the Description for magento products using data collected from Amazon api. I am calling the API and would like to get the xml response into magento description. I have some code prepared which works:

<?php
 require('../AmazonApi.php');
 require_once ('../app/Mage.php');
 Mage::app();

 //Create API access object
 $public_key = '*************';
 $secret_key = '*************';
 $associate_tag = '**********-21';
 $amazon_api = new AmazonAPI($public_key, $secret_key, $associate_tag);


$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('asin')
->addAttributeToSelect('description');

//Array of request parameters
foreach($products as $product)
{
$asin = $product->getAsin();
$product->setDescription($response);
$product->getResource()->saveAttribute($product, 'description');
$params_array = array(
'Operation' => 'ItemLookup',
'IdType' => 'ASIN',
'ItemId' => $asin ,
'ResponseGroup' => 'Tracks');

// returns a list of items for the search query 'Slow Magic'
$response = $amazon_api->sendRequest($params_array);

foreach ($response as $restponse)
{
sleep(1);
}

echo '<pre>';
print_r($response);
echo '</pre>';
}

foreach($parsed_xml->OperationRequest->Errors->Error as $error){
 echo "Error code: " . $error->Code . "\r\n";
 echo $error->Message . "\r\n";
 echo "\r\n";
 }

The code is correct the only problem with is the response, the error I am getting in the logs is:

Object of class stdClass could not be converted to string Object of class stdClass could not be converted to string.

Any how to convert to a string?

6
  • 2
    You have a typo in your code ->addAttributeToSeleXct('asin') where it should be ->addAttributeToSelect('asin') Commented Feb 10, 2016 at 13:49
  • Thank you , I've must of typed it in as I was copying the code over Commented Feb 10, 2016 at 13:54
  • Just change Mage::app(); to Mage::app('admin'); and check what happen!
    – Amit Bera
    Commented Feb 10, 2016 at 13:58
  • Everything stays the same after I've placed 'admin' . Still no change in product Commented Feb 10, 2016 at 14:03
  • Can u please tell why u use ->load($productId);
    – Amit Bera
    Commented Feb 10, 2016 at 14:19

2 Answers 2

2

Try to use instead

$product->setDescription($response);
$product->save();

to this code

 $product->setDescription($response);
 $product->getResource()->saveAttribute($product, 'description')
3
  • Zmage,Good answer
    – Amit Bera
    Commented Feb 10, 2016 at 14:16
  • Unfortunately , there is no difference in the product description . However the code looks more correct. Commented Feb 10, 2016 at 14:25
  • This is the error that I am getting in the logs : Recoverable Error: Object of class stdClass could not be converted to string in /var/www/html/lib/Varien/Db/Adapter/Pdo/Mysql.php on line 3074 Commented Feb 10, 2016 at 14:51
0

The real problem here is that you're trying to load a collection like if it was a single product:

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('asin')
->addAttributeToSelect('description')
->load($productId);

You do not need to do so, simply do:

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('asin')
->addAttributeToSelect('description');
2
  • Thank you however this doesn't solve the problem as the products are not being updated. Commented Feb 10, 2016 at 13:56
  • Could try adding Mage::log($product->getData()); at the start and the end inside your foreach loop and compare both results please (paste them in your questio if necessary) Commented Feb 10, 2016 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.