【问题标题】:Pass dynamic price from detailed page to cart in magento将动态价格从详细页面传递到magento中的购物车
【发布时间】:2013-05-11 07:55:44
【问题描述】:

我是 magento 的初学者。我需要将动态价格从详细页面传递到购物车。

现在,当我传递动态价格时,它不会在购物车中更新,而是被产品的原始价格取代。

任何帮助将不胜感激。 在这条线上,我得到了价格价值。 $price=$this->getRequest()->getParam('price_custom'); 索引控制器.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);
    }



}

请提出建议。

【问题讨论】:

  • 使用$this-&gt;getRequest()-&gt;getParam('price_custom');而不是$item-&gt;getRequest()-&gt;getParam('price_custom');,看看你是否得到了你的自定义价格。
  • 显示错误异常:TypeError: $(...).down(...) is undefined.上述行在 indexcontroller.php 中有效,但在 Observer.php 中无效 请建议。
  • 使用Mage::app()-&gt;getRequest()-&gt;getParam('price_custom');
  • 我再次收到同样的错误。如果我将价格传递给observer.php 中的$price 变量,我的7 天噩梦将结束Mufaddal。
  • Mufaddal 我得到了在本地工作的代码,但是当我将相同的代码移动到 Linux 服务器时,观察者事件没有被触发。请帮忙

标签: magento dynamic shopping-cart cart


【解决方案1】:

您必须在 get_final_price 上为自定义价格创建观察者

请查看以下链接

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

在哪里应用价格折扣。我认为这是帮助。如果您仍有问题,请告诉我。

【讨论】:

    【解决方案2】:

    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>
    

    然后在 /app/code/local/{namespace}/{yourmodule}/Model/Observer.php 创建一个 Observer 类

     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);
        }
    
    
    
    }
    

    【讨论】:

    • 谢谢穆法达尔。我正在为我的网站使用 Sanorita 主题。主题使用 ajax 将价格传递给购物车。我是否必须将 Observer.php 放在我的主题控制器的块文件夹中。
    • 您可以将您的观察者放入您的模块中,这样每当checkout_cart_product_add_after 触发此事件时,您的自定义价格计算方法就会触发
    • 你不能调用索引控制器方法来设置逻辑,在这个模块文件中创建新函数并在这里应用你的逻辑
    • 我只需要传递价格。我没有使用折扣或任何逻辑。请参阅以下链接:linenorder.com/table-skirts/…
    • 好的,只需在 $price 变量中指定一些价格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多