سؤال

how can i catch import product Mage_Catalog_Model_Convert_Adapter_Product model saverow() event using observer.

Mage::dispatchEvent('catalog_product_import_profile_after', array('adapter'=>$this));

i use this one but not getting success.

This is my code: in my \app\code\community\Compnyname\ImportProduct\controllers\Adminhtml\RunProfileController.php

class Compnyname_ImportProduct_Adminhtml_RunProfileController extends Mage_Adminhtml_Controller_action
{
    protected function _initAction() 
    {
        .....
    } 
    public function indexAction() 
    {
         ........
    }
    public function runAction()
    {   
        ...........

    }


    public function batchRunAction()
    {   

        $currentUrl = Mage::helper('core/url')->getCurrentUrl();
        $ids = explode("/", $currentUrl);
        $id=$ids[11];
        $file=explode("?",$id);
        $file_name =  $file[0];
        $path = Mage::getBaseDir('var') . DS . 'import' . DS ;
        $filepath =$path.$file_name;
        $header=array();
        $header1=array();
        $i = 0;
        if(($handle = fopen("$filepath", "r")) !== FALSE) 
        {   
            while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
            {       
                if($i==0)
                {
                     $header1[$i]=$data;                    
                }  
                else
                {
                     $header[$i]=$data;
                }
                $i++;    
            }
            $array1=array();
            $array2=array();
            for($kd=1;$kd<=count($header);$kd++)
            {
                $jk=0;
                foreach ($header1[0] as $key => $value)
                {   
                    $array1[$value]=$header[$kd][$jk];
                    $jk++;
                }

                 $this->updateData($array1);
            }
        }

        return true;
    }
    public function updateData($data)
    {
        //$importData=$data;

        $collection = Mage::getModel('importproduct/convert_adapter_product')->saveRow($data);
        if($collection)
        {
          $this->batchFinish();
        }else{
          $this->batchError();
        }
        //$j = $j + 1;
        //return "hello";
    }   

    public function batchFinish()
    {
        //$batchId = $this->getRequest()->getParam('id');

        Mage::dispatchEvent('catalog_product_import_profile_after', array('adapter'=>$this));
        //Mage::dispatchEvent('catalog_product_prepare_save', array('adapter'=>$this));
        //catalog_product_prepare_save
    //echo "hello".$var;
        echo "batchfinish....";

    }
    public function batchError()
    {
    echo "error";
    } 

    function testAction()
    {
    echo "testing...";
    }
}

?>

app\code\community\Compnyname\ImportProduct\Model\Convert\Adapter\product.php

class Compmyname_ImportProduct_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
{
 .....
  public function saveRow(array $importData)
  {
    code for save raw
  }
......
}

I want to call observer during import product process is running and observer call after when one row is import. For example if in csv there is 500 product then observer call 500 time.

Please help to solve this problem.

هل كانت مفيدة؟

المحلول

It looks as if you're rewriting the method Mage_Catalog_Model_Convert_Adapter_Product - add your own dispatch in there:

class Compmyname_ImportProduct_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
{

  public function saveRow(array $importData)
  {
    //do the work from the parent class
    parent::saveRow($importData);
    //dispatch your own event
    Mage::dispatchEvent('convert_adapter_product_save_row_after', array('import_data'=>$importData, 'product'=>$this));

  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top