1 回答
TA贡献1784条经验 获得超9个赞
您的代码中有一些错误,请尝试以下将动态显示的内容:
产品总金额乘以产品价格乘以所选数量
购物车小计和产品总金额的总和。
基于你的代码:
add_action( 'woocommerce_after_add_to_cart_form', 'total_product_and_subotal_price' );
function total_product_and_subotal_price() {
global $product;
$product_price = (float) wc_get_price_to_display( $product );
$cart_subtotal = (float) WC()->cart->subtotal + $product_price;
$price_0_html = wc_price( 0 ); // WooCommmerce formatted zero price (formatted model)
$price_html = '<span class="amount">'.number_format($product_price, 2, ',', ' ').'</span>';
$subtotal_html = '<span class="amount">'.number_format($cart_subtotal, 2, ',', ' ').'</span>';
// Display formatted product price total and cart subtotal amounts
printf('<div id="totals-section"><p class="product-total">%s</p><p class="cart-subtotal">%s</p></div>',
str_replace([' amount','0,00'], ['',$price_html], $price_0_html), // formatted html product price
str_replace([' amount','0,00'], ['',$subtotal_html], $price_0_html) // Formatted html cart subtotal
);
?>
<script>
jQuery( function($){
var productPrice = <?php echo $product_price; ?>,
startCartSubtotal = <?php echo $cart_subtotal; ?>;
function formatNumber( floatNumber ) {
return floatNumber.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ').replace('.', ',');
}
$('input[name=quantity]').on( 'input change', function(){
var productQty = $(this).val() == '' ? 1 : $(this).val(),
productTotal = parseFloat(productPrice * productQty),
cartSubtotal = productTotal + startCartSubtotal - productPrice;
cartSubtotal = $(this).val() > 1 ? parseFloat(cartSubtotal) : parseFloat(startCartSubtotal);
$('#totals-section > .product-total .amount').html( formatNumber(productTotal) );
$('#totals-section > .cart-subtotal .amount').html( formatNumber(cartSubtotal) );
});
});
</script>
<?php
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
- 1 回答
- 0 关注
- 175 浏览
添加回答
举报