0

When I add a product to cart then its related products will be added automatically with it and it's working. But, I want to make the price zero on the cart page for related products only, as they will be free. I have added event as below :

<events>
<sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>Inchoo_CustomLinkedProducts_Model_Observer</class>
                <method>modifyPrice</method>
            </priceupdate_observer>
        </observers>
        </sales_quote_add_item>
        </events>

Method in the observer is as below:

public function modifyPrice(Varien_Event_Observer $obs)
    {
        $free_product = $obs->getQuoteItem()->getProduct();
        // Get all custom products
        $custom_products = $free_product->getCustomProductCollection();
        // get all custom product id and save in array
        $custom_product_ids = array();
        foreach($custom_products as $product){
          $custom_product_ids[] = $product->getId(); // double check to make sure this product_id 
        }
        //$collection = Mage::getModel('sales/quote_item')->getCollection();

        // $items = Mage::getModel(‘checkout/cart’)->getQuote()->getAllItems();

       $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
         foreach($items as $item )
         { 
             if(in_array($item->getProductId(), $custom_product_ids))
              {
                $price = $this->_getPriceByItem($item);
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
              }
         }

       }

it is not working and i don't get related/custom product ids in the session , only 1 id gets printed.

4
  • Chances are that the items havent been added to quote by the time your event gets dispatched. Try to change the event to checkout_cart_product_add_after it should work. Check this for details - stackoverflow.com/questions/5104482/… Commented Mar 30, 2016 at 10:50
  • yes i have tried that as well but still not working
    – Laty
    Commented Mar 30, 2016 at 10:50
  • Do you mean set custom price for product? Commented Mar 30, 2016 at 10:52
  • yes but not for all products only for related/custom_free products in the cart
    – Laty
    Commented Mar 30, 2016 at 10:59

1 Answer 1

0

Use below code:

You can use an observer class to listen to checkout_cart_product_add_after, and use a product’s “Super Mode” to set custom prices against the quote item.

In your /app/code/local/{namespace}/{yourmodule}/etc/config.xml:

   <config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>

/app/code/local/{namespace}/{yourmodule}/Model/Observer.php

 class <namespace>_<modulename>_Model_Observer
{
    public function modifyPrice(Varien_Event_Observer $obs)
    {
        // Get the quote item
        $item = $obs->getQuoteItem();
        // Ensure we have the parent item, if it has one
        $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
        // Load the custom price
        $price = "your custom price logic";
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }

}

may be helpful to you

5
  • I am using the same code but still not getting result.
    – Laty
    Commented Mar 30, 2016 at 11:00
  • Confirm have you added $item->getProduct()->setIsSuperMode(true); Commented Mar 30, 2016 at 11:03
  • Yes, Can you please tell me which event will work properly - sales_quote_add_item or checkout_cart_product_add_after or checkout_cart_update_items_after
    – Laty
    Commented Mar 30, 2016 at 11:13
  • checkout_cart_product_add_after event you can use to modify price. Commented Mar 30, 2016 at 11:15
  • Using the same, But it doesn't come in the in_array() as all cart id's are not get.
    – Laty
    Commented Mar 30, 2016 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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