如何使用观察者捕获导入产品mage_catalog_model_model_convert_adapter_adapter_product_product_product_product_product saverow()事件。

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

我使用这个,但没有成功。

这是我的代码:在我的 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
  }
......
}

我想在导入产品过程中在运行过程中打电话给观察者,并在一行导入后呼叫观察者。例如,如果在CSV中有500个产品,则观察者调用500个时间。

请帮助解决这个问题。

有帮助吗?

解决方案

看起来好像您正在重写该方法 Mage_Catalog_Model_Convert_Adapter_Product - 在其中添加自己的调度:

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归因
scroll top