magento - 确定 Magento 中的类别是否为新类别

标签 magento

我的 config.xml 中有一个事件 Hook ,该 Hook 在通过管理区域添加或编辑类别后触发:

    <events>

        <catalog_category_save_commit_after>
            <observers>
                <mymodule_savecategoryrules>
                    <type>singleton</type>
                    <class>Mynamespace_Mymodule_Model_Observer</class>
                    <method>saveCategoryRules</method>
                </mymodule_savecategoryrules>
            </observers>
        </catalog_category_save_commit_after> 

    </events>      

这是我的观察者的片段:

class Mynamespace_Mymodule_Model_Observer 
{

    public function saveCategoryRules($observer) 
    {

        $event      = $observer->getEvent();
        $category   = $event->getCategory();
        $categoryId = $category->getId();

        var_dump($categoryId); // Returns a category ID if edited or the new ID if added

    }
 }

我需要知道该类别是否是新的,是否有替代钩子(Hook)或其他方法来确定这一点?

提前致谢。

最佳答案

回答我自己的问题,以防其他人发现这有用。使用钩子(Hook)catalog_category_prepare_saveconfig.xml中添加一个新的事件监听器:

        <catalog_category_prepare_save>
            <observers>
                <mymodule_savecategory>
                    <type>singleton</type>
                    <class>Mynamespace_Mymodule_Model_Observer</class>
                    <method>saveCategory</method>
                </mymodule_savecategory>
            </observers>
        </catalog_category_prepare_save>  

还有我的观察者方法:

public function saveCategory($observer) 
{

    $event      = $observer->getEvent();
    $category   = $event->getCategory();

    if (!$category->getId()) {
        echo 'No ID - must be new';
    } else {
        echo $category->getId();
    }

}

关于magento - 确定 Magento 中的类别是否为新类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288715/

相关文章:

php - 通过 Postman 使用 Magento API 通过 Oauth 1.0 授权

magento - 是否可以在不加载模型的情况下更新模型?

magento - PHP Extensions “0” 必须加载怎么解决?

magento - 创建发货不会更新商品发货数量且订单未完成

php - 使用未定义常量 MCRYPT_BLOWFISH - 假定为 'MCRYPT_BLOWFISH'

php - 如何在 Magento 中将子产品添加到主产品

magento - 将 Magento 升级到 1.8.1 后结帐错误

php - magento 自定义支付模块用于多运

php - 在 Magento 中更改措辞 "Special Price"和 "Regular Price"

magento - 在 Magento 中加载客户购物车