44.事件分发(示例)

Step 1
创建属性:
这里写图片描述

这里写图片描述


Step 2
添加到属性组:

Add this new attribute to your attributeset. If your product’s attributeset isdefault‘, add the new ‘percent_discount’ attribute to this attributeset under “prices” attribute group.

Step 3
创建模块

<?xml version="1.0"?>

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

<config>
    <modules>
        <Xyz_Catalog>
            <version>0.1.0</version>
        </Xyz_Catalog>
    </modules>

    <global>
        <models>
            <xyzcatalog>
                <class>Xyz_Catalog_Model</class>
            </xyzcatalog>
        </models>

        <events>
            <catalog_product_get_final_price>
                <observers>
                    <xyz_catalog_price_observer>
                        <type>singleton</type>
                        <class>Xyz_Catalog_Model_Price_Observer</class>
                        <method>apply_discount_percent</method>
                    </xyz_catalog_price_observer>
                </observers>
            </catalog_product_get_final_price>
        </events>
    </global>
</config>
<?php class Xyz_Catalog_Model_Price_Observer { public function apply_discount_percent($observer) { $event = $observer->getEvent(); $product = $event->getProduct(); // process percentage discounts only for simple products if ($product->getSuperProduct() && $product->getSuperProduct()->isConfigurable()) { } else { $percentDiscount = $product->getPercentDiscount(); if (is_numeric($percentDiscount)) { $today = floor(time()/86400)*86400; $from = floor(strtotime($product->getSpecialFromDate())/86400)*86400; $to = floor(strtotime($product->getSpecialToDate())/86400)*86400; if ($product->getSpecialFromDate() && $today < $from) { } elseif ($product->getSpecialToDate() && $today > $to) { } else { $price = $product->getPrice(); $finalPriceNow = $product->getData('final_price'); $specialPrice = $price - $price * $percentDiscount / 100; // if special price is negative - negate the discount - this may be a mistake in data if ($specialPrice < 0) $specialPrice = $finalPriceNow; if ($specialPrice < $finalPriceNow) $product->setFinalPrice($specialPrice); // set the product final price } } } return $this; } }

更新缓存,索引

这里写图片描述
这里写图片描述


https://magento2.atlassian.net/wiki/display/m1wiki/Using+the+Event-Observer+Method+in+Magento+1.x

定义一个脚本,列出所有事件:
http://edmondscommerce.github.io/magento/magento-events-cheat-sheet-grep-works-for-any-version.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值