class-wc-order-item-shipping-data-store.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * WC Order Item Shipping Data Store
  4. *
  5. * @version 3.0.0
  6. * @package data-stores
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. /**
  12. * WC_Order_Item_Shipping_Data_Store class.
  13. */
  14. class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
  15. /**
  16. * Data stored in meta keys.
  17. *
  18. * @since 3.0.0
  19. * @var array
  20. */
  21. protected $internal_meta_keys = array( 'method_id', 'instance_id', 'cost', 'total_tax', 'taxes' );
  22. /**
  23. * Read/populate data properties specific to this order item.
  24. *
  25. * @since 3.0.0
  26. * @param WC_Order_Item_Shipping $item Item to read to.
  27. * @throws Exception If invalid shipping order item.
  28. */
  29. public function read( &$item ) {
  30. parent::read( $item );
  31. $id = $item->get_id();
  32. $item->set_props(
  33. array(
  34. 'method_id' => get_metadata( 'order_item', $id, 'method_id', true ),
  35. 'instance_id' => get_metadata( 'order_item', $id, 'instance_id', true ),
  36. 'total' => get_metadata( 'order_item', $id, 'cost', true ),
  37. 'taxes' => get_metadata( 'order_item', $id, 'taxes', true ),
  38. )
  39. );
  40. // BW compat.
  41. if ( '' === $item->get_instance_id() && strstr( $item->get_method_id(), ':' ) ) {
  42. $legacy_method_id = explode( ':', $item->get_method_id() );
  43. $item->set_method_id( $legacy_method_id[0] );
  44. $item->set_instance_id( $legacy_method_id[1] );
  45. }
  46. $item->set_object_read( true );
  47. }
  48. /**
  49. * Saves an item's data to the database / item meta.
  50. * Ran after both create and update, so $id will be set.
  51. *
  52. * @since 3.0.0
  53. * @param WC_Order_Item_Shipping $item Item to save.
  54. */
  55. public function save_item_data( &$item ) {
  56. $id = $item->get_id();
  57. $changes = $item->get_changes();
  58. $meta_key_to_props = array(
  59. 'method_id' => 'method_id',
  60. 'instance_id' => 'instance_id',
  61. 'cost' => 'total',
  62. 'total_tax' => 'total_tax',
  63. 'taxes' => 'taxes',
  64. );
  65. $props_to_update = $this->get_props_to_update( $item, $meta_key_to_props, 'order_item' );
  66. foreach ( $props_to_update as $meta_key => $prop ) {
  67. update_metadata( 'order_item', $id, $meta_key, $item->{"get_$prop"}( 'edit' ) );
  68. }
  69. }
  70. }