如果超过1000克,如何显示以公斤为单位的产品重量 [英] How to display the product weight in kg, if more than 1000 grams

查看:71
本文介绍了如果超过1000克,如何显示以公斤为单位的产品重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Storefront主题中,我使用下面的代码将格式化后的重量从1000g更改为1kg:

  add_action('woocommerce_after_shop_loop_item_title','show_weight',10);函数show_weight(){全球产品$ attributes = $ product-> get_attributes();如果($ product-> has_weight()){$ weight = $ product-> get_weight();$ weight_unit ='克';如果($ weight> = 1000){$ weight = round((($ weight/1000),2);$ weight_unit ='kg';}打印'< p>重量:'.$ weight.''.$ weight_unit.'</p>';}} 


如何以相同的格式显示

  • 购物车和结帐页面
  • 订单和电子邮件

以下代码最初来自"


因此,您可以使用以下方法在购物车和结帐页面中显示购物车物品的重量.

注释并在代码中添加了解释

 //在购物车和结帐页面中显示购物车物品的重量函数display_custom_item_data($ cart_item_data,$ cart_item){//变胖$ weight = $ cart_item ['data']-> get_weight();如果($ weight> 0){//设置单位$ weight_unit ='克';//计算$ total_weight = $ cart_item ['quantity'] * $ weight;如果($ total_weight> = 1000){$ total_weight = round((($ total_weight/1000),2);$ weight_unit ='kg';//更改单位}$ cart_item_data [] = array('名称'=>__('小计重量','woocommerce'),'值'=>$ total_weight.''.$ weight_unit,);}返回$ cart_item_data;}add_filter('woocommerce_get_item_data','display_custom_item_data',10,2);//保存并显示订单商品的重量(无处不在)函数display_order_item_data($ item,$ cart_item_key,$ values,$ order){//变胖$ weight = $ values ['data']-> get_weight();如果($ weight> 0){//设置单位$ weight_unit ='克';//计算$ total_weight = $ item ['quantity'] * $ weight;如果($ total_weight> = 1000){$ total_weight = round((($ total_weight/1000),2);$ weight_unit ='kg';//更改单位}$ 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); 

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') );
    }
}

解决方案

The answer to this question assumes that the weight unit is set to 'gram' via the WooCommerce settings


So than you can use the following to display the cart item weight in cart and checkout pages.

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 );

这篇关于如果超过1000克,如何显示以公斤为单位的产品重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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