首页 文章

根据Magento上的自定义选项覆盖价格

提问于
浏览
0

我在Magento商店为我的产品创建了一些自定义选项 . 这些自定义选项的定价取决于某些CSV电子表格,这些电子表格已插入到自定义表格中的Magento数据库中 . 我设法改变了前端的价格(使用Javascript),但是当我按下 BUY 按钮时,Magento的价格覆盖了我的价格 .

如何根据自定义选项告诉Magento使用MY价格而不使用默认价格?以下是我的Javascript代码 . 我找到了控制前端价格的文件 options.html ,至少在继续购物车步骤之前 . 在代码结束之前可以找到我对文件的添加 .

Product.Options = Class.create();
     Product.Options.prototype = {
         initialize : function(config) {
             this.config = config;
             this.reloadPrice();
             document.observe("dom:loaded", this.reloadPrice.bind(this));
         },
         reloadPrice : function() {             var myprice = 0;
             var config = this.config;
             var skipIds = [];
             $$('body .product-custom-option').each(function(element){
                 var optionId = 0;
                 element.name.sub(/[0-9]+/, function(match){
                     optionId = parseInt(match[0], 10);
                 });
                 if (config[optionId]) {
                     var configOptions = config[optionId];
                     var curConfig = {price: 0};
                     if (element.type == 'checkbox' || element.type == 'radio') {
                         if (element.checked) {
                             if (typeof configOptions[element.getValue()] != 'undefined') {
                                 curConfig = configOptions[element.getValue()];
                             }
                         }
                     } else if(element.hasClassName('datetime-picker') && !skipIds.include(optionId)) {
                         dateSelected = true;
                         $$('.product-custom-option[id^="options_' + optionId + '"]').each(function(dt){
                             if (dt.getValue() == '') {
                                 dateSelected = false;
                             }
                         });
                         if (dateSelected) {
                             curConfig = configOptions;
                             skipIds[optionId] = optionId;
                         }
                     } else if(element.type == 'select-one' || element.type == 'select-multiple') {
                         if ('options' in element) {
                            $A(element.options).each(function(selectOption){
                                 if ('selected' in selectOption && selectOption.selected) {
                                     if (typeof(configOptions[selectOption.value]) != 'undefined') {

                                        /* csv pricing */                               
                                        curConfig = configOptions[selectOption.value];
                                        curConfig1 = configOptions[selectOption.value];
                                        //console.log(curConfig1);
                                        if(curConfig1.type !='fixed')
                                        {
                                            current = nextbitscustomprice.getCurrentPrice();

                                            currentPercent = curConfig1.priceValue;
                                            if(current !='undefined'){

                                                now = current*currentPercent /100;
                                                curConfig.price =now;
                                                curConfig.excludeTax =now;
                                                curConfig.includeTax =now;

                                                myprice = current;
                                                console.log('first'+curConfig);
                                            }
                                        }
                                        /* csv pricing */
                                     }
                                 }
                             });
                         }
                     } else {
                         if (element.getValue().strip() != '') {
                             curConfig = configOptions;
                         }
                     }
                     if(element.type == 'select-multiple' && ('options' in element)) {
                         $A(element.options).each(function(selectOption) {
                             if (('selected' in selectOption) && typeof(configOptions[selectOption.value]) != 'undefined') {
                                 if (selectOption.selected) {
                                     curConfig = configOptions[selectOption.value];
                                 } else {
                                     curConfig = {price: 0};
                                 }
                                /* csv pricing */                               
                                if(element.type == 'select-multiple' && selectOption.selected){                             
                                    curConfig1 = configOptions[selectOption.value];
                                    //console.log(curConfig1);
                                    if(curConfig1.type !='fixed')
                                    {
                                        current = nextbitscustomprice.getCurrentPrice();

                                        currentPercent = curConfig1.priceValue;
                                        if(current !='undefined'){

                                            now = current*currentPercent /100;
                                            curConfig.price =now;
                                            curConfig.excludeTax =now;
                                            curConfig.includeTax =now;
                                            myprice = current;
                                            console.log('second'+curConfig);
                                        }
                                    }
                                }
                                /* csv pricing */


                                 optionsPrice.addCustomPrices(optionId + '-' + selectOption.value, curConfig);
                                 optionsPrice.reload();
                             }
                         });

                     } else {
                        if (element.type == 'checkbox' || element.type == 'radio' ) {
                            if (element.checked) {
                                if (typeof configOptions[element.getValue()] != 'undefined') {
                                    curConfig1 =configOptions[element.getValue()];
                                    //console.log(curConfig1);
                                    if(curConfig1.type !='fixed')
                                    {
                                        current = nextbitscustomprice.getCurrentPrice();

                                        currentPercent = curConfig1.priceValue;
                                        if(current !='undefined'){

                                            now = current*currentPercent /100;
                                            curConfig.price =now;
                                            curConfig.excludeTax =now;
                                            curConfig.includeTax =now;
                                            myprice = current;
                                            console.log('third'+curConfig);
                                        }
                                    }
                                }
                            }
                        }

                         optionsPrice.addCustomPrices(element.id || optionId, curConfig);
                         optionsPrice.reload();


                     }
                 }

                //Global Javascript object
                var addedPriceFromMyTableInDB = 12;
                optionsPrice['productPrice'] += addedPriceFromMyTableInDB;
                var myPrice = optionsPrice['productPrice'] + addedPriceFromMyTableInDB;
                console.log('price '+myPrice);
                jQuery('span.price').text(myPrice+',00 €');

             });

         }
     }

1 回答

  • 2

    Magento从不使用产品视图上显示的价格向客户收费,否则你可以免费获得萤火虫并获取东西,而前端的 Value 仅用于显示目的,Magento将重新计算价格 . 产品在添加到购物车之前 . 您可以挂钩事件以在将项目添加到报价之前更改该项目的价格 .

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

    在$ new_price上,您将设置所需的值 .

    无耻地偷走了以下答案的代码Changing the price in quote while adding product to cart: magento

相关问题