需要直接将重量格式更改为1000公斤(1千克,500克更改为500克)

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

我有下面的代码来在店面中显示重量,重量从1000g到1kg,请让我知道如何对“ //在购物车和结帐页面中显示购物车物品的重量”和“保存并显示订购商品的重量(任何地方)“(请最后参考),该重量最初取自Display the weight of cart and order items in WooCommerce回答线程,建议我在此处发布新的问题。

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

我需要下面的代码将重量从1000g以上调整为1kg至500g。

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

提前感谢!!!

php wordpress woocommerce attributes cart
1个回答
0
投票

您可以使用以下注释,并在代码中添加说明并进行解释

// 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 = $values['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 );
© www.soinside.com 2019 - 2024. All rights reserved.