运行产品导入配置文件后,Magento观察者不会触发

时间:2013-04-24 09:16:24

标签: magento events magento-1.7

我创建了一个名为“catalog_product_import_profile_after”的新自定义事件。产品导入运行配置文件完成过程后,此事件调用。如果我为2个产品运行配置文件,那么它工作正常并触发自定义事件,但是当我为数千个产品运行相同的配置文件时,它的工作和自定义事件不会被触发。

以下是observer.php文件的代码 -

<?php
class GWB_ClearOrphan_Model_Observer
{

            public function disableProducts(Varien_Event_Observer $observer)
            {

                try{    
                    $collection = Mage::getModel('catalog/product')->getCollection()
                            ->addAttributeToSelect('sku')
                            ->addAttributeToFilter('attribute_set_id',9)
                            ->load();
                }
                catch(Exception $e) {
                    //Mage::log($e->getMessage(), null, 'collection.log');
                }

                try{
                    $resource = Mage::getModel('core/resource');
                    $readConnection = $resource->getConnection('core_read');
                    $writeConnection = $resource->getConnection('core_write');
                }
                catch(Exception $e) {
                    //Mage::log($e->getMessage(), null, 'developer.log');
                }

                foreach($collection as $val) {
                    $sku = $val->getSku();

                    $query = "SELECT * FROM feed_products WHERE feed_sku='".$sku."'";
                    $results = $readConnection->fetchAll($query);
                    $feed_sku = $results[0]['feed_sku'];

                    if($feed_sku != ''){
                        $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
                        $product->setStatus(2);
                        try {
                            $product->save();
                            //$product->delete();
                        }
                        catch (Exception $e) {
                            Mage::log($e->getMessage(), null, 'product_disable.log');
                        }   
                    }               
                }

                try{
                    $writeConnection->query("TRUNCATE TABLE feed_products");
                } 
                catch(Exception $e){
                    echo $e;
                }
            }

}

?>

我在“ProfileController.php”文件下注册了这个事件,这里是这个函数的代码---

<?php

    public function batchFinishAction()
        {   
            $batchId = $this->getRequest()->getParam('id');
            if ($batchId) {
                $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
                /* @var $batchModel Mage_Dataflow_Model_Batch */

                if ($batchModel->getId()) {
                    $result = array();
                    try {
                        $batchModel->beforeFinish();
                    } catch (Mage_Core_Exception $e) {
                        $result['error'] = $e->getMessage();
                    } catch (Exception $e) {
                        $result['error'] = Mage::helper('adminhtml')->__('An error occurred while finishing process. Please refresh the cache');
                    }

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

                    $batchModel->delete();
                    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                }
            }
        }

?>

1 个答案:

答案 0 :(得分:0)

这取决于您确认事件的确认。 这可能是您从后端或cli运行批处理并在“前端”节点中触发事件。在这里,最好在全局节点中描述此事件的配置。