1

I have 4 main products in cart, Each item is having different price. I want to update cart's total price as $40 without considering each line item price. I want to show $40 as combined price for all 4 items.

I tried with below code, but this can update each line item price.

<events>            
   <sales_quote_add_item>
       <observers>
          <Company_Module_Model_Observer>
             <type>singleton</type>
             <class>Company_Module_Model_Observer</class>
             <method>updatePrice</method>
          </Company_Module_Model_Observer>
      </observers>
   </sales_quote_add_item>
</events>

  <?php
  class Company_Module_Model_Observer
  {
      public function getNewPrice()
      {
          $quote = Mage::getSingleton('checkout/session')->getQuote();
          $cartItems = $quote->getAllVisibleItems();

          $custom_price = 40;

          foreach ($cartItems as $item) {
              $productId = $item->getProductId();
              $product = Mage::getModel('catalog/product')->load($productId);

              $actualPrice = $product->getPrice();

              $newprice = $custom_price;            
          }

          return $newprice;
      }

      public function updatePrice($observer)
      {
          $event = $observer->getEvent();
          $product = $event->getProduct();
          $quote_item = $event->getQuoteItem();
          $new_price = $this->getNewPrice();
          $quote_item->setOriginalCustomPrice($new_price);
          //$quote_item->save();
          $quote_item->getQuote()->save();
          //Mage::getSingleton('checkout/cart')->save();
      }

  }
2
  • You have to check your logic first. How it is possible, you are adding different items with different price in cart and charge something else ? Commented Feb 28, 2017 at 7:48
  • Instead of this you can go with bundle product which have some different product but price is fixed as $40. Commented Feb 28, 2017 at 7:51

2 Answers 2

2

Please reconsider your logic. From a customer point of view, it is not a best practice to display a Grand Total value other than the total price of items in cart.

Or you can use the Bundle Item products feature, where you can configure multiple products with different price value under a single bundle product and set the price value of bundle product to be fixed.

If my understanding is wrong, on your query, please give a more clear picture what you are trying to achieve.

0

Kindly find below URL for showing custom total.

URL :http://magento.ikantam.com/qa/how-add-discount-total-magento

public function collect(Mage_Sales_Model_Quote_Address $address) {
            if ($address->getData('address_type') == 'billing')
                return $this;

                $discount = Mage::helper('my_module')->getCurrentdiscount(); // Custom percentage

                $grandTotal = $address->getGrandTotal();
                $baseGrandTotal = $address->getBaseGrandTotal();

                $totals = array_sum($address->getAllTotalAmounts());
                $baseTotals = array_sum($address->getAllBaseTotalAmounts());

                $address->setFeeAmount(-$totals * $discount / 100);
                $address->setBaseFeeAmount(-$baseTotals * $discount / 100);

                $address->setGrandTotal($grandTotal + $address->getFeeAmount());
                $address->setBaseGrandTotal($baseGrandTotal + $address->getBaseFeeAmount());
        return $this;
    }

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.