【问题标题】:Add a title and display item cost and total item cost on WooCommerce checkout review order table在 WooCommerce 结帐审查订单表上添加标题并显示项目成本和总项目成本
【发布时间】:2020-06-21 07:54:19
【问题描述】:

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


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

到目前为止我的代码:

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

谁能告诉我哪里出错了?

【问题讨论】:

    标签: php wordpress woocommerce checkout price


    【解决方案1】:
    • 您可以改用woocommerce_cart_item_subtotal 挂钩。

    • 不需要使用foreach 循环

    • 注意使用 WooCommerce Conditional tags: 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 );
    

    【讨论】:

    • 我对代码进行了一些更改,在标题和总数之间添加了一个新行,包括指示该更改的文本。感谢您一路添加解释。我一直在学习。
    猜你喜欢
    • 2018-08-28
    • 2019-10-12
    • 2019-06-27
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    相关资源
    最近更新 更多