Magento : Add Product to cart with custom price using observer

情到浓时终转凉″ 提交于 2019-12-23 06:21:16

问题


i have an input in product page that allows to customers to change the price :

i followed that tutorial : http://magentotutorial.org/programmatically-add-product-cart-price-change-magento/

and this is my code : 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>

observer class :

<?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);
        }
    }
}

that function is just for test when i click add to cart nothing change ! is there any error in this ?

来源:https://stackoverflow.com/questions/22638328/magento-add-product-to-cart-with-custom-price-using-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!