Step 1
创建属性:
Step 2
添加到属性组:
Add this new attribute to your attributeset. If your product’s attributeset is ‘default‘, 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