2

Hello I would like to set the Description for magento products using data collected from Amazon api. I am calling the API and receiving the response however in the logs I get :

Recoverable Error: Object of class stdClass could not be converted to string

The question is how to parse the information into a string so it can be used within magento product details?

<?php
require_once '../abstract.php';
require('AmazonApi.php');

class Mage_Shell_Amazon extends Mage_Shell_Abstract
{


public function run() {

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


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

    //Array of request parameters
    foreach($products as $prod)
    {
        //load the actual products data
        $product = Mage::getModel('catalog/product')->load($prod->getId());

        $asin = $product->getAsin();

        $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);

        $product->setDescription($restponse);
        $product->getResource()->saveAttribute($product, 'description');

        foreach ($response as $restponse)
        {
            sleep(1);
        }
        echo '<pre>';
        print_r($restponse);
        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";
     //        }
       }
     }

        $amazonConnector = new Mage_Shell_Amazon();
        $amazonConnector->run();

Sample from Amazon response for one of the products :

[Items] => stdClass Object
    (
        [Request] => stdClass Object
            (
                [IsValid] => True
                [ItemLookupRequest] => stdClass Object
                    (
                        [IdType] => ASIN
                        [ItemId] => B000002OGL
                        [ResponseGroup] => Tracks
                        [VariationPage] => All
                    )

            )

        [Item] => stdClass Object
            (
                [ASIN] => B000002OGL
                [Tracks] => stdClass Object
                    (
                        [Disc] => stdClass Object
                            (
                                [Track] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [_] => Mustang Sally
                                                [Number] => 1
                                            )

                                        [1] => stdClass Object
                                            (
                                                [_] => Take Me To The River
                                                [Number] => 2
                                            )

                                        [2] => stdClass Object
                                            (
                                                [_] => Chain Of Fools
                                                [Number] => 3
                                            )

                                        [3] => stdClass Object
                                            (
                                                [_] => The Dark End Of The Street
                                                [Number] => 4
                                            )

                                        [4] => stdClass Object
                                            (
                                                [_] => Destination: Anywhere
                                                [Number] => 5
                                            )

                                        [5] => stdClass Object
                                            (
                                                [_] => I Can't Stand The Rain
                                                [Number] => 6
                                            )

                                        [6] => stdClass Object
                                            (
                                                [_] => Try A Little Tenderness
                                                [Number] => 7
                                            )

                                        [7] => stdClass Object
                                            (
                                                [_] => Treat Me Right
                                                [Number] => 8
                                            )

                                        [8] => stdClass Object
                                            (
                                                [_] => Do Right Woman Do Right Man
                                                [Number] => 9
                                            )

                                        [9] => stdClass Object
                                            (
                                                [_] => Mr. Pitiful
                                                [Number] => 10
                                            )

                                        [10] => stdClass Object
                                            (
                                                [_] => I Never Loved A Man
                                                [Number] => 11
                                            )

                                        [11] => stdClass Object
                                            (
                                                [_] => In The Midnight Hour
                                                [Number] => 12
                                            )

                                        [12] => stdClass Object
                                            (
                                                [_] => Bye Bye Baby
                                                [Number] => 13
                                            )

                                        [13] => stdClass Object
                                            (
                                                [_] => Slip Away
                                                [Number] => 14
                                            )

                                    )

                                [Number] => 1
                            )
                    )
            )
    )
  )

1 Answer 1

1

I'm not sure on specifics with the Amazon API, so the first thing I would do would research on Amazon's documentation on how to get a string description.

If not, looking at that result, the description is structured data. For example, in this case it's a list of tracks and an ID. If you NEED to get a description from that you can first convert the stdClass into an array using:

json_decode(json_encode($item), true);

And then once that's an array, you could walk through it recursively and compile a string. If it was a one-dimensional array, you could simply use implode with a delimiter to join it together, however in this case it is a multi-dimensional array.

But again, I should re-iterate, this should be the LAST resort. Try as hard as you can to find the best practices for displaying descriptions from Amazon first.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately when I add this to my code it doesn't display anything. But thank you for your answer.
That code doesn't display anything, it only returns something. If you want it to display you have to echo it. You can assign it to a variable with $item = json_decode(json_encode($item), true))
My bad , as it doesn't display anything I've meant "NULL" which means that the JSON can't be decoded.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.