Magento:使用观察者以定制价格将产品添加到购物车

时间:2014-03-25 14:59:04

标签: magento magento-1.7 magento-1.8

我在产品页面中有一个输入,允许客户更改价格: enter image description here

我遵循了该教程:http://magentotutorial.org/programmatically-add-product-cart-price-change-magento/

这是我的代码: config.xml:

<events>
            <core_block_abstract_to_html_after>
                <observers>
                    <customprice>
                        <type>singleton</type>
                        <class>WebDirect_CustomPrice_Model_Observer</class>
                        <method>convertPricespanToInput</method>
                    </customprice>
                </observers>
            </core_block_abstract_to_html_after>

            <checkout_cart_product_add_after>
                <observers>
                    <customprice>
                        <type>singleton</type>
                        <class>WebDirect_CustomPrice_Model_Observer</class>
                        <method>applyCustomPrice</method>
                    </customprice>
                </observers>
            </checkout_cart_product_add_after>

        </events>

观察员班:

<?php
class WebDirect_CustomPrice_Model_Observer
{
    const MODULE_NAME = 'WebDirect_CustomPrice';
    public function convertPricespanToInput($observer = NULL) {
        if (!$observer) {
            return;
        }
        if ('product.info.simple' == $observer->getEvent()->getBlock()->getNameInLayout()) {
            if (!Mage::getStoreConfig('advanced/modules_disable_output/'.self::MODULE_NAME)) {
                $transport = $observer->getEvent()->getTransport();
                $block = new WebDirect_CustomPrice_Block_priceSpanToInput();
                $block->setPassingTransport($transport['html']);
                $block->toHtml();
            }
        }
        return $this;
    }

    /**
     * @param Varien_Event_Observer $observer
     */
    public function applyCustomPrice(Varien_Event_Observer $observer) {
        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {
            $item = $item->getParentItem();
        }

        // Discounted 50% off
        $percentDiscount = 0.50;

        // This makes sure the discount isn't applied over and over when refreshing
        $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

        // Make sure we don't have a negative
        if ($specialPrice > 0) {
            $item->setCustomPrice($specialPrice);
            $item->setOriginalCustomPrice($specialPrice);
            $item->getProduct()->setIsSuperMode(true);
        }
    }
}

该功能仅用于测试 当我点击添加到购物车没有任何变化! 这有什么错误吗?

0 个答案:

没有答案