Как вывести вес товара в кг, если больше 1000 грамм - PullRequest
1 голос
/ 05 мая 2020

В теме Storefront я использую приведенный ниже код, который меняет форматированный вес с 1000 г на 1 кг:

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

Как отобразить тот же формат в

  • Страницы корзины и оформления заказа
  • Заказы и электронная почта

Код ниже взят из " Отображение веса корзины и элементов заказа в WooCommerce" ветка ответов, и мне посоветовали опубликовать здесь как новый вопрос.

Мне нужно, чтобы код ниже был сформирован из веса

  • выше От 1000 г до 1 кг
  • и 500 г. как есть
// 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 Ответ

2 голосов
/ 05 мая 2020

Вы можете использовать следующий комментарий с пояснением, добавленным в код

// 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 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...