How to display the product weight in kg, if more than 1000 grams

旧巷老猫 提交于 2020-05-17 07:41:28

问题


In Storefront theme, I use the code below that change the formatted weight from 1000g to 1kg:

add_action('woocommerce_after_shop_loop_item_title', 'show_weight', 10);
function show_weight()
{
    global $product;
    $attributes = $product->get_attributes();
    if ($product->has_weight()) {
        $weight = $product->get_weight();
        $weight_unit = 'grams';
        if ($weight >= 1000) {
            $weight = round(($weight / 1000), 2);
            $weight_unit = 'kg';
        }
        print '<p>Weight: ' . $weight . ' ' . $weight_unit . '</p>';
    }
}

How to display the same format in

  • Cart and checkout pages
  • Orders & e-mails

Below code is taken originally from "Display the weight of cart and order items in WooCommerce" answer thread and I have been advised to post here as a new question.

I need below code to be formated from weight

  • above 1000g to 1kg
  • and 500g as it is.
// Display the cart item weight in cart and checkout pages

add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
    if ( $cart_item['data']->get_weight() > 0 ){
        $cart_item_data[] = array(
            'name' => __( 'Weight subtotal', 'woocommerce' ),
            'value' =>  ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit')
        );
    }
    return $cart_item_data;
}

// Save and Display the order item weight (everywhere)

add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->get_weight() > 0 ){
        $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

回答1:


You can use the following, comment with explanation added in the code

// Display the cart item weight in cart and checkout pages
function display_custom_item_data( $cart_item_data, $cart_item ) {
    // Get weight
    $weight = $cart_item['data']->get_weight();

    if ( $weight > 0 ) {
        // Set unit
        $weight_unit = 'grams';

        // Calculate
        $total_weight = $cart_item['quantity'] * $weight;

        if ( $total_weight >= 1000 ) {
            $total_weight = round(($total_weight / 1000), 2);
            $weight_unit = 'kg'; // Change unit
        }

        $cart_item_data[] = array(
            'name' => __( 'Weight subtotal', 'woocommerce' ),
            'value' =>  $total_weight . ' ' . $weight_unit,
        );
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );

// Save and Display the order item weight (everywhere)
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    // Get weight
    $weight = $values['data']->get_weight();

    if ( $weight > 0 ) {
        // Set unit
        $weight_unit = 'grams';

        // Calculate
        $total_weight = $item['quantity'] * $weight;

        if ( $total_weight >= 1000 ) {
            $total_weight = round(($total_weight / 1000), 2);
            $weight_unit = 'kg'; // Change unit
        }       

        $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $total_weight )  . ' ' . $weight_unit );
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );


来源:https://stackoverflow.com/questions/61611278/how-to-display-the-product-weight-in-kg-if-more-than-1000-grams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!