Wednesday, 5 June 2013

Import tier price through csv in magento

1. Create tierprice.csv in which there are Colums like  sku,price_General_2 (here genereal is customergroup
& 2 is qty),price_General_5,price_Wholesale_2  having value 
1112,200.99,425.99,150   

2.Create a custom module for import tierprice csv
create file in app/etc /modules     /Plumtree_Tierprice.xml  in it:
<?xml version="1.0"?>
<config>
    <modules>
        <Plumtree_Tierprice>
            <active>true</active>
            <codePool>local</codePool>
        </Plumtree_Tierprice>
    </modules>
</config>

3.Create modules Config.xml .App/code/local/Plumtree/Tierprice/etc/config.xml in it:
<?xml version="1.0"?>
<config>
  <modules>
        <Plumtree_Tierprice>
            <version>0.1.0</version>
        </Plumtree_Tierprice>
    </modules>
     <global>
        <models>
            <catalog>
                <rewrite>
                   

<convert_adapter_product>Plumtree_Tierprice_Catalog_Model_Convert_Adapter_Product</convert_adapter_product>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

4. now create model file in app/code/local/Plumtree/Tierprice/Catalog/Model/Convert/Adapter/Product.php    in

it:
<?php
class Plumtree_Tierprice_Catalog_Model_Convert_Adapter_Product extends

Mage_Catalog_Model_Convert_Adapter_Product
{
   
        private $_group_list = null;
        private $_tier_price_fields = null;
       
        public function load() {
           // load the group list
           $this->_group_list = Mage::getResourceModel('customer/group_collection')-

>setRealGroupsFilter()->loadData()->toOptionArray();
          
            // call the parent load
            return parent::load();
        }
       
    public function saveRow(array $importData)
    {
        // doing normal import...
        parent::saveRow($importData);
       
        if (!is_array($this->_group_list)) {
            $this->_group_list = Mage::getResourceModel('customer/group_collection')-

>setRealGroupsFilter()->loadData()->toOptionArray();
        }
       
        // is there a tier price field? (check this only the first time)
       
        if (!is_array($this->_tier_price_fields)) {
           
            $this->_tier_price_fields = array();
           
            foreach ($importData as $k=>$v) {
                $matches = array();
                if (preg_match('/^price\_([^_]+)\_?([0-9]+)?$/', $k, $matches)) {
                    // found a valid field. Check the group name and quantity
                   
                    $foundvalid = false;
                    foreach ($this->_group_list as $group) {
                        if (strtolower($group['label']) == strtolower($matches[1])) {
                            $foundvalid = true;
                            if (isset($matches[2])) $q = (int)$matches[2]; else $q = 1;
                            $this->_tier_price_fields[$k] = array('id_group'=>$group

['value'], 'quantity'=>$q);
                            break;
                        }
                    }
                   
                    if (!$foundvalid) {
                        // group not found!
                        // can't call exceptions here?
                        //$message = Mage::helper('catalog')->__('Customer group "%s" for

tier price not found', $matches[1]);
                          //    Mage::throwException($message,

Varien_Convert_Exception::NOTICE);
                    }
                   
                } /* end if */
            }
           
        }
       
        if (!count($this->_tier_price_fields)) return true; // no tier prices found
       
       
      // fetch the store object
      if (empty($importData['store'])) {
                if (!is_null($this->getBatchParams('store'))) {
                  $store = $this->getStoreById($this->getBatchParams('store'));
                } else {
                    // can't call exceptions here?
                  //$message = Mage::helper('catalog')->__('Skip import row, required field

"%s" not defined', 'store');
                  //Mage::throwException($message);
                }
            }    else {
                $store = $this->getStoreByCode($importData['store']);
            }
           
            // create the product object
      $product = $this->getProductModel()->reset();
      $product->setStoreId($store->getId());
      $productId = $product->getIdBySku($importData['sku']);
      $storeId = $store->getId();
     
            if ($productId) {
              $product->load($productId);
               
                $tierPrices = $product->tier_price;
               
                foreach ($this->_tier_price_fields as $tier_key=>$imported_tier_price) {
                    // should i update an existing tier price?
                    foreach ($tierPrices as $ktp=>$tp) {
                        if ($tp['website_id'] != $storeId) continue;
                        if ($tp['cust_group'] != $imported_tier_price['id_group'])

continue;
                        if ($tp['all_groups'] != 0) continue;
                        if ($tp['price_qty'] != $imported_tier_price['quantity'])

continue;
                       
                        // it matches this existing tier price. I remove it
                        unset($tierPrices[$ktp]);
                    }
                   
                    // now i add the imported tier_price
                    if ($importData[$tier_key]) {
                        $tierPrices[] = array(
                            'website_id'  => $storeId,
                            'cust_group'  => $imported_tier_price['id_group'],
                  'all_groups'  => 0,
                            'price_qty'   => number_format

($imported_tier_price['quantity'], 4, '.', ''),
                            'price'       => number_format($importData

[$tier_key], 4, '.','')
                        );
                    }
                   
                   
                }
               
                $product->tier_price = $tierPrices;
               
                // Save you product with all tier prices
                $product->save();
            }
       
        return true;
    }
}
  that's it. now Import csv through data flow Advanced profile.
                                                                                                                                                  

1 comment:

  1. Thank for sharing. But those who now to technical and are not familiar with coding might need some alternative solution. Here is the article on how to import tier prices to Magento using extension -
    http://www.mag-manager.com/useful-articles/how-to/how-to-import-magento-tier-prices

    ReplyDelete