[英]Calculation issue on woocommerce order-details totals
我正在為客戶開發一個Wordpress + Woocommerce網站。 他需要客戶提前支付購物車30%的費用,然后他們才開始准備訂單。
由於Woocommerce沒有內置此預付款功能,因此我決定使用優惠券,自動對訂單總額應用70%的折扣(然后我將隱藏優惠券信息)。
對於1000歐元的購物車,客戶將看到: Total = 300€
。
然后,我添加一條“左付”行,並在函數文件中顯示進行簡單計算的結果: $woocommerce->cart->subtotal - $woocommerce->cart->cart_contents_total;
結果是折扣的金額:在這種情況下為700€。
問題:這在購物車和結帳頁面上運行正常,但是在“詳細信息”頁面上,“需支付的金額”為0。
這是在訂單明細模板上顯示總計的代碼(在woocommerce \\ 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版本將支持部分付款,但是在那之前,您還可以查看WooCommerce存款插件
PS- global $woocommerce
已被棄用,因此您應該習慣使用WC()
代替它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.