我希望在选择多个数量时显示变量产品的总价格。我找到了this piece of code being suggested in a few different places,它适用于简单的产品,但当用于可变产品时,'产品总数'乘以变量产品的最低价格。
例如。我们有一个可变产品,价格从20美元到100美元
您选择30美元的变体,将数量增加到2,总价格显示40美元。
当添加到购物车时,小计正确($ 60)。
我们如何根据所选变化的价格计算总价格线?
答案 0 :(得分:2)
尝试使用此代码。
<?php
// we are going to hook this on priority 31, so that it would display below add to cart button.
add_action( 'WC_Product_Variable', 'woocommerce_total_product_price', 'set priority here' );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;display:none">%s %s</div>',__('Product Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
echo sprintf('<div id="cart_total_price" style="margin-bottom:20px;display:none">%s %s</div>',__('Cart Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
current_cart_total = <?php echo $woocommerce->cart->cart_contents_total; ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value),
cart_total = parseFloat(product_total + current_cart_total);
$('#product_total_price .price').html( currency + product_total.toFixed(2));
$('#cart_total_price .price').html( currency + cart_total.toFixed(2));
}
$('#product_total_price,#cart_total_price').toggle(!(this.value <= 1));
});
});
</script>
<?php
}
?>