我正在努力为我所雇用的公司建立一个网站,我们销售可定制的相册很多,所以我们不能像简单的产品一样销售。 我正在使用WordPress与WooCommerce woocommerce属性变量并没有给我足够的选择,我需要一个计算器而不是变化。 所以我创建了一个,它看起来像这样:
<div id="calculator_br">
<form action="">
<!-- <div id="covertype">
<h4>Tip coperta:</h4>
<div id="selector">
<input type="radio" name="hm"> Handmade
<span class="hidden hm">
<p id="hm2020">50</p>
<p id="hm2525">85</p>
<p id="hm3030">105</p>
<p id="hm2030">85</p>
<p id="hm2535">105</p>
<p id="hm3040">105</p>
<p id="hm2519">85</p>
<p id="hm3022.5">95</p>
</span>
<input type="radio" name="pe"> Piele Ecologica
<span class="hidden pe">
<p id="hm2020">45</p>
<p id="hm2525">70</p>
<p id="hm3030">90</p>
<p id="hm2030">70</p>
<p id="hm2535">90</p>
<p id="hm3040">100</p>
<p id="hm2519">70</p>
<p id="hm3022.5">80</p>
</span>
</div>
</div> -->
<div id="dims">
<p>Dimensiune:</p>
<span class="size-form">
<p>Patrat:</p>
<input type="radio" name="size" filaprc="6.5" copertaprc="50" faceoffprc="0" old="25" checked="checked">20x20cm </br>
<input type="radio" name="size" filaprc="9" copertaprc="85" faceoffprc="0" old="30">25x25cm </br>
<input type="radio" name="size" filaprc="10" copertaprc="105" faceoffprc="0" old="35">30x30cm </br>
</span>
<span class="size-form">
<p>Landscape:</p>
<input type="radio" name="size" filaprc="9" copertaprc="85" faceoffprc="0" old="30">20x30cm </br>
<input type="radio" name="size" filaprc="10" copertaprc="105" faceoffprc="0" old="35">25x35cm </br>
<input type="radio" name="size" filaprc="12.5" copertaprc="115" faceoffprc="0" old="40">30x40cm </br>
</span>
<span class="size-form">
<p>Portret:</p>
<input type="radio" name="size" filaprc="8" copertaprc="85" faceoffprc="0" old="25">25x19cm </br>
<input type="radio" name="size" filaprc="9" copertaprc="95" faceoffprc="0" old="30">30x22.5cm </br>
</span>
</div>
<div id="col">
<p>Numar Colaje:</p>
<input type="range" name="colaje" min="5" max="25" filaprc="15"><p id="range">15</p>
</div>
<div id="fof_wrp">
<p>Face-Off:</p>
<input type="radio" name="foff" class="carton" filaprc="0" checked="checked">Carton (Gratuit) </br>
<input type="radio" name="foff" class="catifea" filaprc="">Catifea </br>
<span class="pret_fof">Pret: <p id="fof" style="display: inline;">0</p>lei</span></br>
</div>
</form>
<span class="display-none">Pret pe fila: <p id="ppf" style="display: inline;"></p>lei</span></br>
<span class="display-none">Pret coperta: <p id="cop" style="display: inline;"></p>lei</span></br>
<!-- <span>Pret faceoff: <p id="fof" style="display: inline;"></p>lei</span></br> -->
<span class="display-none">Pret grafica: <p id="grf" style="display: inline;"></p>lei</span>
</br>
<p id="result" style="display: inline;">220lei</p>
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$cart = $('input.carton');
$catf = $('input.catifea');
$cart.click(function(){
$(this).addClass('focheck');
$(this).removeClass('founcheck');
$catf.removeClass('focheck');
$catf.addClass('founcheck');
});
$catf.click(function(){
$(this).addClass('focheck');
$(this).removeClass('founcheck');
$cart.removeClass('focheck');
$cart.addClass('founcheck');
});
$('#dims input').click(function(){
if ($(this).attr("checked", "checked")) {
$val_ppf = $(this).attr('filaprc');
$val_cop = $(this).attr('copertaprc');
$val_cart = 0;
$val_catf = $(this).attr('old');
$('#fof_wrp .catifea').attr('filaprc', $val_catf);
$('#ppf').text($val_ppf);
$('#cop').text($val_cop);
$('input[name="foff"]').click(function(){
$(this).prop('checked', true);
if ($('#fof_wrp > input').prop('checked', true)) {
$fof_fin = $(this).attr('filaprc');
$('#fof').text($fof_fin);
}
});
}
});
$('div#col').mousemove(function(){
$col = $(this).children('input').val();
$('#range').text($col);
if ($col > 20) {
$('#grf').text('100');
} else {
$('#grf').text('80')
}
});
$('form').click(function(){
if ($('#fof').text().length > 0) {
$ppfnr = parseInt($('#ppf').text(), 10);
$copnr = parseInt($('#cop').text(), 10);
$fofnr = parseInt($('#fof').text(), 10);
$colnr = parseInt($('#range').text(), 10);
$grfnr = parseInt($('#grf').text(), 10);
$total = $colnr*$ppfnr + $copnr + $fofnr + $grfnr;
$('#result').text('Total: ' + $total + 'lei');
$('span.woocommerce-Price-amount.amount').html($colnr*$ppfnr + $copnr + $fofnr + $grfnr + '<span class="woocommerce-Price-currencySymbol">lei</span>');
$('meta[itemprop="price"]').attr('content',$colnr*$ppfnr + $copnr + $fofnr + $grfnr);
} else {
$('#result').text('Completati toate campurile!');
}
});
});
漂亮的初学者代码,但我做到了,计算器的工作方式应该如此,但是当我添加到购物车时,产品是以正常价格而非计算价格。 这是我的问题,我怎样才能将计算出的价格设定为产品价格?
*我很抱歉我的英语不好!
答案 0 :(得分:0)
我得到了这个代码,但我不知道如何将计算器的结果输出到插入代码的function.php,以替换$ price ...
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0]*2.5);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);