2

I need to add a second price factor to the cart.

Besides amount/quantity I have to add a second (integer) value that comes from a custom option, so the calculation isn't

base price x amount

but

base price x "custom option value" x amount

Background: Custom option is "number of days" for a rentable product.

Note: I do not need to incorporate anything after the checkout process, since the Magento install is configured to act as "inquiry only" system (no invoicing and such).

I presume there is some event I can observe for doing so?

2

I suggest to use observes both checkout_cart_product_add_after and checkout_cart_update_items_after events.

Those event provide two parameter one is current Quote item quote_item and other is product object.

See Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));

Create Custom Module at config.xml code look like:

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento55235>
      <version>0.1.0</version>
    </Stackexchange_Magento55235>
  </modules>
  <global>
    </helpers>
    <models>
      <magento55235>
        <class>Stackexchange_Magento55235_Model</class>
        <resourceModel>magento55235_mysql4</resourceModel>
      </magento55235>
    </models>
    <events>
      <checkout_cart_product_add_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_cart_product_add_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento55235/observer</class> <!-- observers class alias -->
            <method>resetMyPrice</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_cart_product_add_after_handler>
        </observers>
      </checkout_cart_product_add_after>
      <checkout_cart_update_items_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_cart_update_items_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento55235/observer</class> <!-- observers class alias -->
            <method>AllresetMyPrice</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_cart_update_items_after_handler>
        </observers>
      </checkout_cart_update_items_after>
    </events>
  </global>
</config> 

On Observer file reset product price by setCustomPrice() and setOriginalCustomPrice()

<?php
class Stackexchange_Magento55235_Model_Observer
{

            public function resetMyPrice(Varien_Event_Observer $observer)
            {

                $Event = $observer->getEvent();
                $product=$Event->getProduct();
                $quote_item=$Event->getQuoteItem();
                $basePrice = $product->getPrice();
                $customOptions = $quote_item->getBuyRequest()->getOptions();
                //using $customOptions get option value 
                /* th */
                $price = $basePrice * $numberofdays * $integerField ;
                $quote_item->setCustomPrice($price);
                $quote_item->setOriginalCustomPrice($price);
                $quote_item->getProduct()->setIsSuperMode(true);

            }

            public function AllresetMyPrice(Varien_Event_Observer $observer)
            {

                  // Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
                  /* need to same action here */

            }

}
  • Works! Thanks Amit. There's just one typo in your config.xml (</helpers> should be removed). – lars.schwarz Feb 11 '15 at 12:21
  • Is there an event for "cart view"? Basically it works fine after adding an item, but I also need to modify the price when just viewing the cart. I can't seem to find an event for that ... – lars.schwarz Feb 11 '15 at 17:01

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.