Magento为产品定制价格规则

时间:2011-10-18 10:56:55

标签: php magento

您好我需要使用magento来销售具有自定义价格规则的产品 该规则将取决于所售产品的数量 我知道magento可以制定特殊规则,如果客户按此数量的++数量,但我需要应用不同的规则,我无法找到方法。
例如,产品首先从客户购买100次,价格为:100美元 产品买200-500次,价格为400 $
500-1000次,产品是800 $
1000 - >产品定价1000美元 magento有可能这样做吗?



谢谢

2 个答案:

答案 0 :(得分:8)

您可以通过创建一个使用checkout_cart_product_add_after和checkout_cart_update_items_after观察员的模块来完成此操作。

然后在您的观察者类中,您可以使用以下函数来设置价格:

public function yourAddToCartFunction($observer) {

    if ($p = $observer->getQuoteItem()->getParentItem()) {
        $discount_amount = $your_discount_logic;
        $p->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #configs
    } else  {
        $p = $observer->getQuoteItem();
        $discount_amount = $your_discount_logic;
        $p->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #simple products
    }
}

购物车更新观察员调用很可能需要不同的功能。上面的那个是cart_add_after事件。购物车更新的功能几乎完全相同,除非您必须通过购物车对象来获取逻辑。

public function yourCartUpdateFunction($observer) {
    $cart = $observer->cart;
    $quote = $cart->getQuote();

    foreach($quote->getAllVisibleItems() as $item) {
        if ($p = $item->getParentItem()) {
            $discount_amount = $your_discount_logic;
            $p->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #configs
        } else {
            $discount_amount = $your_discount_logic;
            $item->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #simple products
        }
    }
}

您想要第二个功能的原因是,如果购物车更新,则需要再次发生逻辑。如果一旦它在购物车中,无论发生什么,价格都将永远保持下去,你可以排除第二个功能。

MASSIVE EDIT:

好的,我会这样做。我假设你从一个基本的功能模块开始。

您要做的第一件事是在您的模块config.xml文件中设置您的观察者。在这种情况下,您将使用checkout_cart_product_add_after和checkout_cart_update_items_after观察者。您将通过在config.xml文件中添加以下内容来设置它:

<config>
    ...
    <global>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <call_this_something_unique>
                        <type>singleton</type>
                        <class>Ocaff_Custompricing_Model_Cart_Observer</class>
                        <method>calculateAddToCart</method>
                    </call_this_something_unique>
                </observers>
            </checkout_cart_product_add_after>

            <checkout_cart_update_items_after>
                <observers>
                    <call_this_something_unique_2>
                        <type>singleton</type>
                        <class>Ocaff_Custompricing_Model_Cart_Observer</class>
                        <method>calculateCartUpdate</method>
                    </call_this_something_unique_2>
                </observers>
            </checkout_cart_update_items_after>
        </events>
        ...
    </global>
    ...
</config>

基本上它的作用是告诉Magento当触发checkout_cart_product_add_after事件时,运行Ocaff_Custompricing_Model_Cart_Observer类的calculateAddToCart方法,并在触发checkout_cart_update_items_after事件时运行calculateCartUpdate。

好了,现在您已将配置放在一起,您可以创建模型。对于此示例,该类将位于/app/code/local/Ocaff/Custompricing/Model/Cart/Observer.php文件中(但是您将放置与您的模块匹配的类)

现在,在您的Observer.php文件中,您将输入以下代码:

<?php

class Ocaff_Custompricing_Model_Cart_Observer {

    public function calculateAddToCart($observer) {
        if ($p = $observer->getQuoteItem()->getParentItem()) {
        $discount_amount = Mage::helper('custompricing')->calculatePrice($p);
        $p->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #configs
    } else  {
        $p = $observer->getQuoteItem();
        $discount_amount = Mage::helper('custompricing')->calculatePrice($p);
        $p->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #simple products
    }
    }

    public function calculateCartUpdate($observer) {
        $cart = $observer->cart;
    $quote = $cart->getQuote();

    foreach($quote->getAllVisibleItems() as $item) {
        if ($p = $item->getParentItem()) {
            $discount_amount = Mage::helper('custompricing')->calculatePrice($prpoduct);
            $p->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #configs
        } else {
            $discount_amount = Mage::helper('custompricing')->calculatePrice($p);
            $item->setCustomPrice($discount_amount)->setOriginalCustomPrice($discount_amount); #simple products
        }
    }
    }

}

这里有一些注意事项。这两个函数都使用了一个我称之为观察者的参数(并且几乎所有其他地方都称之为观察者)。这是一个单独的变量,它包含Magento传递给处理事件的所有函数的所有信息。在calculateAddToCart函数的情况下,我们只抓取引用项目(这是令人困惑的,因为在结账时与Magento有很多不同的对象,如果你从50,000英尺看它,它们都代表相同的东西)。此功能的作用是获取报价项目,并确定添加到购物车的产品是简单产品还是可配置产品,并采取相应的行动。

calculateCartUpdate函数基本上完全相同,但它必须遍历购物车中的所有产品,因为在Magento中您一次更新整个购物车,而不是每个订单项。

在这两个功能中,您会注意到我正在更新自定义价格和原始自定义价格字段。我不是100%确定为什么你必须更新两者(如果有效的话,为什么要问问题:))

好了,现在你可能会或可能不会注意到我已经做了一些比传统更少的东西来计算$ discount_amount变量。我正在调用辅助函数来获取产品应该设置的价格。我把它放到帮助器中是因为你可能想要在系统的其他地方访问那个逻辑,我通常更喜欢将这种逻辑放入Helper中(事实上我很确定你会想要在产品上使用它类别页面,因为您正在修改定价对某些产品起作用的方式,并且当价格意外地变化时客户会生气。)

好了,现在进入辅助功能。由于我不是100%确定你最终想要什么逻辑,我们只是保持这个功能简单并让它返回2美元。无论价格究竟是多少,这都将使购物车中的所有物品成为2美元。这将是您找出定价逻辑的地方。

<?php 

class Ocaff_Custompricing_Helper_Data {

    public function calculatePrice($product=null) {

        // Your custom logic will go here. Whatever number you come up with and return will be the price that the item is set to

        return 2;

    }

}

在大多数情况下,这应该可以让你达到你想要的90%。试试这个并发布问题,我会尽力帮助你。

另一个编辑:在config.xml中激活助手

在观察者阻止上面详述后立即将此代码放在config.xml文件中。这将告诉Magento模块的帮助文件的位置。

<helpers>
    <custompricing>
    <class>Ocaff_Custompricing_Helper</class>
    </custompricing>
</helpers>

答案 1 :(得分:1)

这是层级价格功能,您无需创建目录定价规则。在后端管理产品时检查产品编辑页面,选项卡价格并查看“层级价格”选项