0

I have been trying to find a way to do this, but not quite getting what logic to use.

Lets say there is a product "Chocolate" priced $10. If he adds this to cart, the second chocolate should be $8. If he adds one more i.e the third one, it should be $6.

So if he adds 3 chocolates, price will be $10 + $8 +$6

I know in Magento, this can be done using shopping cart rule. But this should appear in product page as well. Customer should know that he can get 2nd one at $8 and third one at $6. As per my understanding, if I use shopping cart rule, discount will be applied only in cart page.

Also if I use tier price, I can do only Buy 3 chocolates @ $X each, which is not what I want.

You can see a demo here: http://www.v2cigs.com/v2pro-vaporizer/series-3-vaporizer-kit (The above link is using Interspire, not Magento) We want to achieve same in our Magento site as well.

1

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);
         }
    }
}
1
  • Yes, this is a good solution. But this is giving us one issue as we have been trying the same to achieve. The issue arises when a customer added suppose two products to his cart and after that he changes his currency to let us say USD from GBP. In that case the price of the second product i.e the discounted one doesn't change. It remains USD .75 which should have changed to approx USD 1.2. where as the price of the first product changes to approx USD 1.6 which was initially GBP 1. So, what I noticed that the price of the discounted product doesn't change. Thanks. – Nasir Perwaiz Nov 6 '14 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.