Programmatically add product to cart with price change

前端 未结 6 1334
孤街浪徒
孤街浪徒 2020-11-28 20:44

I want to add a product to cart programmatically. Also, I want to change the product price when added to cart.

Suppose, my product\'s price is $100. I wanted to cha

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 21:02

    After digging a bit into Magento's core code, I found that you need to use $item->getProduct()->setIsSuperMode(true) in order to make $item->setCustomPrice() and $item->setOriginalPrice() work.

    Here is some sample code you can use within an Observer that listens for the checkout_cart_product_add_after or checkout_cart_update_items_after events. The code is logically the same except checkout_cart_product_add_after is called for only one item and checkout_cart_update_items_after is called for all items in the cart. This code is separated/duplicated into 2 methods only as an example.

    Event: checkout_cart_product_add_after

    /**
     * @param Varien_Event_Observer $observer
     */
    public function applyDiscount(Varien_Event_Observer $observer)
    {
        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {
            $item = $item->getParentItem();
        }
    
        // Discounted 25% off
        $percentDiscount = 0.25; 
    
        // 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);
        }
    }
    

    Event: checkout_cart_update_items_after

    /**
     * @param Varien_Event_Observer $observer
     */
    public function applyDiscounts(Varien_Event_Observer $observer)
    {
        foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) {
             if ($item->getParentItem()) {
                 $item = $item->getParentItem();
             }
    
             // Discounted 25% off
             $percentDiscount = 0.25; 
    
             // 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);
             }
        }
    }
    

提交回复
热议问题