Abstract_WC_Order_Item_Type_Data_Store

WC Order Item Data Store.

Defined (1)

The class is defined in the following location(s).

/includes/data-stores/abstract-wc-order-item-type-data-store.php  
  1. abstract class Abstract_WC_Order_Item_Type_Data_Store extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface { 
  2.  
  3. /** 
  4. * Meta type. This should match up with 
  5. * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. 
  6. * WP defines 'post', 'user', 'comment', and 'term'. 
  7. */ 
  8. protected $meta_type = 'order_item'; 
  9.  
  10. /** 
  11. * This only needs set if you are using a custom metadata type (for example payment tokens. 
  12. * This should be the name of the field your table uses for associating meta with objects. 
  13. * For example, in payment_tokenmeta, this would be payment_token_id. 
  14. * @var string 
  15. */ 
  16. protected $object_id_field_for_meta = 'order_item_id'; 
  17.  
  18. /** 
  19. * Create a new order item in the database. 
  20. * @since 3.0.0 
  21. * @param WC_Order_Item $item 
  22. */ 
  23. public function create( &$item ) { 
  24. global $wpdb; 
  25.  
  26. $wpdb->insert( $wpdb->prefix . 'woocommerce_order_items', array( 
  27. 'order_item_name' => $item->get_name(),  
  28. 'order_item_type' => $item->get_type(),  
  29. 'order_id' => $item->get_order_id(),  
  30. ) ); 
  31. $item->set_id( $wpdb->insert_id ); 
  32. $this->save_item_data( $item ); 
  33. $item->save_meta_data(); 
  34. $item->apply_changes(); 
  35. $this->clear_cache( $item ); 
  36.  
  37. do_action( 'woocommerce_new_order_item', $item->get_id(), $item, $item->get_order_id() ); 
  38.  
  39. /** 
  40. * Update a order item in the database. 
  41. * @since 3.0.0 
  42. * @param WC_Order_Item $item 
  43. */ 
  44. public function update( &$item ) { 
  45. global $wpdb; 
  46.  
  47. $changes = $item->get_changes(); 
  48.  
  49. if ( array_intersect( array( 'name', 'order_id' ), array_keys( $changes ) ) ) { 
  50. $wpdb->update( $wpdb->prefix . 'woocommerce_order_items', array( 
  51. 'order_item_name' => $item->get_name(),  
  52. 'order_item_type' => $item->get_type(),  
  53. 'order_id' => $item->get_order_id(),  
  54. ), array( 'order_item_id' => $item->get_id() ) ); 
  55.  
  56. $this->save_item_data( $item ); 
  57. $item->save_meta_data(); 
  58. $item->apply_changes(); 
  59. $this->clear_cache( $item ); 
  60.  
  61. do_action( 'woocommerce_update_order_item', $item->get_id(), $item, $item->get_order_id() ); 
  62.  
  63. /** 
  64. * Remove an order item from the database. 
  65. * @since 3.0.0 
  66. * @param WC_Order_Item $item 
  67. * @param array $args Array of args to pass to the delete method. 
  68. */ 
  69. public function delete( &$item, $args = array() ) { 
  70. if ( $item->get_id() ) { 
  71. global $wpdb; 
  72. do_action( 'woocommerce_before_delete_order_item', $item->get_id() ); 
  73. $wpdb->delete( $wpdb->prefix . 'woocommerce_order_items', array( 'order_item_id' => $item->get_id() ) ); 
  74. $wpdb->delete( $wpdb->prefix . 'woocommerce_order_itemmeta', array( 'order_item_id' => $item->get_id() ) ); 
  75. do_action( 'woocommerce_delete_order_item', $item->get_id() ); 
  76.  
  77. /** 
  78. * Read a order item from the database. 
  79. * @since 3.0.0 
  80. * @param WC_Order_Item $item 
  81. */ 
  82. public function read( &$item ) { 
  83. global $wpdb; 
  84.  
  85. $item->set_defaults(); 
  86.  
  87. // Get from cache if available. 
  88. $data = wp_cache_get( 'item-' . $item->get_id(), 'order-items' ); 
  89.  
  90. if ( false === $data ) { 
  91. $data = $wpdb->get_row( $wpdb->prepare( "SELECT order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) ); 
  92. wp_cache_set( 'item-' . $item->get_id(), $data, 'order-items' ); 
  93.  
  94. if ( ! $data ) { 
  95. throw new Exception( __( 'Invalid order item.', 'woocommerce' ) ); 
  96.  
  97. $item->set_props( array( 
  98. 'order_id' => $data->order_id,  
  99. 'name' => $data->order_item_name,  
  100. ) ); 
  101. $item->read_meta_data(); 
  102.  
  103. /** 
  104. * Saves an item's data to the database / item meta. 
  105. * Ran after both create and update, so $item->get_id() will be set. 
  106. * @since 3.0.0 
  107. * @param WC_Order_Item $item 
  108. */ 
  109. public function save_item_data( &$item ) {} 
  110.  
  111. /** 
  112. * Clear meta cachce. 
  113. */ 
  114. public function clear_cache( &$item ) { 
  115. wp_cache_delete( 'item-' . $item->get_id(), 'order-items' );