Search code examples
phpwordpresswoocommerceorders

WC_Order->get_items() not populating `tax_class` field


In Woocommerce, var_dumping an order shows an empty field tax_class, but tax classes are defined and assigned correctly to the product.

enter image description here

enter image description here

Here is my code:

array(13) {
  ["name"]=>
  string(14) "Nuovo prodotto"
  ["type"]=>
  string(9) "line_item"
  ["item_meta"]=> ...
  ["item_meta_array"]=> ...
  ["qty"]=>
  string(1) "1"
  ["tax_class"]=>
  string(0) "" <-- this shouldn't be empty
  ["product_id"]=>
  string(2) "54"
  ["variation_id"]=>
  string(1) "0"
  ["line_subtotal"]=>
  string(6) "5.8824"
  ["line_total"]=>
  string(6) "5.8824"
  ["line_subtotal_tax"]=>
  string(6) "1.2941"
  ["line_tax"]=>
  string(6) "1.2941"
  ["line_tax_data"]=> ...
}

What I am doing wrong?


Solution

  • The answer is that is completely normal

    Because for a product, when tax class value is set on default the corresponding data stored in the database is 'meta_key' = '_tax_class' => 'meta_value' = ''. So the value is empty.