0

enter image description here

config.xml for adding event:-

<events>
        <checkout_cart_product_add_after>
            <observers>
               <Mymodule_Cart_Model_Observer>
                  <type>singleton</type>
                  <class>Mymodule_Cart_Model_Observer</class>
                  <method>modifyPrice</method>
               </Mymodule_Cart_Model_Observer>
           </observers>
        </checkout_cart_product_add_after>
</events>

Observer.php:-

<?php

class Mymodule_Cart_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 = $this->_getPriceByItem($item);

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

        }


        protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
        {
            $qty = $item->getQty();
            $product = $item->getProduct();
            $finalprice = $product->getFinalPrice();

            $productOptions = $product->getTypeInstance(true)->getOrderOptions($product);

            foreach ($productOptions['options'] as $key => $value) {

                $product = Mage::getModel("catalog/product")->load($product->getId()); 


                $flag = 0;
                foreach ($product->getOptions() as $o) {
                    $values = $o->getValues();
                    if ($o->getTitle() == 'Package Options') {
                        foreach ($values as $v) {
                            if ($value['option_value'] == $v->getOptionTypeId()) {

                                $pkg_price = round($v->getprice(), 2); 


                                $pkg_weight = intval(preg_replace('/[^0-9]+/', '', $v->getTitle()), 10);

                                if($pkg_weight != 0){
                                $flag = 1;
                                if($qty > $pkg_weight){

                                    if($qty % $pkg_weight == 0){

                                         $pkg_count = $qty / $pkg_weight;

                                    }else{

                                        $pkg_count = floor($qty / $pkg_weight) + 1;

                                    }

                                }else{

                                     $pkg_count = 1;
                                  }

                                }
                            }
                        }
                    }
                }

            }

            if(isset($pkg_count) and $pkg_count != 1){ 

            $newPkgPrice = $pkg_count * $pkg_price;

            $newPkgPriceSplitted = $newPkgPrice / $qty;
            $newPkgPriceSplitted = $newPkgPriceSplitted;
            $finalprice = $finalprice - $pkg_price;
            $newFinalPrice = $finalprice + $newPkgPriceSplitted;
            }
            if($flag > 0){ return $newFinalPrice; }



        }

    }

?>

Here I could change amount in unit price.But i want to add amount to subtotal instead of unit price.

  • Please help it's urgent – Jithu Johnson Sep 2 '17 at 10:11
  • why need to change subtotal? i mean purpose of changing the subtotal? – Padhiyar Gaurang Sep 2 '17 at 10:49
1

Please update the code

    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 = $this->_getPriceByItem($item);

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

         //for updating the totals
         $quote = Mage::getModel('checkout/session')->getQuote(); 
         $quote->collectTotals()->save(); 

    }
| improve this answer | |
  • But it works but I need to change the row total of the product in cart instead of changing unit price – Jithu Johnson Sep 2 '17 at 11:24
  • please see the image above – Jithu Johnson Sep 2 '17 at 11:27
  • please it's urgent – Jithu Johnson Sep 2 '17 at 12:03
  • Anyone here to help me? – Jithu Johnson Sep 6 '17 at 6:20
0

I got the solution :)

  1. Overrided the Model 'Mage_Sales_Model_Quote_Item' and Added the event 'sales_quote_save_after'

vendor/Mymodule/etc/config.xml

<global>
<models>
    <sales>
        <rewrite>
            <quote_item>vendor_Mymodule_Model_QuoteItem</quote_item>
        </rewrite>
</sales>
</models>

<events>
    <sales_quote_save_after>
        <observers>
           <vendor_Mymodule_Model_Observer>
              <type>singleton</type>
              <class>vendor_Mymodule_Model_Observer</class>
              <method>modifyPrice</method>
           </vendor_Mymodule_Model_Observer>
       </observers>
    </sales_quote_save_after>

    <sales_quote_save_after>
        <observers>
           <vendor_Mymodule_Model_Observer>
              <type>singleton</type>
              <class>vendor_Mymodule_Model_Observer</class>
              <method>modifyPrice</method>
           </vendor_Mymodule_Model_Observer>
       </observers>
    </sales_quote_save_after>


</events>
</global>
  1. Overrided the class Mage_Sales_Model_Quote_Item and the function calcRowTotal() in my custom module

Vendor/Mymodule/Quoteitem.php

  <?php class Vendor_Mymodule_Model_QuoteItem extends Mage_Sales_Model_Quote_Item
{
protected $customRowTotalPrice = null;

public function setCustomRowTotalPrice($price)
{
    $this->customRowTotalPrice = $price;
}

public function calcRowTotal()
{
    if ($this->customRowTotalPrice !== null) {

        $this->setRowTotal($this->getStore()->roundPrice($this->customRowTotalPrice));
        $this->setBaseRowTotal($this->getStore()->roundPrice($this->customRowTotalPrice));
        return $this;
    }

    $qty        = $this->getTotalQty();
    // Round unit price before multiplying to prevent losing 1 cent on subtotal
    $total      = $this->getStore()->roundPrice($this->getCalculationPriceOriginal()) * $qty;
    $baseTotal  = $this->getStore()->roundPrice($this->getBaseCalculationPriceOriginal()) * $qty;

    $this->setRowTotal($this->getStore()->roundPrice($total));
    $this->setBaseRowTotal($this->getStore()->roundPrice($baseTotal));
    return $this;




    }

}


?>
  1. Added observer event 'sales_quote_save_after' in vendor/Mymodule/Model/Observer.php

    <?php
    class Vendor_Mymodule_Model_Observer    
    {
    public  function modifyPrice(Varien_Event_Observer $obs)
    {
    $salesQuote = Mage::getSingleton('checkout/session')->getQuote();
    $items = $salesQuote->getAllItems();
    
    foreach($items as $item):
    
      $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
    
        $qty = $item->getQty();
        $product = $item->getProduct();
        $finalprice = $product->getFinalPrice();
        $productOptions = $product->getTypeInstance(true)->getOrderOptions($product);
        foreach($productOptions['options'] as $key => $value)
            {
            $product = Mage::getModel("catalog/product")->load($product->getId());
            $flag = 0;
            foreach($product->getOptions() as $o)
                {
                $values = $o->getValues();
                if ($o->getTitle() == 'Package Options')
                    {
                    foreach($values as $v)
                        {
                        if ($value['option_value'] == $v->getOptionTypeId())
                            {
                            $pkg_price = round($v->getprice() , 2);
                            $pkg_weight = intval(preg_replace('/[^0-9]+/', '', $v->getTitle()) , 10);
                            if ($pkg_weight != 0)
                                {
                                $flag = 1;
                                if ($qty > $pkg_weight)
                                    {
                                    if ($qty % $pkg_weight == 0)
                                        {
                                        $pkg_count = $qty / $pkg_weight;
                                        }
                                      else
                                        {
                                        $pkg_count = floor($qty / $pkg_weight) + 1;
                                        }
                                    }
                                  else
                                    {
                                    $pkg_count = 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
    
        $newPkgPrice = $pkg_count * $pkg_price;
        $PkgPriceWithQty = $pkg_price * $qty;
        $finalprice = $finalprice - $pkg_price;
        $item->setCustomPrice($finalprice);
        $item->setOriginalCustomPrice($finalprice);
        $newfinalprice = $finalprice * $qty;
        $newfinalpricewithpkgprice = $newfinalprice + $newPkgPrice;
        $item->setCustomRowTotalPrice($newfinalpricewithpkgprice);
        $item->getProduct()->setIsSuperMode(true);
        $item->save();
        $salesQuote = Mage::getSingleton('checkout/session')->getQuote();
        $salesQuote->setTotalsCollectedFlag(false);
        $salesQuote->collectTotals();
    
    
    
    endforeach; 
    
     }
       }    ?>
    

NOTE: 'sales_quote_save_after' is the right event to modify Row Total price in cart.

| improve this answer | |

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.