【问题标题】:Get tax rate used in an order for one product and for shipping in WooCommerce获取在 WooCommerce 中用于一种产品的订单和运输的税率
【发布时间】:2018-04-01 20:29:25
【问题描述】:

在woocomerce中,如何获得一个产品订单中使用的税rate?以及运费税率

【问题讨论】:

  • @LoicTheAztec 请注意,这个问题是针对税率的,而不是针对整个税务细节。请不要编辑标题以使您的答案适合(但是,其余信息可能对某些人有用)
  • 对不起,但我没有尝试使我的答案适合。我的实际答案只是在第二个代码 sn-p 中回答您的问题……

标签: php wordpress woocommerce orders tax


【解决方案1】:

您可以通过WC_Order_Item_Product对象和方法获取用于某项(产品)的税务数据:

// Get the an occurrence of the WC_Order object (if needed, from a defined $order ID)
$order = wc_get_order( $order_id );

// Iterating through WC_Order_Item_Product objects
foreach( $order->get_items() as $item_id => $line_item ){
    ## -- Get all protected data in an accessible array -- ##

    $item_data = $line_item->get_data(); // Get the Tax data in an array

    $item_tax_class = $item_data['tax_class'];
    $item_subtotal_tax = $item_data['subtotal_tax'];
    $item_total_tax = $item_data['total_tax'];
    $item_taxes_array = $item_data['taxes'];

    ## -- OR Use WC_Order_Item_Tax methods -- ##

    $item_tax_class = $line_item->get_tax_class(); // Tax class
    $item_subtotal_tax = $line_item->get_subtotal_tax(); // Line item name
    $item_total_tax = $line_item->get_total_tax(); // Tax rate code
    $item_taxes_array = $line_item->get_taxes(); // Tax detailed Array
}

您还可以通过WC_Order_Item_Tax 对象和方法获取税务数据(如运费)

// Get the an occurrence of the WC_Order object (if needed, from a defined $order ID)
$order = wc_get_order( $order_id );

// Iterating through WC_Order_Item_Tax objects
foreach( $order->get_items( 'tax' ) as $item_id => $item_tax ){
    ## -- Get all protected data in an accessible array -- ##

    $tax_data = $item_tax->get_data(); // Get the Tax data in an array

    $item_tax_rate_code = $tax_data['rate_code'];
    $item_tax_rate_id = $tax_data['rate_id'];
    $item_tax_label = $tax_data['label'];
    $item_tax_total = $tax_data['tax_total']; // Tax total amount
    $item_tax_shipping_total = $tax_data['shipping_tax_total']; // Tax shipping total

    ## -- OR Use WC_Order_Item_Tax methods -- ##

    $item_type = $item_tax->get_type(); // Line item type
    $item_name = $item_tax->get_name(); // Line item name
    $rate_code = $item_tax->get_rate_code(); // Tax rate code
    $tax_rate_label = $item_tax->get_label(); // Tax label
    $tax_rate_id = $item_tax->get_rate_id(); // Tax rate ID
    $compound = $item_tax->get_compound(); // Tax compound
    $tax_amount_total = $item_tax->get_tax_total(); // Tax rate total
    $tax_shipping_total = $item_tax->get_shipping_tax_total(); // Tax shipping total
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2018-12-23
    • 2021-06-01
    • 2023-03-20
    • 2019-01-10
    • 2020-09-28
    相关资源
    最近更新 更多