我正在尝试构建一个小模块,该模块容纳了一个观察者,以便被打电话给何时和事件。 catalog_product_import_profile_after 被解雇了这是我使用的自定义事件

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

在里面 app/code/core/Mage/Adminhtml/controllers/System/Convert/ProfileController.php 文件

我的模块config.xml是

<?xml version="1.0"?>
<config>
  <modules>
    <GWB_ClearOrphan>
      <version>0.1.0</version>
    </GWB_ClearOrphan>
  </modules>
  <global>
    <helpers>
      <clearorphan>
        <class>GWB_ClearOrphan_Helper</class>
      </clearorphan>
    </helpers>
    <models>
      <clearorphan>
        <class>GWB_ClearOrphan_Model</class>
        <resourceModel>clearorphan_mysql4</resourceModel>
      </clearorphan>
    </models>
    <events>
      <catalog_product_import_profile_after> <!-- identifier of the event we want to catch -->
        <observers>
        <catalog_product_import_profile_after_handler> <!-- identifier of the event handler -->
        <type>model</type> <!-- class method call type; valid are model, object and singleton -->
        <class>clearorphan/observer</class> <!-- observers class alias -->
        <method>disableProducts</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
            </catalog_product_import_profile_after_handler>
        </observers>
      </catalog_product_import_profile_after>
    </events>
  </global>
</config>

Observer.php

<?php
class GWB_ClearOrphan_Model_Observer
{

        public function disableProducts(Varien_Event_Observer $observer)
        {
            //mail('kapil_gupta@gowebbaby.com','sub-test','msg-test');

            try{    
                $collection = Mage::getModel('catalog/product')->getCollection();

                $pro_collection = var_dump($collection);
                mail('kapil_gupta@gowebbaby.com','collection',$pro_collection);

            }
            catch(Exception $e) {
                //Mage::log($e->getMessage(), null, 'collection.log');
            }

 }

Disausproducts功能下的邮件正常工作,但我无法使用此代码获得产品集合 $collection = Mage::getModel('catalog/product')->getCollection();

任何对我做错了什么有任何想法的身体都可以指出。谢谢。

有帮助吗?

解决方案

收集将是空的,直到您访问其中的数据为止(即 foreach 循环),或致电 ->load() 反对它的方法。加载集合后,您无法再编辑其包含的结果集,因此该方法使您可以通过更改集合对象中包含的选择对象来完善和过滤返回的结果。反过来,这会修改与数据库相对于数据库运行的查询,从而在集合加载后会在集合中找到的项目。

如果您使用

$collection = Mage::getModel('catalog/product')->getCollection()->load();

您会发现该集合包含数据。请记住取决于商店的 catalog/product 收集可能很大,因此我当然不建议通过电子邮件将其内容发送给,因为这可能是大量数据 - 尤其是现场商店的情况。如果您试图进行调试,则应在本地开发环境中使用Xdebug和NetBeans等IDE来检查对象及其内容。

其他提示

尝试使用

$collection = $observer->getProduct(); 
var_dump($collection);
许可以下: CC-BY-SA归因
scroll top