gpt4 book ai didi

php - WooCommerce:仅针对特定税种的商品获取折扣总额

转载 作者:行者123 更新时间:2023-12-04 08:01:26 26 4
gpt4 key购买 nike

我只想从税率降低的项目中导出优惠券总计(您可以在下图中看到的总计)。
对于这些项目的总数,我也这样做。 A great answer helped me仅导出税率降低的订单项目的总数。为此,我使用以下代码:

// gets the total of the order items by tax class
function get_total_order_items_by_tax_class( $order_id, $tax_class = 'reduced-rate' ) {
$order = wc_get_order( $order_id );
// initializes the total of the order items
$total = 0;
foreach( $order->get_items() as $item_id => $order_item ) {
// if the product tax class is equal to "$tax_class"
if ( $tax_class == $order_item['tax_class'] ) {
// sum the total
$total += $order_item['total'];
}
}
return $total;
}
我尝试了类似的东西并添加了以下行( found here ):
$order->get_discount_total();
到片段。但这会导出每个税种的每个项目的全部折扣。
我还尝试了来自 this answer 的以下代码:
foreach( $order->get_coupon_codes() as $coupon_code ) {
// Get the WC_Coupon object
$coupon = new WC_Coupon($coupon_code);

$discount_type = $coupon->get_discount_type(); // Get coupon discount type
$coupon_amount = $coupon->get_amount(); // Get coupon amount
}
但这也是整个订单的折扣。
有没有办法只获得税率降低的项目的折扣总额?
我相信一定有办法,因为订单会在每个项目下方显示这些总计。
但是我找不到获得这些折扣的方法。
我看到了 $order仅包含优惠券总额和优惠券税。不是每个订单项。而且似乎 $order_item不包含任何折扣。只有东西行 WC_Coupon_Data_Store_CPT .
enter image description here

最佳答案

尝试以下操作以按税种获取订单商品折扣金额:

// Get order items discount amount excl. taxes by tax class
function get_order_items_discount_total_by_tax_class( $order_id, $tax_class = 'reduced-rate' ) {
$order = wc_get_order( $order_id );
$discount = 0; // Initializing;

foreach( $order->get_items() as $item ) {
// if the product tax class is equal to "$tax_class"
if ( $tax_class == $item['tax_class'] ) {
$discount += $item->get_subtotal() - $item->get_total(); // Excluding taxes
}
}
return $discount;
}
它应该工作。
相关: Get Order items and WC_Order_Item_Product in WooCommerce 3

关于php - WooCommerce:仅针对特定税种的商品获取折扣总额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66447543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com