显示运费 WooCommerce

Show shipping costs WooCommerce

我正在使用 WooCommerce 的购物车小部件 -> mini-cart.php 我正在通过 AJAX 向其中添加产品,并自己向其中添加了一些元素。标准它显示购物车中的小计价格,但我想在它之后显示运费,然后显示总价格 (subtotal + shipping)

因此购物车将如下所示:

- Products in cart - Subtotal - Shipping costs - Total price (shipping + subtotal)

我加了总价 code:

<p><?php echo WC()->cart->get_cart_total(); ?></p>

但是这个 echo 的价格与小计的价格相同,因为它没有添加运费。有人可以帮我加上运费吗?一些钩子或代码。

在购物车页面(或结帐页面)中获取并显示所选的送货方式标签(以及其他相关数据,如果需要):

foreach( WC()->session->get('shipping_for_package_0')['rates'] as $method_id => $rate ){
    if( WC()->session->get('chosen_shipping_methods')[0] == $method_id ){
        $rate_label = $rate->label; // The shipping method label name
        $rate_cost_excl_tax = floatval($rate->cost); // The cost excluding tax
        // The taxes cost
        $rate_taxes = 0;
        foreach ($rate->taxes as $rate_tax)
            $rate_taxes += floatval($rate_tax);
        // The cost including tax
        $rate_cost_incl_tax = $rate_cost_excl_tax + $rate_taxes;

        echo '<p class="shipping-total">
            <strong class="label">'.$rate_label.': </strong>
            <span class="totals">'. WC()->cart->get_cart_shipping_total() .'</span>
        </p>';
        break;
    }
}