【问题标题】:Overwrite price depending custom options on Magento根据 Magento 上的自定义选项覆盖价格
【发布时间】:2015-02-13 12:42:14
【问题描述】:

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

如何告诉 Magento 根据自定义选项使用我的价格而不使用默认选项?下面是我的 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 €');

             });

         }
     }

【问题讨论】:

    标签: javascript php magento


    【解决方案1】:

    Magento 从不使用产品视图上显示的价格向客户收费,否则您可以带着萤火虫进来免费获得东西,而前端的值只是为了显示目的,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盗取代码

    【讨论】:

    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    相关资源
    最近更新 更多