【问题标题】:Use Magento opConfig.reloadPrice() with jQuery and Custom options将 Magento opConfig.reloadPrice() 与 jQuery 和自定义选项一起使用
【发布时间】:2015-03-09 11:40:03
【问题描述】:

我正在尝试使用 Magento reloadPrice() 和 jQuery 来刷新价格。我有一个带有自定义选项的可配置产品。如果没有 jQuery,该选项的 SELECT 代码是:

<select id="select_281" class=" required-entry product-custom-option" title="" name="options[281]" onchange="opConfig.reloadPrice()">
<option value="0"></option>
<option rel="1" price="0" value="275"></option>
<option rel="2" price="0" value="276"></option>
</select>

使用 jQuery,我删除了 Prototype onchange 代码并尝试计算我的选项的价格(比如 50 美元):

jQuery('#select_281').removeAttr('onchange').change(function(){

//Price of the option to add to a basic price of the conf product
price = 50;

optionsPrice.changePrice('opConfig', price);
optionsPrice.reload();

});

可配置产品的价格:$150

添加一个选项 (SELECT):我们添加 $50

新价格$200 显示在产品页面中,但不在购物车页面中:购物车页面仅显示 150 美元,这是不正确的。

有人可以帮忙吗?

最好的问候, 通信。

【问题讨论】:

    标签: php jquery magento


    【解决方案1】:

    reloadPrice() 不能更改任何价格服务器端。 所以我们通过使用 2 个观察者解决了这个问题:checkout_cart_product_add_afterobserver 在产品第一次添加到购物车时更改价格,checkout_cart_update_items_afterobserver 更改购物车中每件商品的价格(当用户单击 “修改购物车” 按钮在购物车页面)。

    此处的代码针对具有 2 个自定义选项 numberOfColors 和 engravingType 的可配置产品进行了测试。对于每一对 numberOfColors/engravingType,我们将 tierprices 存储在特殊的 MySQL 表中,我们可以使用自定义价格更改价格。每个简单的产品都有它的 tierPrices。

    <code>//checkout_cart_product_add_after observer
       public function modifyPrice(Varien_Event_Observer $observer)
        {
            $item = $observer->getQuoteItem();
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $productType = $product->getTypeID();
            $price = 100;//Unit price of product without engraving, 100 for example
    
            //Unit price with engraving, depending of 2 custom options of configurable product
            if($productType == 'configurable'){
                //get custom options of config here
                .
                .
                $engravingPrice = 1.2;//Get unit price with engraving from a special MySQL table
                $finalUnitPrice = $price + $engravingPrice;//Final custom price
    
                //Modify the price
                $item->setCustomPrice($finalUnitPrice);
                $item->setOriginalCustomPrice($finalUnitPrice);
                $item->getProduct()->setIsSuperMode(true);
            }
        }
    
        //checkout_cart_update_items_after observer
        public function modifyCartItems(Varien_Event_Observer $observer)
        {
            foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item ) {
                if ($item->getParentItem()) {$item = $item->getParentItem();}
                $productType = $product->getTypeID();
                $productType = $product->getTypeID();
                $price = 100;//Unit price of product without engraving
    
                //Unit price with engraving, depending of 2 custom options of configurable product
                if($productType == 'configurable'){
                    .
                    .
                    $engravingPrice = 1.2;//Get unit price with engraving from a special MySQL table
                    $finalUnitPrice = $price + $engravingPrice;//Final custom price
    
                    //Modify the price for the item
                    $item->setCustomPrice($finalUnitPrice);
                    $item->setOriginalCustomPrice($finalUnitPrice);
                    $item->getProduct()->setIsSuperMode(true);
                }
            }
        }</code>
    

    但是.....仍然存在一个问题:在购物车页面上,当用户单击“编辑”链接以在产品页面中编辑产品以便更改数量时,...然后单击 “更新cart" 按钮,此更新按钮不会读取 checkout_cart_product_add_after 来刷新价格。

    不知道如何强制“更新购物车” 操作来处理checkout_cart_product_add_after 观察者中的代码?此代码是否仅在产品第一次添加到购物车时执行?

    谢谢。

    COM

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多