class-wc-order-item-tax-data-store.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Class WC_Order_Item_Tax_Data_Store file.
  4. *
  5. * @package WooCommerce\DataStores
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * WC Order Item Tax Data Store
  12. *
  13. * @version 3.0.0
  14. */
  15. class WC_Order_Item_Tax_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
  16. /**
  17. * Data stored in meta keys.
  18. *
  19. * @since 3.0.0
  20. * @var array
  21. */
  22. protected $internal_meta_keys = array( 'rate_id', 'label', 'compound', 'tax_amount', 'shipping_tax_amount' );
  23. /**
  24. * Read/populate data properties specific to this order item.
  25. *
  26. * @since 3.0.0
  27. * @param WC_Order_Item_Tax $item Tax order item object.
  28. * @throws Exception If invalid order item.
  29. */
  30. public function read( &$item ) {
  31. parent::read( $item );
  32. $id = $item->get_id();
  33. $item->set_props(
  34. array(
  35. 'rate_id' => get_metadata( 'order_item', $id, 'rate_id', true ),
  36. 'label' => get_metadata( 'order_item', $id, 'label', true ),
  37. 'compound' => get_metadata( 'order_item', $id, 'compound', true ),
  38. 'tax_total' => get_metadata( 'order_item', $id, 'tax_amount', true ),
  39. 'shipping_tax_total' => get_metadata( 'order_item', $id, 'shipping_tax_amount', true ),
  40. )
  41. );
  42. $item->set_object_read( true );
  43. }
  44. /**
  45. * Saves an item's data to the database / item meta.
  46. * Ran after both create and update, so $id will be set.
  47. *
  48. * @since 3.0.0
  49. * @param WC_Order_Item_Tax $item Tax order item object.
  50. */
  51. public function save_item_data( &$item ) {
  52. $id = $item->get_id();
  53. $changes = $item->get_changes();
  54. $meta_key_to_props = array(
  55. 'rate_id' => 'rate_id',
  56. 'label' => 'label',
  57. 'compound' => 'compound',
  58. 'tax_amount' => 'tax_total',
  59. 'shipping_tax_amount' => 'shipping_tax_total',
  60. );
  61. $props_to_update = $this->get_props_to_update( $item, $meta_key_to_props, 'order_item' );
  62. foreach ( $props_to_update as $meta_key => $prop ) {
  63. update_metadata( 'order_item', $id, $meta_key, $item->{"get_$prop"}( 'edit' ) );
  64. }
  65. }
  66. }