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