Magento 1.9 – Custom Price Attribute on Cart Page

checkoutmagento-1.9price

I am making a rent module and I want to send a price attribute "rent_price" to cart page when customer click on rent button and normal price(base/special) when they click on add to cart.
Please help.

Best Answer

Edited:

  1. add New Action (rentAction) to CartContoller.php and Copy code of addAction to this new Action
  2. add New field (from_rent) to sales_quote_item which define a cart is doing from rent button

See Add a column to quote item table

  1. then using checkout_cart_product_add_after set from_rent=1 using
    current request Action

See how use this event http://doghouse.agency/article/magento-adding-message-product-during-purchase

public function addrentFlag(Varien_Event_Observer $observer) {
        try {

            $Event = $observer->getEvent();
                $product=$Event->getProduct();
                $quote_item=$Event->getQuoteItem();
                $item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
                if(Mage::app()->getRequest()->getActionName()=='rent'):
                $item->setFromRent(1);
            endif;
        } catch (Exception $e) {
            // log any issues, but allow system to continue.

            mage::logException($e);
            if (mage::getIsDeveloperMode()) {
                die($e->getMessage());
            }
        }
        return $this;
    }

a.you can use magento event/observer. Basically there are two events,by which you can change the cart price of that products:

  1. checkout_cart_product_add_after
  2. checkout_cart_update_items_after
  3. checkout_cart_product_update_after

This 3 event is need because of:

Event1:checkout_cart_product_add_after

This event is fire when first time a product is cart for current session of current input values from frontend

Event2:checkout_cart_product_update_after

This event is fire when a current exiting cart item edited from edit link of cart item.

Event3:checkout_cart_update_items_after

This event is fire whenever cart items update from cart page.

As per as magento system, a cart item price change using setter functions setCustomPrice(), setOriginalCustomPrice of cart item Object.

Just like:

$EachCartitem->setCustomPrice($price);
$EachCartitem->setOriginalCustomPrice($price);
// Enable super mode on the product.
$EachCartitem->getProduct()->setIsSuperMode(true);

And using events fire an observer which is calculate price basic of your your rend field.

![enter image description here][2]

Config.xml code like:

  <global>
    <models>
      <magento65569>
        <class>Stackexchange_Magento65569_Model</class>
      </magento65569>
    </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>magento65569/observer</class> <!-- observers class alias -->
            <method>CalculatePrice</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>magento65569/observer</class> <!-- observers class alias -->
            <method>UpdateCalculatePrice</method>  <!-- observer's method to be called -->
          </checkout_cart_update_items_after_handler>
        </observers>
      </checkout_cart_update_items_after>
        <checkout_cart_product_update_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_cart_product_update_after_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento65569/observer</class> <!-- observers class alias -->
            <method>ExitingcartItemUpdate</method>  <!-- observer's method to be called -->
          </checkout_cart_product_update_after_handler>
        </observers>
        </checkout_cart_product_update_after>
            </events>
  </global>

Observer code like this:

<?php
class Stackexchange_Magento65569_Model_Observer
{

        /* This function will work when product will cart */ 
            public function CalculatePrice(Varien_Event_Observer $observer)
            {
                //Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
                $RadioVal=0;
                $TextVal=0;
                $resultParams=array();
                $Event = $observer->getEvent();
                $product=$Event->getProduct();
                $quote_item=$Event->getQuoteItem();
                $item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
            $price=$product->getData('rent_price');
                $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            // Enable super mode on the product.
            $item->getProduct()->setIsSuperMode(true);
            return;

            }
            /*  This function will work when cart item  will   qty  update */
            public function UpdateCalculatePrice(Varien_Event_Observer $observer){
                 // Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
                 $cart=$observer->getEvent()->getCart();
                 $data=$observer->getEvent()->getInfo();
                 foreach ($data as $itemId => $itemInfo) {
                        $quote_item = $cart->getQuote()->getItemById($itemId);
                    if (!$quote_item) {
                        continue;
                    }

                    $item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );
                    $price=$product->getData('rent_price');
                        $item->setCustomPrice($price);
                    $item->setOriginalCustomPrice($price);
                    // Enable super mode on the product.
                    $item->getProduct()->setIsSuperMode(true);
                 }


            }
            public function ExitingcartItemUpdate($observer){
              /*Mage::dispatchEvent('checkout_cart_product_update_after', array(
                        'quote_item' => $result,
                        'product' => $product
                    ));*/
                $Event = $observer->getEvent();
                $product=$Event->getProduct();
            $price=$product->getData('rent_price');
                $quote_item=$Event->getQuoteItem();
                $item = ($quote_item->getParentItem()?$quote_item->getParentItem():$quote_item );

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


                return
            }






}
Related Topic