1 回答

TA貢獻1898條經驗 獲得超8個贊
由于這與運輸包裹有關(因為購物車商品可以拆分(劃分)為多個運輸包裹),因此您需要使用方法$packages中包含的變量參數calculate_shipping()。
WC_Cart因此,如果不使用對象方法,您的代碼將會略有不同:
public function calculate_shipping( $packages = array() ) {
$total = $total_tax = 0; // Initializing
// Loop through shipping packages
foreach( $packages as $key => $package ){
// Loop through cart items for this package
foreach( $package['contents'] as $item ){
$total += $item['total']; // Item subtotal discounted
$total_tax += $item['total_tax']; // Item subtotal tax discounted
}
}
add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => $total * $this->percentage_rate / 100,
// 'calc_tax' => 'per_item'
) );
}
代碼位于活動子主題(活動主題)的functions.php 文件中。經過測試并有效。
注:此處計算的是折扣后的購物車商品小計(不含稅)。您可以輕松添加添加使其在含稅折扣后的購物車商品小計中,替換:
'cost' => $total * $this->percentage_rate / 100,
經過:
'cost' => ($total + $total_tax) * $this->percentage_rate / 100,
您可以查看如何制作運輸包裹:
WC_Cart
get_shipping_packages()
方法源代碼
如果您還想處理傳送類等,請檢查:
WC_Shipping_Flat_Rate
calculate_shipping()
方法源代碼。
- 1 回答
- 0 關注
- 105 瀏覽
添加回答
舉報