更改数量时,动态显示变量产品的总价格

时间:2017-06-11 17:07:44

标签: wordpress woocommerce

我需要这个功能,我发现它可以正确使用单个产品,但是,我无法让它在变量产品中运行。

这是

单品的代码

的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>',__('Total Words Number:','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
}

我真的很感谢你的帮助!

3 个答案:

答案 0 :(得分:0)

解决了,也适用于可变产品。

答案 1 :(得分:0)

以上代码不适用于可变产品。仅适用于简单产品

    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>',__('Total Words Number:','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
}

答案 2 :(得分:0)

我不知道它是如何工作的,但它有效

// shows the total price and total weight based on the quantity entered
add_action( 'woocommerce_single_product_summary', 'show_total_price_and_total_weight_by_quantity', 31 );
function show_total_price_and_total_weight_by_quantity() {
    // create the divs that will contain the total price and total weight
    echo sprintf('<div id="product_total_price" style="display:none;margin-bottom:20px;">%s %s</div>', __('Totale Prodotto:','woocommerce'),'<span class="price"></span>');
    echo sprintf('<div id="product_total_weight" style="display:none;margin-bottom:20px;">%s %s</div>', __('Totale Peso:','woocommerce'),'<span class="weight"></span>');
}

// change the total price and total weight based on the quantity entered
add_action( 'wp_footer', 'change_total_price_and_total_weight_by_quantity', 99 );
function change_total_price_and_total_weight_by_quantity() {
    // only on the product page
    if ( ! is_product() ) {
        return;
    }

    global $product;
    
    // initializes the array that will contain the product data
    $product_data = array();

    // do divs need to be shown?
    $show = true;

    // gets the currency and unit of weight
    $currency = get_woocommerce_currency_symbol();
    $weight_unit = get_option('woocommerce_weight_unit');

    // if the product is simple
    if ( $product->is_type( 'simple' ) ) {
        // set the type of product
        $product_data['type']  = 'simple';
        // get simple product data
        $product_data['price'] = $product->get_price();
        $product_data['currency'] = $currency;
        $product_data['weight'] = $product->get_weight();
        $product_data['weight_unit'] = $weight_unit;
    }

    // if the product is variable
    if ( $product->is_type( 'variable' ) ) {
        // set the type of product
        $product_data['type']  = 'variable';
        // get the ids of the product variations
        $variation_ids = $product->get_children();
        foreach ( $variation_ids as $variation_id ) {
            // gets the object of the variation product
            $variation = wc_get_product( $variation_id );
            // gets product variation data
            $product_data['variation'][$variation_id]['price'] = $variation->get_price();
            $product_data['variation'][$variation_id]['currency'] = $currency;
            $product_data['variation'][$variation_id]['weight'] = $variation->get_weight();
            $product_data['variation'][$variation_id]['weight_unit'] = $weight_unit;
        }
        // hides the div
        $show = false;
    }

    // returns the JSON representation of a data
    $product_data_json = json_encode( $product_data );

    ?>
        <script>
            jQuery(function($) {
                // create a javascript object with product data
                <?php
                echo "var productData = ". $product_data_json . ";";
                if ( $show ) {
                    ?>
                    var product_total = parseFloat( productData.price * $('[name=quantity]').val());
                    $('#product_total_price .price').html( productData.currency + product_total.toFixed(2) );
                    var weight_total = parseFloat(productData.weight * $('[name=quantity]').val());
                    $('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + productData.weight_unit);
                    $('#product_total_price').show();
                    $('#product_total_weight').show();
                    <?php
                }
                ?>
                // when the quantity is changed or a product option is selected
                jQuery('[name=quantity], table.variations select').on('change',function(){
                    // shows data based on product type
                    switch( productData.type ) {
                        case 'simple':
                            // update the fields
                            var product_total = parseFloat(productData.price * $(this).val());
                            $('#product_total_price .price').html( productData.currency + product_total.toFixed(2) );
                            var weight_total = parseFloat(productData.weight * $(this).val());
                            $('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + productData.weight_unit);                      
                            break;
                        case 'variable':
                            // gets the id variation based on the options chosen
                            var variation_id = $('input.variation_id').val();
                            // if the variation id is valid and the current option is different from "Choose an option"
                            if ( parseInt( $('input.variation_id').val() ) > 0 && $('input.variation_id').val() != '' && $(this).find('option').filter(':selected').val() != '' ) {
                                $('#product_total_price').show();
                                $('#product_total_weight').show();
                                // gets the object based on the selected variation
                                var obj = productData.variation[variation_id];
                                // update the fields
                                var product_total = parseFloat(obj.price * $(this).val());
                                $('#product_total_price .price').html( obj.currency + ' ' + product_total.toFixed(2) );
                                var weight_total = parseFloat(obj.weight * $(this).val());
                                $('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + obj.weight_unit);
                            // otherwise it hides the divs
                            } else {
                                $('#product_total_price').hide();
                                $('#product_total_weight').hide();
                            }
                            break;
                    }
                });
            });
        </script>
    <?php

}```