1

我正在部署中,我试图通过使用 Magento 的观察者和事件提供某些属性固定数据来覆盖产品保存,但问题是我已经在我一直在开发的本地工作,但是当我上传到服务器并尝试通过观察者运行时似乎根本没有触发。我基于本教程的以下代码:http: //www.magentocommerce.com/wiki/5__-_modules_and_development/0__-_module_development_in_magento/customizing_magento_using_event-observer_method

我有两个文件用于在 /app/code/local 中实现此目的。

/app/code/local/Leafcutter/目录/etc/config.xml

<?xml version="1.0"?>
<config>
  <global>
    <models>
        <leafcuttercatalog>
             <class>Leafcutter_Catalog_Model</class>
        </leafcuttercatalog>
    </models>
    <events>
      <catalog_product_save_before>
        <observers>
          <leaf_cutter_catalog_productautoinsert_observer>
            <type>singleton</type>
            <class>Leafcutter_Catalog_Model_ProductAutoInsert_Observer</class>
            <method>catalog_product_save_before</method>
          </leaf_cutter_catalog_productautoinsert_observer>
        </observers>
      </catalog_product_save_before>
    </events>
  </global>
</config>

/app/code/local/Leafcutter/Catalog/Model/ProductAutoInsert/Observer.php

class Leafcutter_Catalog_Model_ProductAutoInsert_Observer {

public function __construct(){}

/**
 * catalog_product_save_before() is called before the product in the model is about to be saved. In this
 * case, we want to override the saving mechanism and force the product to save certain preset data onto the product.
 * @param $observer
 * @return Leafcutter_Catalog_Model_ProductAutoInsert_Observer
 */
public function catalog_product_save_before($observer){
    Mage::log('Going to observer');
    $event = $observer->getEvent();
    $product = $event->getProduct();

    if($product->getWantitnow()){
        Mage::log('Product is want it now');

        $this->_checkAndSave($product, WANT_IT_NOW_SHIPPING_GROUP, WANT_IT_NOW_DELIVERIES_RETURNS);
    } else {
        Mage::log('Product is in warehouse');
        $this->_checkAndSave($product, IN_WAREHOUSE_SHIPPING_GROUP, IN_WAREHOUSE_DELIVERIES_RETURNS);
    }

    return $this;

}

/**
 * _checkAndSave() checks to ensure that the product has the appropriate delivery and returns text as well
 * @param $product - The product object in question
 * @param $specialShippingGroupId - The Special Shipping Group value that needs to be set.
 * @param $deliveriesReturnsText - The Deliveries and Returns text that needs to be set.
 * @return void
 */
private function _checkAndSave($product, $specialShippingGroupId, $deliveriesReturnsText){
    if($product->getDeliveriesAndReturns()!= $deliveriesReturnsText){
        $product->setDeliveriesAndReturns($deliveriesReturnsText);
    }

    if($product->getSpecialShippingGroup() != $specialShippingGroupId){
        $product->setSpecialShippingGroup($specialShippingGroupId);
    }

    Mage::log('_checkAndSave: outputting deliveries and returns' . $product->getDeliveriesAndReturns());
    Mage::log('_checkAndSave: outputting deliveries and returns' . $product->getGetSpecialShippingGroup());

}

}

我也尝试输出一些文本进行测试,但没有任何内容进入日志,所以我认为事件没有被触发。我安装了确切的插件,所以我不确定实时版本的插件之间是否会发生冲突。

如果上面的代码可能不是真正的问题,还有哪些其他可能性会导致这个问题?

多谢你们。

4

4 回答 4

6

您是否添加了 app/etc/modules/Leafcutter_Catalog.xml?

<?xml version="1.0"?>
<config>
    <modules>
        <Leafcutter_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </Leafcutter_Catalog>
    </modules>
</config>

/app/code/local/Leafcutter/目录/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Leafcutter_Catalog>
        <version>1.1</version>
    </Leafcutter_Catalog>
  </modules>
  <global>
    <models>
        <leafcuttercatalog>
             <class>Leafcutter_Catalog_Model</class>
        </leafcuttercatalog>
    </models>
    <events>
        <catalog_product_save_before>
            <observers>
                <leafcuttercatalog>
                    <type>singleton</type>
                    <class>catalog/productautoinsert_observer</class>
                    <method>catalog_product_save_before</method>
                </leafcuttercatalog>
            </observers>
        </catalog_product_save_before>
      </events>
  </global>
</config>
于 2012-12-18T20:07:06.497 回答
1

一种可能性是本地代码分支未打开,或者存在文件系统区分大小写的差异。

于 2012-12-18T19:31:08.980 回答
1

使用此事件“ catalog_product_prepare_save ”将起作用。您正在使用的事件现在可能已被弃用。我不确定虽然上面的一个可以正常工作。

于 2012-12-19T09:55:37.980 回答
0

抱歉耽误你们时间了。但最后它是我为 setSpecialShippingGroup 设置的值之一,但我错误地定义了它。但感谢您抽出宝贵时间。:)

于 2012-12-19T03:53:16.183 回答