Skip to content

Instantly share code, notes, and snippets.

@philcon93
Last active September 29, 2016 22:44
Show Gist options
  • Save philcon93/73a45d2543409835be29a5e6cc4b146e to your computer and use it in GitHub Desktop.
Save philcon93/73a45d2543409835be29a5e6cc4b146e to your computer and use it in GitHub Desktop.
Extra Options Update Price

Extra Options Update Price

B@se and JS

Visually update the price on product page when a customer selects an extra option that has additional fees.

<!-- buying_options.template.html-->
<!-- We need to select a price that updates based on product variation -->
[%if ![@inpromo@]%]
<input type="hidden" id="productvalue" value="[%FORMAT type:'number' dp:'2'%][@store_price@][%/FORMAT%]"/>
[%else%]
<input type="hidden" id="productvalue" value="[%FORMAT type:'number' dp:'2'%][@promo_price@][%/FORMAT%]"/>
[%/if%]
<!-- Give the select box a selector-->
[%PARAM *select_option%]
<select name="extra[@count@]" class="form-control nCustom-extraselect" id="productextra[@count@]" rel="[@SKU@]">
[@choices@]
</select>
[%/ PARAM%]
<!-- Give the options data tag for their prices -->
[%PARAM *choices%]
<option type="text" class="form-control" value="[@option_id@]" data-price="[@price@]">[%nohtml%][@text@][%end nohtml%]
[%DATA id:'price' if:'!=' value:'0'%]
(+ [%FORMAT type:'currency'%][@price@][%END FORMAT%])
[%/ DATA%]
</option>
[%/ PARAM%]
// Change product price if selected an extra option with a additional price
$('#_jstl__buying_options').on('change', '.nCustom-extraselect', function(){
var currentPrice = parseFloat($("#productvalue").val());
var selectedPrice = parseFloat($(this).find('option:selected').attr('data-price'));
var newPrice = currentPrice + selectedPrice;
$('.productprice').html($.formatCurrency(newPrice));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment