0

I have created some custom options for my products in my Magento shop. The pricing of those custom options depends on some CSV spreadsheets, which were inserted into the Magento database in custom tables. I 've managed to alter the price in frontend (with Javascript), yet when I hit the BUY button the Magento's price overwrite mine.

How can I tell Magento to use MY price depending on custom options and don't use the default one? Below is my Javascript code. I found the file options.html which controls the price in the frontend, at least before proceeding in the shopping cart step. Before the code ends can find my addition to the file.

  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 €');

             });

         }
     }
Ricardus
  • 739
  • 2
  • 8
  • 15

1 Answers1

2

Magento never uses the prices that are displayed on the product view to charge customers, otherwise you could come in with firebug and get stuff for free, instead the values on the frontend are there just for display purposes, Magento will go recalculate the price of the product before it's added to the cart. You can hook into an event to change the price of the Item before it is added to the Quote.

<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>

And then on the observer class

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->save();
}

On $new_price you will set the value you want.

Shamelessly stole code from the following answer Changing the price in quote while adding product to cart: magento

Community
  • 1
  • 1
changeling
  • 778
  • 1
  • 8
  • 20