在 WooCommerce 产品页面中显示数量变化的产品总数和购物车小计

这个想法很简单。在 WooCommerce 产品页面上,随着客户更改数量,显示产品总价(价格上涨或下跌)和购物车小计。


我现在卡住了。即使打开调试,我也没有收到任何错误。产品页面上没有显示任何内容。


add_action( 'woocommerce_after_add_to_cart_form', 'total_product_and_subotal_price' );

function total_product_and_subotal_price() {

    global $product;

    // the div section

    echo sprintf('',__('Product Total:','woocommerce'),''.$product->get_price().'');

    echo sprintf('',__('Cart Total:','woocommerce'),''.$product->get_price().'');

    ?>

        <script>

            jQuery(function($){

                var price = get_price(); ?>,

                    current_cart_total = cart->cart_contents_total; ?>,

                    currency = '';

                $('[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

}


慕娘9325324
浏览 132回答 1
1回答

千万里不及你

您的代码中有一些错误,请尝试以下将动态显示的内容:产品总金额乘以产品价格乘以所选数量购物车小计和产品总金额的总和。基于你的代码:add_action( 'woocommerce_after_add_to_cart_form', 'total_product_and_subotal_price' );function total_product_and_subotal_price() {&nbsp; &nbsp; global $product;&nbsp; &nbsp; $product_price = (float) wc_get_price_to_display( $product );&nbsp; &nbsp; $cart_subtotal = (float) WC()->cart->subtotal + $product_price;&nbsp; &nbsp; $price_0_html&nbsp; = wc_price( 0 ); // WooCommmerce formatted zero price (formatted model)&nbsp; &nbsp; $price_html&nbsp; &nbsp; = '<span class="amount">'.number_format($product_price, 2, ',', ' ').'</span>';&nbsp; &nbsp; $subtotal_html = '<span class="amount">'.number_format($cart_subtotal, 2, ',', ' ').'</span>';&nbsp; &nbsp; // Display formatted product price total and cart subtotal amounts&nbsp; &nbsp; printf('<div id="totals-section"><p class="product-total">%s</p><p class="cart-subtotal">%s</p></div>',&nbsp; &nbsp; &nbsp; &nbsp; str_replace([' amount','0,00'], ['',$price_html], $price_0_html), // formatted html product price&nbsp; &nbsp; &nbsp; &nbsp; str_replace([' amount','0,00'], ['',$subtotal_html], $price_0_html) // Formatted html cart subtotal&nbsp; &nbsp; );&nbsp; &nbsp; ?>&nbsp; &nbsp; <script>&nbsp; &nbsp; jQuery( function($){&nbsp; &nbsp; &nbsp; &nbsp; var productPrice&nbsp; &nbsp; &nbsp; = <?php echo $product_price; ?>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startCartSubtotal = <?php echo $cart_subtotal; ?>;&nbsp; &nbsp; &nbsp; &nbsp; function formatNumber( floatNumber ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return floatNumber.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ').replace('.', ',');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $('input[name=quantity]').on( 'input change', function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var productQty&nbsp; &nbsp;=&nbsp; $(this).val() == '' ? 1 : $(this).val(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productTotal = parseFloat(productPrice * productQty),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cartSubtotal = productTotal + startCartSubtotal - productPrice;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cartSubtotal = $(this).val() > 1 ? parseFloat(cartSubtotal) : parseFloat(startCartSubtotal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#totals-section > .product-total .amount').html( formatNumber(productTotal) );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#totals-section > .cart-subtotal .amount').html( formatNumber(cartSubtotal) );&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP