如果产品有重量则打印重量

问题描述 投票:0回答:1

您好,我有这段代码,显示 woocomerce 产品的价格和重量,并计算总重量,我只想在产品有重量的情况下打印重量。

// 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>', __('Total:','woocommerce'),'<span class="price"></span>');
    echo sprintf('<div id="product_total_weight" style="display:none;margin-bottom:20px;">%s %s</div>', __('Área:','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 = 'm²';

    // 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

}

如果产品没有重量我不需要执行,这是为了 woocomerce。

谢谢!

我尝试了多个 if 条件,但都破坏了代码。

php wordpress woocommerce product
1个回答
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>', __('Total:','woocommerce'),'<span class="price"></span>');
    echo sprintf('<div id="product_total_weight" style="display:none;margin-bottom:20px;">%s %s</div>', __('Área:','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 = 'm²';

    // 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) );
                    
                        // Only display the weight if the product has weight
                        if (productData.weight) {
                            var weight_total = parseFloat(productData.weight * $('[name=quantity]').val());
                            $('#product_total_weight .weight').html( weight_total.toFixed(2) + ' ' + productData.weight_unit);
                            $('#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) );
                            
                            // Only update the weight if the product has weight
                            if (productData.weight) {
                                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();
                                
                                // Only update the weight if the product has weight
                                if (productData.variation[variation_id].weight) {
                                    $('#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);
                                } else {
                                    $('#product_total_weight').hide();
                                }
                            } else {
                                $('#product_total_price').hide();
                                $('#product_total_weight').hide();
                            }
                            break;
                    }
                });
            });
        </script>
    <?php

}

没有重量的产品不显示重量。

抱歉谷歌翻译:)

© www.soinside.com 2019 - 2024. All rights reserved.