【问题标题】:Show shipping costs WooCommerce显示运费 WooCommerce
【发布时间】:2019-07-02 07:37:23
【问题描述】:

我正在使用 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>

但此回声与小计的价格相同,因为未添加运费。有人可以帮我加上运费。一些钩子或代码。

【问题讨论】:

    标签: php wordpress woocommerce hook shipping


    【解决方案1】:

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

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

    【讨论】:

    • 你为什么要计算rate_cost_excl_taxrate_cost_incl_tax 如果最后你将使用WC()-&gt;cart-&gt;get_cart_shipping_total()
    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 2019-09-12
    • 2020-05-11
    • 2019-04-21
    • 1970-01-01
    • 2018-08-28
    • 2014-02-12
    • 2014-02-04
    相关资源
    最近更新 更多