Contents


How to generate any Magento product attribute from data delivered by the Amazon API

This feature allows to prepare Magento product attributes before saving during import (synchronization) process from Amazon API. You can take any data delivered by the Amazon API and generate any product Attribute (Size, Brand, Color, Weight, Label, Material, etc.) from it. For example, apply automatic translation or unification routine to a product description, etc.

Please do the following actions:

Open file / app / code / local / WP / Amazonimportproductsaddon / Model / Observers.php

Place required code inside the process() function.

Setting Size attribute of the product

<?php

class WP_Amazonimportproductsaddon_Model_Observers
{
    public function processing(Varien_Event_Observer $observer)
    {
        $product                = $observer->getData('product');
        $apiResponseItemData    = $observer->getData('apiResponseItemData');
        $processingType         = $observer->getData('processingType');

        /**
        * $processingType possible values:
        * - WP_Amazonimportproducts_Model_Observer::PRODUCT_PROCESSING_TYPE_CREATE
        * - WP_Amazonimportproducts_Model_Observer::PRODUCT_PROCESSING_TYPE_UPDATE
        *
        */

        // --- uncomment this string in order to log data, you can find log the file here var/log/amazonimportproductsaddon.log ---
        #Mage::log(array($product->getId(), $processingType, $apiResponseItemData), null, 'amazonimportproductsaddon.log');

        /**
         * Size attribute generation example
	 * Size  = (Width + Height + Length) / 100
         * For US locale the size would be delivered in inches. Check size dimensions for clarification.
         */
        
	// taking size values from Amazon API response
        $size = ($apiResponseItemData->ItemAttributes->ItemDimensions->Width->_
        + $apiResponseItemData->ItemAttributes->ItemDimensions->Height->_
        + $apiResponseItemData->ItemAttributes->ItemDimensions->Length->_) / 100;
        
	// setting Size attribute for the product
        Mage::getSingleton('catalog/product_action')
            ->updateAttributes(array($product->getId()), array('size' => $size), Mage_Core_Model_App::ADMIN_STORE_ID);

        return $this;
    }
}

Shipping weight

There is a package weight attribute delivered by the API and it can be mapped to proper product attribute. But it is different from weight and shipping modules should be able to take this weight attribute in calculations. I guess the easiest way is to map package weight to product weight.

It is in the System->Config->Amazon Import-> Map attributes section, you can map almost any attribute there.

Expression with Amazon API Attribute:
{{var attr="PackageDimensions/Weight/_"}}

Recount the shipping weight in ounces into pounds

A: You can use math equations like
[{{var attr="weight"}} * 16] - this would give ounces if the weight is in pounds.


Custom php function

You can alter the custom php function in this file: app/code/local/WP/Amazonimportproductsaddon/Model/Observers.php

If you do not have that file, get to your original extension package and upload amazonimportproductsaddon.zip file content to the root of your magento folder.

Open the file Observers.php and find the function attributesMappingAfter()

Replace the function with your code.

Example: implement Special Price if there is a price and list price attributes in the Amazon API response.

Many products available through Affiliate Amazon API has the ListPrice attribute which is usually higher than the actual Price. Which means the product is sold with a discount.

And you can use this feature to create Price and Special Price in Magento.

We would check the example product ASIN B00NBOYRNS.

This is the Sony Xperia Z3 smartphone.

So, we search the product by ASIN, set Default Condition to New value, Default price to Lowest available.

After the product is found, click Edit button for this product and get to the Additional Attributes tab just to make sure that the attribute {{var attr="ListPrice/Amount"}} is there.

It is there:

Magento Amazon extension - attribute ListPrice/Amoun

Code for custom php function

    public function attributesMappingAfter(Varien_Event_Observer $observer)
    {
     
        $product                = $observer->getData('product');
        $apiResponseItemData    = $observer->getData('apiResponseItemData');
        $processingType         = $observer->getData('processingType');
       


         $_product = Mage::getModel('catalog/product')->load($product->getId());

         // Product is already created and has the price attribute assigned according to the offer found.
         $actualPrice = number_format($_product->getPrice(),2);         
         
         if (isset($apiResponseItemData->ItemAttributes->ListPrice)) {  
             // we take the list price attribute from the response API

             $listPrice = ($apiResponseItemData->ItemAttributes->ListPrice->Amount) / 100;
          
             if ( $actualPrice < $listPrice ) {
                 // If actual price is lower than the list price from the API we can set Special Price in magento
				 //Set new price
			     //Set new special_price
			     Mage::getSingleton('catalog/product_action')
	                 ->updateAttributes(array($product->getId()), array('price' => $listPrice, 'special_price' => $actualPrice), Mage_Core_Model_App::ADMIN_STORE_ID); 
		     }
		     else
		     {
		     	// we have to make sure that the special price is now removed
			     Mage::getSingleton('catalog/product_action')
	                 ->updateAttributes(array($product->getId()), array('special_price' => ''), Mage_Core_Model_App::ADMIN_STORE_ID); 
		     }
         }
         else
         {
		     	// we have to make sure that the special price is now removed
			     Mage::getSingleton('catalog/product_action')
	                 ->updateAttributes(array($product->getId()), array('special_price' => ''), Mage_Core_Model_App::ADMIN_STORE_ID); 
         }
      
        return $this;
    }

Check the code carefully, how did we get the ListPrice value from the API:

$listPrice = ($apiResponseItemData->ItemAttributes->ListPrice->Amount) / 100;

Remember the attribute value:

{{var attr="ListPrice/Amount"}}, it was converted to ListPrice->Amount. We need now to add $apiResponseItemData->ItemAttributes-> before the attribute 

and get

$apiResponseItemData->ItemAttributes->ListPrice->Amount

Since the value was 62999, and there was the attribute {{var attr="ListPrice/FormattedPrice"}} near by with value of 629.99, we might figure out, that we need to divide the listPrice value by 100 in order to get the right price.

Example: change SKU using any attribute delivered by the Amazon API

Changing SKU is quite simple. Basic code would set Model from Amazon API to the product SKU:

Code for custom php function:

if (isset($apiResponseItemData->ItemAttributes->Model)) {  

        	$sku = $_product->getSku();
        	$newSku = $apiResponseItemData->ItemAttributes->Model;

        	if($sku != $newSku)
        	{
        		$_product->setSku($newSku)->save();
        	}
			
}         

Example: alter description with Features options

Amazon API sometimes retreive array data which can not be used in the attributes mapping, it is too complex to use arrays in a simple attributes mapping functionality. It is possible to use array data in the php function.

That is the array data we are talking about, you can find them in the Additional attributes tab

Magento Amazon extension - Additional attributes tab

Many products over Amazon keep empty description with only those Features fields filled.

Here is the code to place features to the description:

		if(isset($apiResponseItemData->ItemAttributes->Feature) && is_array($apiResponseItemData->ItemAttributes->Feature) && count($apiResponseItemData->ItemAttributes->Feature))	{

			$html = "<ul><li>";
			$html .= implode("</li>\r\n<li>", $apiResponseItemData->ItemAttributes->Feature);
			$html .= "</li></ul>";
			
			Mage::getSingleton('catalog/product_action')
                 ->updateAttributes(array($product->getId()), array('description' => $html), Mage_Core_Model_App::ADMIN_STORE_ID); 
		}

In this code we define which attribute to change by placing the attribute code to this line

->updateAttributes(array($product->getId()), array('description' => $html), Mage_Core_Model_App::ADMIN_STORE_ID);

You can easily replace it to short_description or any other data attribute you want to use.