@shpilevich

Как вывести вес заказ?

Всем привет!
подскажите как вывести вес заказ в заказы (в управлении woocommerce), а так же в накладную. В корзине и в эл.письме вывел через добавление в function:
add_action('woocommerce_before_checkout_form', 'bbloomer_print_cart_weight');
add_action('woocommerce_before_cart', 'bbloomer_print_cart_weight');
function bbloomer_print_cart_weight($posted) {
global $woocommerce;
$notice = 'Вес заказа: ' . $woocommerce->cart->cart_contents_weight . get_option('woocommerce_weight_unit');
if (is_cart()) {
wc_print_notice($notice, 'notice');
} else {
wc_add_notice($notice, 'notice');
}
}

add_action('woocommerce_email_after_order_table','show_total_weight', 10, 4);
function show_total_weight( $order, $sent_to_admin, $plain_text, $email ){

if ( 'new_order' != $email->id ) return;

$total_weight = 0;

foreach( $order->get_items() as $item_id => $product_item ){
$quantity = $product_item->get_quantity(); // get quantity
$product = $product_item->get_product(); // get the WC_Product object
$product_weight = $product->get_weight(); // get the product weight
// Add the line item weight to the total weight calculation
$total_weight += floatval( $product_weight * $quantity );
}

// Styles
$style1 = 'style="width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; color: #737373; border: 1px solid #e4e4e4; border-top:0;"';
$style2 = ' style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
$style3 = ' style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';

// Output
echo "
" . __( 'Вес заказа: ', 'woocommerce' ) . "" . $total_weight . " kg
";
}
  • Вопрос задан
  • 235 просмотров
Пригласить эксперта
Ответы на вопрос 1
@DimDim7778
Вот четыре кода ниже для вывода веса везде где необходимо
/*вес заказа на почту уведомление*/
add_action('woocommerce_email_after_order_table','show_total_weight', 10, 4);
function show_total_weight( $order, $sent_to_admin, $plain_text, $email ){

    if ( 'new_order' != $email->id ) return;

    $total_weight = 0;

    foreach( $order->get_items() as $item_id => $product_item ){
        $quantity = $product_item->get_quantity(); // get quantity
        $product = $product_item->get_product(); // get the WC_Product object
        $product_weight = $product->get_weight(); // get the product weight
        // Add the line item weight to the total weight calculation
        $total_weight += floatval( $product_weight * $quantity );
    }

    // Styles
    $style1 = 'style="width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; color: #737373; border: 1px solid #e4e4e4; border-top:0;"';
    $style2 = '  style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';
    $style3 = ' style="text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;border-top:0;"';

    // Output
    echo "<table class='td' cellspacing='0' cellpadding='6' $style1><tr><th $style2>" . __( 'Вес заказа: ', 'woocommerce' ) . "</th><td $style3>" . $total_weight . " kg</td></tr></table>";
}

// 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( __( 'Вес', 'woocommerce' ), ( $values['quantity'] * $values['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

/**/
add_action ('woocommerce_single_product_summary', 'show_weight', 20);
function show_weight() {
global $product;
$weight_unit = get_option('woocommerce_weight_unit');
$attributes = $product->get_attributes();
if ( $product->has_weight() ) {
print '<p>Вес: '.$product->get_weight(). $weight_unit . '</p>'.PHP_EOL;
}
}

/** Show product weight on archive pages **/
 
add_action( 'woocommerce_after_shop_loop_item', 'rs_show_weights', 9 );

function rs_show_weights() {

    global $product;
    $weight = $product->get_weight();

    if ( $product->has_weight() ) {
        echo '<div class="product-meta"><span class="product-meta-label">вес: </span>' . $weight . get_option('woocommerce_weight_unit') . '</div></br>';
    }
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы