Changing the price in quote while adding product t

2019-01-03 10:07发布

I want to change the product price while add that product to cart.

How its possible let me know...

标签: magento
7条回答
在下西门庆
2楼-- · 2019-01-03 10:33

The question does not state whether this should be done by adding some logic to the code or not. So since you have answers for developer there is also something that is called cart price rules (in admin panel go to Promotions > Shopping Cart Price Rules) where you can create different rules for making sales and discounts (with or without coupons).

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-03 10:39
    To change product price while adding product to cart, use an observer event.
    Follow the steps given below
    1. Add an observer in your module config.xml file.
    2. Create an observer file in your model
    3. add checkout_cart_product_add_after event

File: app/code/local/Namespace/Module/etc/config.xml

eg: app/code/local/MGS/Rileytheme/etc/config.xml

     <frontend>
           <routers>
              <routeurfrontend>
                  <use>standard</use>
                  <args>
                     <module>MGS_Rileytheme</module>
                     <frontName>rileytheme</frontName>
                  </args>
               </routeurfrontend>
           </routers>
           <layout>
            <updates>
             <rileytheme>
              <file>rileytheme.xml</file>
             </rileytheme>
            </updates>
           </layout>
            <events>
              <checkout_cart_product_add_after>
                <observers>
                    <rileytheme>
                    <class>rileytheme/observer</class>
                    <method>modifyPrice</method>
                    </rileytheme>
                </observers>
               </checkout_cart_product_add_after>
            </events></b>
     <frontend>

File:app/code/local/MGS/Rileytheme/Model/Observer.php

class MGS_Rileytheme_Model_Observer {
    public function modifyPrice(Varien_Event_Observer $observer) {
         //$order = Mage::registry('current_order'); For getting current order 
         //$orderid = $order->getRealOrderId(); For getting orderid
          $quote_item = $observer->getQuoteItem();
          $payment = 30; //add your custom product price here and do your logic here
          $quote_item->setOriginalCustomPrice($payment);
          $quote_item->save();
          return $this;
    }
 }
查看更多
贼婆χ
4楼-- · 2019-01-03 10:41

Soup to Nuts.

File: /app/etc/modules/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Ajax_ProductAdjust>
      <codePool>local</codePool>
      <active>true</active>
    </Ajax_ProductAdjust>
  </modules>
</config>

File: /app/code/local/Ajax/ProductAdjust/etc/config.xml

<?xml version="1.0"?>
      <config>
       <modules>
         <Ajax_ProductAdjust>
           <version>1.0.1</version>
         </Ajax_ProductAdjust>
       </modules>
       <global>
           <models>
             <Ajax_ProductAdjust>
               <class>Ajax_ProductAdjust_Model</class>
             </Ajax_ProductAdjust>
           </models>
           <events>
              <sales_quote_add_item>
                  <observers>
                     <ajax_productadjust_model_observer>
                        <type>singleton</type>
                        <class>Ajax_ProductAdjust_Model_Observer</class>
                        <method>updatePrice</method>
                     </ajax_productadjust_model_observer>
                 </observers>
              </sales_quote_add_item>
          </events>
      </global>
     </config>

File: /app/code/local/Ajax/ProductAdjust/Model/Observer.php

<?php
//Notes
class Ajax_ProductAdjust_Model_Observer
{

    public function _construct()
      {
      }

    public function getNewPrice()
      {
        //Your new functionality here
        //
        $newprice = "";

        return $newprice;
      }

     public function updatePrice( Varien_Event_Observer $observer ) 
     {
        $event = $observer->getEvent();
        $quote_item = $event->getQuoteItem();
        $new_price = $this->getNewPrice();
        $quote_item->setOriginalCustomPrice($new_price);
        $quote_item->save();
      }
 }

Cheers,

查看更多
神经病院院长
5楼-- · 2019-01-03 10:42

You can use an observer class to listen to checkout_cart_product_add_after, and use a product’s “Super Mode” to set custom prices against the quote item.

In your /app/code/local/{namespace}/{yourmodule}/etc/config.xml:

<config>
    ...
    <frontend>
        ...
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>{{modulename}}/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        ...
    </frontend>
    ...
</config>

And then create an Observer class at /app/code/local/{namespace}/{yourmodule}/Model/Observer.php

    <?php
        class <namespace>_<modulename>_Model_Observer
        {
            public function modifyPrice(Varien_Event_Observer $obs)
            {
                $customPrice = Mage::getSingleton(’core/session’)->getCustomPriceCalcuation(); // Provide you price i have set with session
                $p = $obs->getQuoteItem();
                $p->setCustomPrice($customPrice)->setOriginalCustomPrice($customPrice); 
            }

        }
查看更多
爷、活的狠高调
6楼-- · 2019-01-03 10:42

As righty answered by Gershon Herczeg, Jürgen Thelen and Xman Classical. You will need to write a observer of sales_quote_add_item event. When any product is added to cart your observer will be triggered. If the product is Configurable then this event will be fired twice, You will have to something like that to get simple product only.

    $item = $observer->getEvent()->getQuoteItem();
    $quote = $item->getQuote();
    $product = $item->getProduct();
    if ($product->getTypeId() != "configurable") {
       //Do your thing here
    }
查看更多
等我变得足够好
7楼-- · 2019-01-03 10:44

The way to do it is add an observer which looks for this event 'sales_quote_add_item':

<events>
    <sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
    </sales_quote_add_item>
</events>

The observer should have a method which does something like this:

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->save();
}
查看更多
登录 后发表回答