1

We are working on a custom module which can add product to the cart. So currently there are two methods to update cart, ie from Magento's normal add to cart and from our custom Module. While adding product from the custom module we want to set a custom price for the product. I was able to do this using the following code (checkout_cart_product_add_after).

public function modifyPrice(Varien_Event_Observer $observer) {
    $item = $observer->getQuoteItem();
    // Ensure we have the parent item, if it has one
    $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
    // Load the custom price
    $price = 2;
    // Set the custom price
    $item->setCustomPrice($price);
    $item->setOriginalCustomPrice($price);
    // Enable super mode on the product.
    $item->getProduct()->setIsSuperMode(true);
}

As per my understanding the above code will be executed when ever a product added to the cart. ie from custom module or from Magento's add to cart. But we need to execute the code only when it is added from custom module. To do this We need to pass a flag/parameter to Observer function. How can we do this? Or are there any alternative methods to achieve what we are trying to do?.

0

In Mage:: dispatchEvent of checkout_cart_product_add_after you can see there is an array with two parameters which is quote_item and product, similarly you can add your custom parameters to this array. And as you call to get quote_item and product in observer similarly you can call your passed parameter to get its value.

0

You need to add below code in your observer file:

 -------------------

/*Add custom Option(on-fly) */
$additionalOptions = array(array(
     'code' => 'option_code',
     'label' => 'Some_option_Label',
     'value' => 'Option_Value',
));
$item->addOption(array(
     'code' => 'additional_options',
     'value' => serialize($additionalOptions),
));

// Enable super mode on the product.
$item->getProduct()->setIsSuperMode(true);  

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.