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 attributesMappingAfter() function.

Setting short_description attribute of the product

<?php

class WP_Amazonimportproductsaddon_Model_Observers
{
    public function attributesMappingAfter(Varien_Event_Observer $observer) {

        /*
         * $processingType possible values:
         * - WP_Amazonimportproducts_Model_Observer::PRODUCT_PROCESSING_TYPE_CREATE
         * - WP_Amazonimportproducts_Model_Observer::PRODUCT_PROCESSING_TYPE_UPDATE
         * 
         * $product->setUpdateAttributes(array) is used to alter the list of attributes to update. Most attributes can be updated this way.
         * The index status would also be updated if you change attributes using this function.
         * 
         * 
         *
         */

        $product = $observer->getData('product');
        $apiResponseItemData = $product->getItem();
        $processingType = $observer->getData('processingType');
        $_product = Mage::getModel('catalog/product')->load($product->getId());
        $configImport = Mage::helper('amazonimportproducts')->getConfigParam('import_params');

        $attributesToUpdate = array();
		if(isset($apiResponseItemData->ItemAttributes->Feature) && is_array($apiResponseItemData->ItemAttributes->Feature) && count($apiResponseItemData->ItemAttributes->Feature)) {
			$shortDescription = "
  • "; $shortDescription .= implode("
  • \r\n
  • ", $apiResponseItemData->ItemAttributes->Feature); $shortDescription .= "
"; $attributesToUpdate['short_description'] = $shortDescription; } if (count($attributesToUpdate)) { // right function to add attributes, this function checks if the value has changed and saves the data only if it is required. $product->setAttributesToUpdate($attributesToUpdate); } 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 attributesMappingAfter() after this line $attributesToUpdate = array();

          
          // 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
                 $attributesToUpdate['price'] = listPrice;
                 $attributesToUpdate['special_price'] = special_price;
		     }
		     else
		     {
		     	// we have to make sure that the special price is now removed
                $attributesToUpdate['special_price'] = '';
		     }
         }
         else
         {
		     	// we have to make sure that the special price is now removed
                $attributesToUpdate['special_price'] = '';
         }

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)) {  

        	$newSku = $apiResponseItemData->ItemAttributes->Model;
			$attributesToUpdate['sku'] = $newSku;
		
}         

Go to contentsBack to previous page