关于woocommerce订单详细信息总计的计算问题


Calculation issue on woocommerce order-details totals

我正在为一个客户开发一个Wordpress+Wooccommerce网站。他需要他的客户提前支付购物车30%的费用,然后他们开始准备订单。

由于Wooccommerce没有内置这种预付款功能,我决定使用优惠券,自动在订单总额上申请70%的折扣(然后我会隐藏优惠券信息)。

对于1000欧元的购物车,客户将看到:Total = 300€
然后我添加一行"Left to pay",并在函数文件中显示进行简单计算的结果:$woocommerce->cart->subtotal - $woocommerce->cart->cart_contents_total;
结果是折扣金额:在这种情况下为700欧元。

问题:这在购物车和结账页面上非常有效,但在订单详细信息页面上,"剩余付款"金额为0。

这是在订单详细信息模板上显示总数的代码(在wooccommerce''order中)。

<!-- show the totals on Order Details footer -->
    <?php
        if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) :
            ?>
            <tr>
                <th scope="row"><?php echo $total['label']; ?></th>
                <td><?php echo $total['value']; ?></td>
            </tr>
            <?php
        endforeach;
    ?>
<!-- Trying to add the Left to pay line here -->
    <?php   if ( $totals = $order->get_order_item_totals() ) { ?>
        <tr class="order-total">
            <th>Left to pay</th>
            <td><?php echo number_format(custom_Total_Solde(),2,'.','')."€"; ?></td>
        </tr>
    <?php }; ?>

这是我在函数中使用的代码,用来计算"剩余支付"的金额。

function custom_Total_Solde() {
    global $woocommerce;
    $solde = $woocommerce->cart->subtotal - $woocommerce->cart->cart_contents_total;
    return $solde;
}

有人知道订单明细是如何计算总额的吗?它与购物车或结账时不同吗?也许你知道订单详细信息总标签存储在哪个模板中?如果我能找到它,也许我能理解为什么我的计算在这个特定的部分不起作用。。。

签出后,购物车信息将被清除,因此无法进行计算。

您需要查看includes/class-wc-order.php中的WC_Order类。

$order->get_total()应告知您已支付的总金额。因此,$order->get_total_discount()$order->get_cart_discount()可能是需要支付的金额?

或者,当订单在结账时使用以下挂钩创建时,您可以向订单添加一些自定义元:

do_action( 'woocommerce_checkout_update_order_meta', $order_id, $this->posted );

我听说未来版本的WooCommerce将支持部分支付,但在此之前,你也可以查看WooCommCommerce存款插件

PS-global $woocommerce是软弃用的,所以您应该习惯于使用WC()代替它。