【问题标题】:Get a value of a custom input in magento在magento中获取自定义输入的值
【发布时间】:2014-03-26 09:47:04
【问题描述】:

首先,我在 magento 产品视图页面中创建了一个输入,如 bello 所述:

我已经做了一个观察者,所以它在添加到购物车时设置了一个自定义价格(checkout_cart_product_add_after 事件),这就是函数:

public function applyCustomPrice(Varien_Event_Observer $observer) {

        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getQuoteItem();
        if ($item->getParentItem()) {
            $item = $item->getParentItem();
        }

        $item->setCustomPrice(599.5);
        $item->setOriginalCustomPrice(599.5);
        $item->getProduct()->setIsSuperMode(true);

    }

如您所见,我输入了“599.5”,并且有效。 现在我想要的是在产品视图页面中向观察者获取该输入的值,这就是输入:

<div class="price-box">

    <span id="product-price-27" class="price">
        <input id="CP_ID" class="input-text price" type="text" onmouseout="onChangeCP(this);" value="2699.9900" style="width:auto;" name="custom_price"></input>
    </span>
    <input id="custom_price_total" type="hidden" value="2699.9900" name="custom_price_total"></input>

</div>

有人知道怎么做吗?

【问题讨论】:

  • 您必须创建一个自定义选项文本框并使用隐藏变量来设置您的自定义选项[价格] 值,您可以使用 getParams() 在观察者中轻松获得隐藏值
  • 我该怎么做? @KeyurShah
  • 首先,您必须为产品文本框自定义选项,然后采用一种隐藏的输入类型并单击 addtocart,您必须使用 JavaScript 将文本框值设置为隐藏变量跨度>
  • 是的,我会这样做,只是告诉我如何在观察者中做到这一点:p @KeyurShah

标签: magento


【解决方案1】:

如果您能够使用checkout_cart_product_add_after 事件成功调用您的观察者,则编写以下代码以更改产品价格

 $event = $observer->getEvent();
        $quote_item = $event->getQuoteItem();
        $new_price = Mage::app()->getRequest()->getPost('pricecustom');

pricecustom 是我的隐藏变量

        if(!is_null($new_price))
        {
            $quote_item->setCustomPrice($new_price);
            $quote_item->setOriginalCustomPrice($new_price);
            $quote_item->getProduct()->setIsSuperMode(true);
        }

如果您有任何疑问,请告诉我

【讨论】:

  • @keyur 如果需要从列表页面设置自定义产品价格,我们可以使用相同的代码吗?
  • @Slimshadddyyy 我认为您也可以在列表页面上申请,因为此事件在将产品添加到购物车 checkout_cart_product_add_after 时执行
  • @AmitBera 我面临同样的情况,当我按照这些步骤操作时,custom_price 和原始自定义价格正在正确更新,但购物车价格仍然显示原始产品价格而不是新价格?
  • 我们可以在 magento 2 中实现相同的功能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多