仅在Magento中提供*特惠价* [英] Round *special price only* in Magento

查看:84
本文介绍了仅在Magento中提供*特惠价*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,客户希望将特惠价格四舍五入到最接近的整数(例如:1922.25 => 1922.001256.85 => 1257.00)

I have a situation where the customer wants the special price to be rounded to the nearest whole number (Example: 1922.25 => 1922.00, 1256.85 => 1257.00)

这仅适用于Special prices客户说,这意味着,如果该产品没有特殊价格,则价格应为原价.

This should be only for Special prices says the customer which means, if there's no special price for the product, the price should come as it is.

请注意,这不仅用于显示.此规则适用于所有地方,甚至包括发票.那么有没有办法轻松地做到这一点,或者我们应该只修改核心文件?

Note this is not only for display. This rule applies everywhere, even invoices. So is there a way to do this easily or we should only modify the core files?

编辑

如果我们修改核心框架文件(如Product.php或Price.php),它们会被更新覆盖吗?因此,这也应该是一种安全的方法.

If we modify the core framework files like Product.php or Price.php, they will get overwritten with an update right? So this should be a safe way too preferably.

推荐答案

这很简单.

只需观察catalog_product_load_after事件,然后用四舍五入的值覆盖special_price.

Just observe catalog_product_load_after event, and overwrite special_price with a rounded value.

为您的模块创建一个引导文件:

Create a bootstrap file for your module:

app/etc/modules/Danslo_RoundSpecialPrice.xml:

app/etc/modules/Danslo_RoundSpecialPrice.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Danslo_RoundSpecialPrice>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Danslo_RoundSpecialPrice>
    </modules>
</config>

指定我们要观察产品加载事件:

Specify that we want to observe the product load event:

app/code/local/Danslo/RoundSpecialPrice/etc/config.xml:

app/code/local/Danslo/RoundSpecialPrice/etc/config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
        <models>
            <roundspecialprice>
                <class>Danslo_RoundSpecialPrice_Model</class>
            </roundspecialprice>
        </models>
        <events>
            <catalog_product_load_after>
                <observers>
                    <round_special_price>
                        <class>roundspecialprice/observer</class>
                        <type>singleton</type>
                        <method>roundSpecialPrice</method>
                    </round_special_price>
                </observers>
            </catalog_product_load_after>
            <catalog_product_collection_load_after>
                <observers>
                    <round_special_price>
                        <class>roundspecialprice/observer</class>
                        <type>singleton</type>
                        <method>roundSpecialPriceInCollection</method>
                    </round_special_price>
                </observers>
            </catalog_product_collection_load_after>
        </events>
    </global>
</config>

然后只需编写您的观察者实现:

Then just write your observer implementation:

app/code/local/Danslo/RoundSpecialPrice/Model/Observer.php:

app/code/local/Danslo/RoundSpecialPrice/Model/Observer.php:

class Danslo_RoundSpecialPrice_Model_Observer
{

    public function roundSpecialPrice($observer)
    {
        $product = $observer->getProduct();
        if ($product->getSpecialPrice()) {
            $product->setSpecialPrice(round($product->getSpecialPrice()));
        }
    }

    public function roundSpecialPriceInCollection($observer)
    {
        foreach ($observer->getCollection() as $product) {
            if ($product->getSpecialPrice()) {
                $product->setSpecialPrice(round($product->getSpecialPrice()));
            }
        }
    }

}

这篇关于仅在Magento中提供*特惠价*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆