Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce:仅获取特定税种商品的折扣总额_Php_Wordpress_Woocommerce_Orders_Tax - Fatal编程技术网

Php WooCommerce:仅获取特定税种商品的折扣总额

Php WooCommerce:仅获取特定税种商品的折扣总额,php,wordpress,woocommerce,orders,tax,Php,Wordpress,Woocommerce,Orders,Tax,我只想导出税率降低的项目的优惠券总额(您可以在下图中看到的总额) 对于这些项目的总数,我也会这样做。仅导出税率降低的订单项目总数。为此,我使用以下代码: // 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_i

我只想导出税率降低的项目的优惠券总额(您可以在下图中看到的总额)

对于这些项目的总数,我也会这样做。仅导出税率降低的订单项目总数。为此,我使用以下代码:

// 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;
}
我尝试了类似的方法并添加了一行():

到代码片段。但这会导出每个税种的每个项目的全部折扣

我还尝试了以下代码:

但这也是整个订单的折扣

有没有办法只获得税率降低项目的折扣总额? 我相信一定有办法,因为订单上每一项下都显示了这些总数。 但是我找不到办法得到这些折扣

我看到,
$order
只包含息票总额和息票税。不是每行项目。而且,
$order\u项目
似乎不包含任何折扣。只有行
WC\u优惠券\u数据\u存储\u CPT


请尝试以下操作以按税务类别获取订单项目折扣金额:

// 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;
}
它应该会起作用


相关:

太好了,就这样!
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
}
// 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;
}