当数量选择更改时,WooCommerce显示总价 [英] WooCommerce display total price when quantity selection change

查看:148
本文介绍了当数量选择更改时,WooCommerce显示总价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数量更改时,我需要在产品页面上显示总价.
如果您在购物车中添加产品数量,则此价格与购物车中的行价相同.
我对WooCommerce还是陌生的,所以我不确定从哪里开始.但是,如果有人可以帮助我朝正确的方向发展,我想我可以自己管理.

I'm in need of displaying the total price in product page when quantity changes.
This is the same as line price in the cart if you add quantity of products in the cart.
I'm still new with WooCommerce so I'm not sure where to start. But if someone could help me to the right direction, I think I can manage on my own.

所以这是我应该如何做的想法.

So here's my thoughts on how should I do it.

我在想我的jquery会是这样.

I'm thinking my jquery would be like this.

jQuery(document).ready(function($){
    $('.qty').on('change',function(){
         // grab the price
         var price = $('[itemprop="price"]').attr('content');
         var total_price = price * this.value;
         console.log(price, this.value, total_price);
    });
})

这在粘贴到控制台上时有效.但是我不确定将此代码放在WooCommerce上的什么位置.

this works when pasted on the console. But I'm not sure where to put this code on WooCommerce.

推荐答案

您快到了……尝试一下,将其粘贴到您的functions.php中

You're almost there... try this, paste this in your functions.php

add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
    global $woocommerce, $product;
    // let's setup our divs
    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Product Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
    ?>
        <script>
            jQuery(function($){
                var price = <?php echo $product->get_price(); ?>,
                    currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

                $('[name=quantity]').change(function(){
                    if (!(this.value < 1)) {

                        var product_total = parseFloat(price * this.value);

                        $('#product_total_price .price').html( currency + product_total.toFixed(2));

                    }
                });
            });
        </script>
    <?php
}

来源: http://reigelgallarde.me/programming/show-product-price-times-selected-quantity-on-woocommecre-product-page/

这篇关于当数量选择更改时,WooCommerce显示总价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆