我有一个WooCommerce商店,如果数量发生变化,我需要更新产品价格。例如,如果您想以50美元的价格购买3个马克杯,那么单个产品页面上的价格会自动变为150美元,当您增加数量时,每次显示的价格将增加50美元。
我的商店大部分都是可变产品,所以价格必须更新变量产品以及简单产品。
以下代码几乎就在那里,但仍然不对。它在可变产品上不能很好地工作,并且不会更新主要产品的正常价格。
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
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
}
任何帮助将不胜感激!
答案 0 :(得分:-2)
WooCommerce Single Page Product already does that out of the box and you dont have to do anything else to make it work, now you are hooking into woocommerce_single_product_summary but you are not doing any extra functioanlity there so there is no need for it, you add your extras in the templates of woocommerce itself and leave the base foundation functionality to WooCommerce to handle.