在 WooCommerce 结帐审核订单上添加标题并显示商品成本和总商品成本 table

Add a title and display item cost and total item cost on WooCommerce checkout review order table

我知道我的问题的标题可能听起来令人困惑,因此,我请您查看所附图片以进行澄清。


我已经尝试自己完成这项工作但没有成功。测试我的代码不会改变结帐页面上的任何内容。没有变化,没有错误,没有通知 - 什么都没有。

到目前为止我的代码:

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

谁能告诉我哪里出了问题?

  • 您可以改用 woocommerce_cart_item_subtotal 挂钩。

  • 没有必要使用 foreach 循环

  • 注意使用 WooCommerce Conditional tagsis_checkout() - Returns 在结帐页面上是正确的

  • 可选:$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 );