2013-05-16 24 views
2

我是magento的初學者。我需要將動態價格從詳細頁面傳遞到購物車。將詳細頁面的動態價格傳遞到magento中的購物車

現在,當我傳遞動態價格時,它不會在購物車中更新,而是由產品的原始價格取代。

任何幫助將不勝感激。 在這一行我得到的價格價值。 $price=$this->getRequest()->getParam('price_custom'); Indexcontroller.php

public function cartAction() 
    { 
     if ($this->getRequest()->getParam('cart')){ 
      if ($this->getRequest()->getParam('cart') == "delete"){ 
       $id = $this->getRequest()->getParam('id'); 
       if ($id) { 
        try { 
         Mage::getSingleton('checkout/cart')->removeItem($id) 
          ->save(); 
        } catch (Exception $e) { 
         Mage::getSingleton('checkout/session')->addError($this->__('Cannot remove item')); 
        } 
       } 
      } 
     } 

     if ($this->getRequest()->getParam('product')) { 
      $cart = Mage::getSingleton('checkout/cart'); 
      $params = $this->getRequest()->getParams(); 
      $related = $this->getRequest()->getParam('related_product'); 
      $price=$this->getRequest()->getParam('price_custom'); 
      $productId = (int) $this->getRequest()->getParam('product'); 


      if ($productId) { 
       $product = Mage::getModel('catalog/product') 
        ->setStoreId(Mage::app()->getStore()->getId()) 
        ->load($productId); 
       try { 

        if (!isset($params['qty'])) { 
         $params['qty'] = 1; 
        } 

        $cart->addProduct($product, $params); 




        if (!empty($related)) { 
         $cart->addProductsByIds(explode(',', $related)); 
        } 

        $cart->save(); 

        Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
        Mage::getSingleton('checkout/session')->setCartInsertedItem($product->getId()); 

        $img = ''; 
        Mage::dispatchEvent('checkout_cart_add_product_complete', array('product'=>$product, 'request'=>$this->getRequest())); 

        $photo_arr = explode("x",Mage::getStoreConfig('mdlajaxcheckout/default/mdl_ajax_cart_image_size', Mage::app()->getStore()->getId())); 

        $img = '<img src="'.Mage::helper('catalog/image')->init($product, 'image')->resize($photo_arr[0],$photo_arr[1]).'" width="'.$photo_arr[0].'" height="'.$photo_arr[1].'" />'; 
        $message = $this->__('%s was successfully added to your shopping cart.', $product->getName()); 
        Mage::getSingleton('checkout/session')->addSuccess('<div class="mdlajax-checkout-img">'.$img.'</div><div class="mdlajax-checkout-txt">'.$message.'</div>'); 
       } 
       catch (Mage_Core_Exception $e) { 
        if (Mage::getSingleton('checkout/session')->getUseNotice(true)) { 
         Mage::getSingleton('checkout/session')->addNotice($e->getMessage()); 
        } else { 
         $messages = array_unique(explode("\n", $e->getMessage())); 
         foreach ($messages as $message) { 
          Mage::getSingleton('checkout/session')->addError($message); 
         } 
        } 
       } 
       catch (Exception $e) { 
        Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart')); 
       } 

      } 
     } 
     $this->loadLayout(); 
     $this->_initLayoutMessages('checkout/session'); 

     $this->renderLayout(); 
    } 

Observer.php

class Mdl_Ajaxcheckout_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 =$item->getRequest()->getParam('price_custom'); 
     // Set the custom price 
     $item->setCustomPrice($price); 
     $item->setOriginalCustomPrice($price); 
     // Enable super mode on the product. 
     $item->getProduct()->setIsSuperMode(true); 
    } 



} 

請建議。

+0

使用'$這 - > Request()方法 - > getParam( 'price_custom');'代替'$用品 - > Request()方法 - > getParam( 'price_custom');'看看你是否得到你的定製價格。 – Mufaddal

+0

它顯示錯誤異常:TypeError:$(...).down(...)是undefined.Above提到的線在indexcontroller.php中工作,但不在Observer.php請建議。 – Nagamani

+0

使用'Mage :: app() - > getRequest() - > getParam('price_custom')'; – Mufaddal

回答

1

Magento在將商品添加到購物車時不提供添加自定義價格的功能。這是我偶爾使用過的解決方案。

您可以使用觀察員類來收聽checkout_cart_product_add_after,並使用產品的「超級模式」根據報價項目設置自定義價格。

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

class <namespace>_<modulename>_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 = "your custom price logic"; 
     // Set the custom price 
     $item->setCustomPrice($price); 
     $item->setOriginalCustomPrice($price); 
     // Enable super mode on the product. 
     $item->getProduct()->setIsSuperMode(true); 
    } 



} 
+0

感謝Mufaddal。 Iam使用Sanorita主題爲我的網站。我使用ajax將價格傳遞到購物車。我是否必須將Observer.php放置在我的主題控制器的塊文件夾中。 – Nagamani

+0

你可以把你觀察你的模塊所以每當'checkout_cart_product_add_after'此事件觸發您的自定義價格的計算方法是火 – Mufaddal

+0

不能調用索引器方法來設置邏輯,在這個模塊文件中創建新的功能,並在這裏適用你的邏輯 – Mufaddal

相關問題