Добавьте заголовок и отобразите стоимость товара и общую стоимость товара в таблице заказов на просмотр оформления заказа WooCommerce. - PullRequest
1 голос
/ 21 июня 2020

Я понимаю, что название моего вопроса может показаться непонятным, и поэтому прошу вас посмотреть на прилагаемое изображение для пояснения.

enter image description here


I've tried myself to make this work without success. Testing my code changes nothing on the checkout page. No change, no error, no notices - nothing.

My code so far:

add_filter( 'woocommerce_cart_item_price', 'item_price_and_total_price', 10, 3 );
function item_price_and_total_price( $price, $cart_item, $cart_item_key ) {

    foreach( WC()->cart->get_cart() as $cart_item ){    
        $item = $cart_item['data']->get_id();
    }

    // item price title
    $price_title = 'Item price';

    // item price (this should not change as the qty changes)
    $price_per_item = wc_price( $item ) . ' per item';

    // total item price (cost) as the qty changes
    $total_item_price = // don't know how to get it

    // return everything
    $price = $price_title . '<br />' . $price_per_item . '<br />' . $total_item_price;

    return $price;
}

Кто-нибудь, кто может сказать мне, где что-то пошло не так?

1 Ответ

0 голосов
/ 21 июня 2020
  • Вместо этого вы можете использовать хук woocommerce_cart_item_subtotal.

  • Использование foreach l oop не обязательно

  • Обратите внимание на использование WooCommerce Условные теги : is_checkout() - Возвращает true на странице оформления заказа

  • Необязательно: $cart_item['quantity']; может быть используется при необходимости для обеспечения дополнительных условий исходя из количества товара

function filter_woocommerce_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
    // Returns true on the checkout page
    if ( is_checkout() ) {  
         // Item price title
        $price_title = 'Item price';
        
        // Item price
        $item_price = $cart_item['data']->get_price();
        
        // Line subtotal
        $line_subtotal = $cart_item['line_subtotal'];
        
        // Item title + item price
        $subtotal = '<span>' . $price_title . '</span>' . wc_price( $item_price );
        
        // Append to
        $subtotal .= wc_price( $line_subtotal );
    }

    return $subtotal;
}
add_filter( 'woocommerce_cart_item_subtotal', 'filter_woocommerce_cart_item_subtotal', 10, 3 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...