1

I have 5 items in cart with same product id.

I want to set first 4 item price as 9.99 and 5th item onwards will have of 20% discount on original price.

I have set first 4 items as club_item and 5th product onwards as other product(using my custom table).

If I add only 4 items, then all the 4 product's price is showing correctly as 9.99;

But when I add all 5 items, then all the 5 items price is changing and it is the price of last item added. Means all items are showing price = (actual_productprice - 20% discount).

How can I show different price for only 5th Item.

I have below event

    <events>            
           <sales_quote_add_item>
               <observers>
                  <Company_Module_Model_Observer>
                     <type>singleton</type>
                     <class>Company_Module_Model_Observer</class>
                     <method>updatePrice</method>
                  </Company_Module_Model_Observer>
              </observers>
           </sales_quote_add_item>   
    </events>

And my observer function is :-

    public function updatePrice($observer)
    {
        $event = $observer->getEvent();
        $product = $event->getProduct();
        $quote_item = $event->getQuoteItem();

        $productId = $quote_item->getProductId();

        if(Mage::getSingleton('customer/session')->isLoggedIn()) {
             $customerData = Mage::getSingleton('customer/session')->getCustomer();
              $customer_id = $customerData->getId();
        }

        $_customModel = Mage::getModel('custom/custom')->getCollection()
                ->addFieldToFilter('product_id', array('eq'=>$productId))
                ->addFieldToFilter('customer_id', array('eq'=>$customer_id));

        $_customData = $_customModel->getData();

        $actualPrice = $quote_item->getProduct()->getFinalPrice();

        /* this is for separating each line item in cart */
        $quote_item->addOption(array(
              "code" => "random",
              "value" => serialize(array(time()))
        ));

        $new_price = 0; 
        foreach ($_customData as $_customDataValue) {
            $_isClubProductId   = $_customDataValue['product_id'];
            $_isClubItem        = $_customDataValue['is_club_item'];

            /* Here I am differentiting the products with variable $_isClubItem */

            if($productId == $_isClubProductId && $_isClubItem == 1){
                $customPrice = 9.99;
                $new_price = round($customPrice, 4);
            }else{
                $new_price = (float)number_format($actualPrice * ((100-20) / 100),2);
            }
        }

        $quote_item->setOriginalCustomPrice($new_price);

        //$quote_item->save();
        $quote_item->getQuote()->save();
    }
0

I think you can do this with a shopping cart price rule. You can set the Requirements as minimum quantity in cart of 4, and apply a 20% discount on the products. The price rule will be only applied on the fifth product, and onwards.

  • Actually I am creating cart programatically. – Nitz Mar 8 '17 at 14:12

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.