在将产品添加到购物车时更改报价中的价格:magento

问题描述:

我想在将产品添加到购物车时更改产品价格。在将产品添加到购物车时更改报价中的价格:magento

如何其可能的让我知道...

做的方式是添加一个观察者,看起来此事件'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> 

观察员应该有哪些不一样的东西的方法这样的:

public function updatePrice($observer) { 
    $event = $observer->getEvent(); 
    $quote_item = $event->getQuoteItem(); 
    $new_price = <insert logic> 
    $quote_item->setOriginalCustomPrice($new_price); 
    $quote_item->save(); 
} 
+1

感谢分享信息!所以,XML将进入我们需要创建的本地模块,是的? (我通过搜索找到了这个:http://goo.gl/UBFgS)然后公共函数可以在运行我正在考虑的购物车代码的phtml中声明吗? – 2012-05-08 21:43:56

+1

在这里,这应该给你一个关于如何创建观察者的概述:http://alturl.com/tja43 – 2012-05-08 22:02:23

+4

回答了我的问题!感谢分享! – haifacarina 2012-12-05 20:30:10

您可以使用一个观察者类听checkout_cart_product_add_after,并使用产品的“超级模式”设置自定义价格AGA引述报价项目。

在你/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> 

然后在创建/应用/代码观察员类/本地/ {命名空间}/{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); 
      } 

     } 

如用右手由革顺Herczeg,尔根Thelen和古典的xman回答。您需要编写一个sales_quote_add_item事件的观察者。 当任何产品添加到购物车时,您的观察者将被触发。如果产品是可配置的,那么这个事件将被触发两次,您将不得不这样做,以获得简单的产品。

$item = $observer->getEvent()->getQuoteItem(); 
    $quote = $item->getQuote(); 
    $product = $item->getProduct(); 
    if ($product->getTypeId() != "configurable") { 
     //Do your thing here 
    } 

有关可配置产品的问题已解决。您只需删除

$ quote_item-> save();

然后为产品不会添加到购物车两次。但是这个功能仍然存在很严重的问题。也就是说,使用此功能,我们可以更新购物车中的商品价格,但添加到购物车后,产品价格不会根据不同的货币进行更改。所以这个功能不能用于有多种货币的商店。

如果有人发现关于这个问题的任何解决方案,请与我们分享......

一应俱全。

文件:/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> 

文件:/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> 

文件: /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(); 
     } 
} 

干杯,

+1

如何获得此观察者的产品详细信息? – Naveenbos 2015-05-05 12:15:33

+0

我尝试过使用这种方法,并且不断收到错误:'1452无法添加或更新子行:外键约束失败' – NotJay 2016-02-09 21:20:37

这个问题并没有说明是否应该通过向代码中添加一些逻辑来完成。因此,既然你有开发者的答案,那么也有一些被称为购物车价格规则的东西(在管理面板中去促销>购物车价格规则),你可以创建不同的规则来制作销售和折扣(带或不带优惠券)。

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 

文件:应用程序/代码/本地/命名空间/模块的/ etc/config.xml中

例如:应用程序/代码/本地/ 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> 

文件:应用程序/代码/本地/ MGS/Rileytheme /型号/ 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; 
    } 
}