var PC=PC||{};
var tax_info=tax_info||null;
(function($, _){
var should_update_prices=false;
let product_qty_changed=false;
let base_product_price=0;
if(! wp||! wp.hooks) return;
wp.hooks.addAction('PC.fe.start', 'mkl.pc.extra-price', function(configurator){
if(configurator.$('.extra-cost').length) configurator.$el.addClass('display-extra-price-total');
});
wp.hooks.addAction('PC.fe.init.modal', 'mkl.pc.extra-price', ()=> {
base_product_price=null;
should_update_prices=true;
});
wp.hooks.addAction('PC.fe.choice.change', 'mkl.pc.extra-price', set_sub_total);
wp.hooks.addAction('PC.fe.form.item.change', 'mkl.pc.extra-price', formItemChange, 20);
wp.hooks.addAction('PC.fe.open', 'mkl.pc.extra-price', set_sub_total);
wp.hooks.addAction('PC.fe.qty_changed', 'mkl.pc.extra-price', on_qty_change, 50);
wp.hooks.addAction('PC.fe.linked_product_qty.updated', 'mkl.pc.extra-price', function(){
set_sub_total()
});
wp.hooks.addAction('mkl_checked_conditions', 'mkl.pc.extra-price', set_sub_total);
wp.hooks.addAction('mkl_checked_conditions.clicked', 'mkl.pc.extra-price', set_sub_total);
wp.hooks.addAction('PC.fe.choice.change', 'mkl.pc.extra-price', calculateRelativePrice, 900);
function calculateRelativePrice(choice){
if(PC_config.config.relative_price&&'simple'==choice.collection.layer_type){
var current_price=get_extra_price(choice)||0;
if(! choice.collection.findWhere('active') ){
current_price=0;
}
choice.collection.each(function(item){
var item_price=get_extra_price(item)||0;
item.set('extra_price_to_display', item_price - current_price);
});
}else{
choice.set('extra_price_to_display', wp.hooks.applyFilters('PC.extra_price_to_display', get_extra_price(choice), choice) );
}}
wp.hooks.addAction('PC.fe.reset_configurator', 'mkl.pc.extra-price', function(){
set_sub_total();
});
wp.hooks.addAction('PC.fe.configurator.choice-item.render.after-template', 'mkl.pc.extra-price', function(choice){
if(! $('#tmpl-mkl-pc-extra-price').length) return;
if(! wp.hooks.applyFilters('PC.fe.show.extra_price', true, choice, 'choice-item') ) return;
var ep=new ExtraPrice( { model: choice.model, context: 'toolbar' });
if(choice.$(' > .choice-item .choice-text--container').length){
choice.$(' > .choice-item .choice-text--container').append(ep.$el);
}else{
choice.$(' > .choice-item').append(ep.$el);
}
choice.extra_price=ep;
});
wp.hooks.addAction('PC.fe.configurator.summary-item.render.after-template', 'mkl.pc.extra-price', function(summary_item){
if(! $('#tmpl-mkl-pc-extra-price').length) return;
if(! wp.hooks.applyFilters('PC.fe.show.extra_price', true, summary_item.model, 'summary-item') ) return;
var ep=new ExtraPrice( { model: summary_item.model, no_relative_price: true, show_plus_sign: false, context: 'summary' });
summary_item.$el.append(ep.$el);
});
wp.hooks.addAction('PC.fe.models.choice.init', 'mkl.pc.extra-price', function(choice){
choice.set('extra_price_to_display', wp.hooks.applyFilters('PC.extra_price_to_display', get_extra_price(choice), choice) );
}, 30);
function formItemChange(model, event){
if('select'==model.get('text_field_type') ){
var items=model.get('select_field_options');
var selection=event.target.selectedIndex;
if(items[selection]&&'undefined'!==typeof items[selection].option_price&&''!==items[selection].option_price){
model.set('extra_price', items[selection].option_price);
model.set('extra_price_from_form_field', true);
}else{
if(model.get('extra_price_from_form_field') ) model.set('extra_price', 0);
}}else if('radio'==model.get('text_field_type') ){
var items=model.get('select_field_options');
var selection=model.get('field_value') - 1;
if(items[selection]&&'undefined'!==typeof items[selection].option_price&&''!==items[selection].option_price){
model.set('extra_price', items[selection].option_price);
model.set('extra_price_from_form_field', true);
}else{
if(model.get('extra_price_from_form_field') ) model.set('extra_price', 0);
}}
}
function get_sub_total(context){
var subtotal=0;
var has_calculation_error=0;
var forRegularTotal=context&&context.forRegularTotal;
if(PC.fe.layers){
PC.fe.layers.each(function(layer){
if(item_is_hidden(layer) ) return;
var layer_contents=PC.fe.getLayerContent(layer.id)
if(! layer_contents) return;
layer_contents.each(function(choice){
if(item_is_hidden(choice) ) return;
if('form'==layer.get('type')||choice.get('has_text_field') ){
if('form'!=layer.get('type')&&! choice.get('active') ) return;
var field_type=choice.get('text_field_type');
if(( 'number'==field_type||'mkl_quantity'==field_type)&&choice.get('field_value')&&choice.get('price_per_unit') ){
var field_price=parseFloat(choice.get('field_value') ) * parseFloat(choice.get('price_per_unit') );
if(field_price) subtotal +=field_price;
}
if('form'==layer.get('type')&&('select'==field_type||'radio'==field_type)&&choice.get('extra_price') ){
var field_price=parseFloat(choice.get('extra_price') );
if(field_price) subtotal +=field_price;
}
if('calculation'==field_type){
var formula=choice.get('number_field_calculation_formula');
if(formula){
if(forRegularTotal){
var calculated_regular;
if(window.pc_extra_price_debug_formula){
console.log(parse_formula(formula, choice, 'number', { useLinkedRegular: true }), formula, choice);
}
try {
calculated_regular=mexp.eval(parse_formula(formula, choice, 'number', { useLinkedRegular: true }) );
} catch(error){
console.error('Caculation error: ', error);
console.error('Formula', parse_formula(formula, choice, 'number', { useLinkedRegular: true }), choice);
console.trace(error);
if(! has_calculation_error){
has_calculation_error=1;
alert('There seems to be an error in the calculation field. Check the browser console for details');
}}
var calculation_min_reg=choice.get('calculation_min_value');
var calculation_max_reg=choice.get('calculation_max_value');
if(! isNaN(calculated_regular) ){
if('undefined'!=typeof calculation_min_reg&&calculated_regular < calculation_min_reg){
calculated_regular=calculation_min_reg;
}else if('undefined'!=typeof calculation_max_reg&&calculation_max_reg > calculation_min_reg&&calculated_regular > calculation_max_reg){
calculated_regular=calculation_max_reg;
}
subtotal +=calculated_regular;
}}else if(update_calculation){
var calculated_price;
if(window.pc_extra_price_debug_formula){
console.log(parse_formula(formula, choice, 'number'), formula, choice);
}
try {
calculated_price=mexp.eval(parse_formula(formula, choice, 'number') );
} catch(error){
console.error('Caculation error: ', error);
console.error('Formula', parse_formula(formula, choice, 'number'), choice);
console.trace(error);
if(! has_calculation_error){
has_calculation_error=1;
alert('There seems to be an error in the calculation field. Check the browser console for details');
}}
var calculation_min_value=choice.get('calculation_min_value');
var calculation_max_value=choice.get('calculation_max_value');
if(! isNaN(calculated_price) ){
if('undefined'!=typeof calculation_min_value&&calculated_price < calculation_min_value){
calculated_price=calculation_min_value;
}else if('undefined'!=typeof calculation_max_value&&calculation_max_value > calculation_min_value&&calculated_price > calculation_max_value){
calculated_price=calculation_max_value;
}
choice.set('extra_price', calculated_price);
}}
if(! forRegularTotal){
subtotal +=choice.get('extra_price');
}}
}
if(choice.get('extra_price')&&('text'==field_type||'textarea'==field_type||'checkbox'==field_type)&&'form'==layer.get('type') ){
var content=choice.get('field_value');
if(content){
subtotal +=choice.get('extra_price');
}}
}
wp.hooks.doAction('PC.fe.extra_price.before.add.extra_price', choice, layer);
if(! choice.get('active') ) return;
var item_price=get_extra_price(choice, context);
subtotal +=wp.hooks.applyFilters('PC.fe.extra_price.item_price', item_price, choice, layer);
});
});
subtotal=wp.hooks.applyFilters('PC.fe.extra_price.subtotal.before_apply_taxes', subtotal);
if(subtotal) subtotal=maybe_apply_taxes(subtotal);
}
return wp.hooks.applyFilters('PC.fe.extra_price.subtotal', subtotal);
}
function on_qty_change(){
product_qty_changed=true;
set_sub_total();
product_qty_changed=false;
}
function set_sub_total(){
var subtotal=get_sub_total()||0;
var subtotalRegular=get_sub_total( { forRegularTotal: true })||0;
if(subtotal > 0){
$('.pc-price').html(' + '+ PC.utils.formatMoney(subtotal) );
$('.extra-cost').addClass('show');
}else if(subtotal < 0){
$('.pc-price').html(' ' + PC.utils.formatMoney(subtotal) );
$('.extra-cost').addClass('show');
}else{
$('.pc-price').html('');
$('.extra-cost').removeClass('show');
}
const base_price=get_product_price();
if($('.pc-total-price').length){
if(PC_config.config.woocs_is_fixed_enabled
||('undefined'!=typeof PC.fe.config.wcml_rate&&parseFloat(PC.fe.config.wcml_rate) )
){
$('.pc-total-price').html(PC.utils.formatMoney(( base_price + parseFloat(PC.utils.maybeConvertAmountToCurrency(subtotal) )), false), false);
if(PC.fe.currentProductData.product_info.regular_price&&PC.fe.currentProductData.product_info.is_on_sale&&$('.pc-total--regular-price').length){
$('.pc-total--regular-price').html(PC.utils.formatMoney(( parseFloat(PC.fe.currentProductData.product_info.regular_price) + parseFloat(PC.utils.maybeConvertAmountToCurrency(subtotalRegular) )), false), false);
}}else{
$('.pc-total-price').html(PC.utils.formatMoney(base_price + parseFloat(subtotal) ));
if(PC.fe.currentProductData.product_info.regular_price&&PC.fe.currentProductData.product_info.is_on_sale&&$('.pc-total--regular-price').length){
$('.pc-total--regular-price').html(PC.utils.formatMoney(( parseFloat(PC.fe.currentProductData.product_info.regular_price) + parseFloat(subtotalRegular) )) );
}}
}
wp.hooks.doAction('PC.fe.extra_price.after.setSubTotal', base_price, subtotal);
}
var formula_warning={
done: [],
show: function(message_id, message, always){
if(always||! this.done[message_id]){
console.warn('Configurator formula - ' + message_id, message);
this.done[message_id]=1;
}}
}
var get_formula_callbacks=function(formula_model, formulaOptions){
if(! PC.fe.formula_callbacks||'object'!==typeof PC.fe.formula_callbacks){
PC.fe.formula_callbacks={};}
return wp.hooks.applyFilters('PC.fe.extra_price.formula_callbacks', PC.fe.formula_callbacks, formula_model, formulaOptions)||{};};
var parse_formula_function_placeholder=function(formula, start_index){
var prefix='{function:',
index=start_index + prefix.length,
function_name='',
paren_depth=0,
brace_depth=1,
args_start=-1;
while(index < formula.length){
var character=formula.charAt(index);
if('('===character){
args_start=index + 1;
paren_depth=1;
index++;
break;
}
if('}'===character){
return {
raw: formula.slice(start_index, index + 1),
name: function_name.trim(),
args_string: '',
next_index: index + 1,
};}
function_name +=character;
index++;
}
if(-1===args_start) return null;
while(index < formula.length){
var character=formula.charAt(index);
if('{'===character){
brace_depth++;
}else if('}'===character){
brace_depth--;
if(0===brace_depth&&0===paren_depth){
return {
raw: formula.slice(start_index, index + 1),
name: function_name.trim(),
args_string: formula.slice(args_start, index - 1),
next_index: index + 1,
};}}else if('('===character){
paren_depth++;
}else if(')'===character){
paren_depth--;
}
index++;
}
return null;
};
var split_formula_function_args=function(args_string){
var args=[],
current='',
paren_depth=0,
brace_depth=0;
if(! args_string) return [];
for(var i=0; i < args_string.length; i++){
var character=args_string.charAt(i);
if('('===character){
paren_depth++;
}else if(')'===character){
paren_depth--;
}else if('{'===character){
brace_depth++;
}else if('}'===character){
brace_depth--;
}
if(','===character&&0===paren_depth&&0===brace_depth){
args.push(current.trim());
current='';
continue;
}
current +=character;
}
if(current.length||args_string.indexOf(',')!==-1){
args.push(current.trim());
}
return args;
};
var evaluate_formula_function_arg=function(arg_expression, formula_model, data_type, formulaOptions){
var expression=(arg_expression||'').trim();
if(! expression) return 0;
expression=resolve_formula_function_placeholders(expression, formula_model, data_type, formulaOptions);
if(! expression) return 0;
if(! isNaN(expression) ){
return parseFloat(expression);
}
return mexp.eval(expression);
};
var evaluate_formula_function_placeholder=function(placeholder, formula_model, data_type, formulaOptions){
var callbacks=get_formula_callbacks(formula_model, formulaOptions),
callback=callbacks[ placeholder.name ],
args=[],
context={
formula_model: formula_model,
data_type: data_type,
formula_options: formulaOptions,
placeholder: placeholder.raw,
};
if(! placeholder.name){
formula_warning.show('function_placeholder_invalid', [ 'Configurator formula callback placeholder is invalid.', { Placeholder: placeholder.raw } ], true);
return 0;
}
if('function'!==typeof callback){
formula_warning.show('function_callback_missing', [ 'Configurator formula callback was not found; the value was defaulted by 0.', { Function: placeholder.name, Placeholder: placeholder.raw } ], true);
return 0;
}
try {
args=split_formula_function_args(placeholder.args_string).map(function(arg_expression){
return evaluate_formula_function_arg(arg_expression, formula_model, data_type, formulaOptions);
});
} catch(error){
formula_warning.show('function_callback_arguments', [ 'Configurator formula callback arguments could not be evaluated; the value was defaulted by 0.', { Function: placeholder.name, Placeholder: placeholder.raw, Error: error.message } ], true);
return 0;
}
try {
var result=callback.apply(window, args.concat(context) );
if(null===result||''===result||'undefined'===typeof result||isNaN(result) ){
formula_warning.show('function_callback_result', [ 'Configurator formula callback returned an invalid value; the value was defaulted by 0.', { Function: placeholder.name, Placeholder: placeholder.raw, Result: result } ], true);
return 0;
}
return parseFloat(result);
} catch(error){
formula_warning.show('function_callback_error', [ 'Configurator formula callback threw an error; the value was defaulted by 0.', { Function: placeholder.name, Placeholder: placeholder.raw, Error: error.message } ], true);
return 0;
}};
var resolve_formula_function_placeholders=function(formula, formula_model, data_type, formulaOptions){
var prefix='{function:',
index=0,
resolved_formula='';
if(-1===formula.indexOf(prefix) ) return formula;
while(index < formula.length){
var start_index=formula.indexOf(prefix, index);
if(-1===start_index){
resolved_formula +=formula.slice(index);
break;
}
resolved_formula +=formula.slice(index, start_index);
var placeholder=parse_formula_function_placeholder(formula, start_index);
if(! placeholder){
resolved_formula +=formula.slice(start_index, start_index + prefix.length);
index=start_index + prefix.length;
continue;
}
resolved_formula +=evaluate_formula_function_placeholder(placeholder, formula_model, data_type, formulaOptions);
index=placeholder.next_index;
}
return resolved_formula;
};
var parse_formula=function(formula, formula_model, data_type, formulaOptions){
var fields=formula.match(new RegExp(/{field:(.*?)}/g) );
var formula_with_values=formula;
if(fields){
fields=fields.map(function(field){
var key=field.replace('}', '').replace('{field:', '');
var field_model=formula_model.collection.findWhere({ 'text_field_id': key });
if('undefined'==typeof field_model) return;
if('undefined'==typeof data_type||'number'==data_type){
var value=field_model.get('field_value');
if('select'==field_model.get('text_field_type')||'radio'==field_model.get('text_field_type') ){
var option=field_model.get('select_field_options')[ value - 1 ];
if(option) value=option.value;
}
if(! value||isNaN(value) ) value=0;
}else if('labels'==data_type){
var value=field_model.get('name');
if(! value) value='';
}
formula_with_values=formula_with_values.replace('{field:' + key + '}', value);
});
}
var single_layers=formula.matchAll(new RegExp(/{simple \(.*?\):(.*?)(\|default:(.*?))?}/g) );
for(var sl of single_layers){
var choices=PC.fe.getLayerContent(parseInt(sl[1]) )
if(! choices){
formula_warning.show('single', [ 'Configurator - price calculation: a single choice layer was not found; the value was defaulted by 0', { 'Layer ID': sl[1] } ]);
if(sl[ 3 ]){
formula_with_values=formula_with_values.replace(sl[0], sl[3]);
}else{
formula_with_values=formula_with_values.replace(sl[0], 0);
}}else{
var selection=choices.findWhere({ active: true });
if(selection&&item_is_hidden(selection) ){
if(sl[ 3 ]){
formula_with_values=formula_with_values.replace(sl[0], sl[3]);
}else{
formula_with_values=formula_with_values.replace(sl[0], 0);
}}else if(selection&&'undefined'!=typeof selection.get('calculation_value')&&! isNaN(parseFloat(selection.get('calculation_value') )) ){
formula_with_values=formula_with_values.replace(sl[0], selection.get('calculation_value') );
}else if(selection&&typeof selection.get('has_text_field')&&'number'==selection.get('text_field_type') ){
formula_with_values=formula_with_values.replace(sl[0], selection.get('field_value') );
}else{
if(sl[ 3 ]){
formula_with_values=formula_with_values.replace(sl[0], sl[3]);
}else{
formula_with_values=formula_with_values.replace(sl[0], 0);
}}
}}
var single_layers_linked_product=formula.matchAll(new RegExp(/{simple_link \(.*?\):(.*?)(\|default:(.*?))?}/g) );
for(var sl of single_layers_linked_product){
var choices=PC.fe.getLayerContent(parseInt(sl[1]) ),
selection=false;
if(choices){
selection=choices.findWhere({ active: true });
}else{
formula_warning.show('single_linked', [ 'Configurator - price calculation: a single choice layer was not found; the value was defaulted by 0', { 'Layer ID': sl[1] } ]);
}
var simpleLinkPrice=null;
if(selection&&! item_is_hidden(selection) ){
if(formulaOptions&&formulaOptions.useLinkedRegular){
var simpleReg=selection.get('linked_product_regular_price');
if('undefined'!=typeof simpleReg&&null!==simpleReg&&''!==simpleReg&&! isNaN(parseFloat(simpleReg) )){
simpleLinkPrice=simpleReg;
}}
if(null===simpleLinkPrice&&'undefined'!=typeof selection.get('linked_product_price') ){
simpleLinkPrice=selection.get('linked_product_price');
}}
if(selection&&null!==simpleLinkPrice&&! item_is_hidden(selection) ){
formula_with_values=formula_with_values.replace(sl[0], simpleLinkPrice);
}else{
if(sl[ 3 ]){
formula_with_values=formula_with_values.replace(sl[0], sl[3]);
}else{
formula_with_values=formula_with_values.replace(sl[0], 0);
}}
}
var multiple_layers=formula.matchAll(new RegExp(/{multiple \(.*?\):(.*?):(.*?)}/g) );
for(var ml of multiple_layers){
var choices=PC.fe.getLayerContent(parseInt(ml[1]) );
if(choices){
var selection=choices.get(parseInt(ml[2]) );
if(selection&&(item_is_hidden(selection)||! selection.get('active') )){
formula_with_values=formula_with_values.replace(ml[0], 0);
}else if(selection&&selection.get('has_text_field')&&'number'==selection.get('text_field_type') ){
var value='undefined'!=typeof selection.get('field_value') ? selection.get('field_value'):0;
formula_with_values=formula_with_values.replace(ml[0], value);
}else if(selection&&'undefined'!=typeof selection.get('calculation_value') ){
formula_with_values=formula_with_values.replace(ml[0], selection.get('calculation_value') );
}else{
if(! selection){
formula_warning.show('multiple_choice', [ 'Configurator - price calculation: a multiple choice item was not found; the value was defaulted by 0', { 'Layer ID': ml[1], 'Choice ID': ml[2] } ]);
}
formula_with_values=formula_with_values.replace(ml[0], 0);
}}else{
formula_warning.show('multiple_layer', [ 'Configurator - price calculation: a multiple choice layer was not found; the value was defaulted by 0', { 'Layer ID': ml[1], 'Choice ID': ml[2] } ]);
formula_with_values=formula_with_values.replace(ml[0], 0);
}}
var multiple_layers_linked_product=formula.matchAll(new RegExp(/{multiple_link \(.*?\):(.*?):(.*?)}/g) );
for(var mll of multiple_layers_linked_product){
var choices=PC.fe.getLayerContent(parseInt(mll[1]) );
if(choices){
var selection=choices.get(parseInt(mll[2]) );
}else{
var selection=false;
formula_warning.show('multiple_layer_linked', [ 'Configurator - price calculation: a multiple choice layer was not found; the value was defaulted by 0', { 'Layer ID': mll[1], 'Choice ID': mll[2] } ]);
}
var multiLinkPrice=null;
if(selection&&! item_is_hidden(selection)&&selection.get('active') ){
if(formulaOptions&&formulaOptions.useLinkedRegular){
var multiReg=selection.get('linked_product_regular_price');
if('undefined'!=typeof multiReg&&null!==multiReg&&''!==multiReg&&! isNaN(parseFloat(multiReg) )){
multiLinkPrice=multiReg;
}}
if(null===multiLinkPrice&&'undefined'!=typeof selection.get('linked_product_price') ){
multiLinkPrice=selection.get('linked_product_price');
}}
if(selection&&null!==multiLinkPrice&&! item_is_hidden(selection)&&selection.get('active') ){
formula_with_values=formula_with_values.replace(mll[0], multiLinkPrice);
}else{
formula_warning.show('multiple_choice_linked', [ 'Configurator - price calculation: a multiple choice layer was not found; the value was defaulted by 0', { 'Layer ID': mll[1], 'Choice ID': mll[2] } ]);
if(mll[ 3 ]){
formula_with_values=formula_with_values.replace(mll[0], mll[3]);
}else{
formula_with_values=formula_with_values.replace(mll[0], 0);
}}
}
var product_price=formula.matchAll(new RegExp(/{product_price}/g) );
for(var pp of product_price){
formula_with_values=formula_with_values.replace(pp[0], get_product_price());
}
var count_unique_values=formula_with_values.matchAll(new RegExp(/(count_unique_values\((.*?)\))/g) );
for(var uv of count_unique_values){
var items=uv[2].split(',');
items=items.map(function(item){
return parseInt(item.trim());
});
formula_with_values=formula_with_values.replace(uv[0], _.unique(items).length);
}
formula_with_values=resolve_formula_function_placeholders(formula_with_values, formula_model, data_type, formulaOptions);
formula_with_values=wp.hooks.applyFilters('formula_with_values', formula_with_values, formula, formula_model, data_type);
formula_with_values=formula_with_values.replaceAll(/{.*?}/ig, 0);
return formula_with_values;
}
var get_extra_price=function(choice, context){
var ep=0;
if(! choice.get('extra_price') ){
ep=0;
}else if(! choice.get('extra_price_type')||'amount'==choice.get('extra_price_type') ){
ep=choice.get('extra_price');
}else if('percentage'==choice.get('extra_price_type') ){
ep=choice.get('extra_price') * get_product_price() / 100;
}
return wp.hooks.applyFilters('pc.get_extra_price', ep, choice, context);
};
var maybe_apply_taxes=function(amount, return_inclusive=null){
if(!tax_info||!PC_config.config.tax_enabled) return amount;
const Settings=PC_config.config.tax_settings;
const compound_rates=[];
const normal_rates=[];
let total_normal_rate=0;
const should_return_inclusive=return_inclusive!==null
? return_inclusive
: Settings.tax_display_mode==='incl';
if(Settings.prices_include_tax &&
should_return_inclusive &&
!Settings.adjust_non_base_location_prices
){
return amount;
}
if(!Settings.prices_include_tax
&& !should_return_inclusive
){
return amount;
}
_.each(tax_info.tax_rates, function(rate){
if(rate.compound==='yes'){
compound_rates.push(rate);
}else{
normal_rates.push(rate);
total_normal_rate +=rate.rate / 100;
}});
if(Settings.prices_include_tax){
amount=amount * tax_info.price_without_tax_ratio;
}
if(should_return_inclusive){
let tax_amount=0;
_.each(normal_rates, function(rate){
tax_amount +=amount *(rate.rate / 100);
});
let price_with_taxes=amount + tax_amount;
_.each(compound_rates, function(rate){
price_with_taxes +=price_with_taxes *(rate.rate / 100);
});
return price_with_taxes;
}
return amount;
};
function get_price_without_tax(amount, default_value){
if(tax_info?.price_without_tax_ratio){
return amount * tax_info?.price_without_tax_ratio;
}
if(tax_info?.price_with_tax_ratio){
return amount / tax_info?.price_with_tax_ratio;
}
return('undefined'!=typeof default_value) ? default_value:amount;
}
function get_product_price(){
if(!base_product_price||product_qty_changed){
if(PC?.fe?.get_product_price){
base_product_price=PC.fe.get_product_price();
}else{
base_product_price=parseFloat(PC.fe.currentProductData.product_info.price)||0;
}}
return base_product_price;
}
function get_tax_rates(){
var configurator=$('.configure-product[data-product_id]');
if(! configurator.length) return [];
wp.ajax.send('mkl_pc_get_tax_rates', {
data: {
product_id: configurator.data('product_id'),
},
success: function(data){
if('taxable'!=data.prod_tax_status) return;
tax_info=data;
should_update_prices=true;
console.log('got taxes');
wp.hooks.doAction('PC.fe.extra_price.after.get_tax_rates');
if(PC.fe&&PC.fe.modal){
PC.fe.modal.trigger('PC.fe.extra_price.update_taxes');
}
set_sub_total();
wp.hooks.doAction('PC.fe.extra_price.after.update_price');
},
error: function (data){
console.log(data);
}});
}
function item_is_hidden(item){
if(PC.hasOwnProperty('conditionalLogic')&&PC.conditionalLogic){
if(PC.conditionalLogic.hasOwnProperty('item_is_hidden')&&PC.conditionalLogic.item_is_hidden(item) ) return true;
}
return false;
}
var ExtraPrice=Backbone.View.extend({
template: wp.template('mkl-pc-extra-price'),
tagName: 'span',
className: 'choice-price',
initialize: function(options){
this.options=options;
this.listenTo(this.model.collection, 'change:extra_price_to_display', this.render);
this.listenTo(this.model, 'change:on_sale', this.render);
this.listenTo(PC.fe.modal, 'PC.fe.extra_price.update_taxes', this.check_price);
wp.hooks.addAction('PC.fe.start', 'mkl.pc.extra-price', this.check_price.bind(this), 30);
this.render();
},
render: function(){
var data=_.extend({}, this.model.attributes, { show_plus_sign:(this.options.show_plus_sign||true) });
if(true===this.options.no_relative_price&&get_extra_price(this.model) ){
data.no_relative_price=true;
data.formatted_price=PC.utils.formatMoney(Math.abs(wp.hooks.applyFilters('PC.ExtraPrice.view.extra_price_to_display', maybe_apply_taxes(get_extra_price(this.model) ), this.model, this) )) ;
}else if(this.model.get('extra_price_to_display') ){
data.formatted_price=PC.utils.formatMoney(Math.abs(wp.hooks.applyFilters('PC.ExtraPrice.view.extra_price_to_display', maybe_apply_taxes(this.model.get('extra_price_to_display') ), this.model, this) )) ;
}else{
data.formatted_price=false;
}
data=wp.hooks.applyFilters('PC.ExtraPrice.view.data', data, this.model, this);
this.$el.html(this.template(data) );
this.$el.toggleClass('no-price', ! this.model.get('extra_price_to_display') );
this.$el.toggleClass('on-sale', !!this.model.get('on_sale') );
},
check_price: function(){
if(this.model.get('active')&&'simple'==this.model.collection.layer_type){
calculateRelativePrice(this.model);
}else if(! this.model.collection.findWhere('active', true)||'multiple'==this.model.collection.layer_type){
this.model.set('extra_price_to_display', wp.hooks.applyFilters('PC.extra_price_to_display', get_extra_price(this.model), this.model) );
}
this.render();
}});
$(function(){
if(PC_config.config.tax_enabled) get_tax_rates();
set_sub_total();
});
window.pc_maybe_apply_taxes=maybe_apply_taxes;
window.pc_get_extra_price=get_extra_price;
window.pc_extra_price_get_sub_total=get_sub_total;
window.pc_get_price_without_tax=get_price_without_tax;
})(jQuery, PC._us||window._);