abstract-wc-order-data-store-cpt.php
2 months ago
abstract-wc-order-item-type-data-store.php
7 months ago
class-wc-coupon-data-store-cpt.php
2 months ago
class-wc-customer-data-store-session.php
9 months ago
class-wc-customer-data-store.php
3 months ago
class-wc-customer-download-data-store.php
3 years ago
class-wc-customer-download-log-data-store.php
1 year ago
class-wc-data-store-wp.php
9 months ago
class-wc-order-data-store-cpt.php
2 months ago
class-wc-order-item-coupon-data-store.php
8 years ago
class-wc-order-item-data-store.php
2 months ago
class-wc-order-item-fee-data-store.php
8 years ago
class-wc-order-item-product-data-store.php
3 years ago
class-wc-order-item-shipping-data-store.php
1 year ago
class-wc-order-item-tax-data-store.php
6 years ago
class-wc-order-refund-data-store-cpt.php
2 months ago
class-wc-payment-token-data-store.php
4 months ago
class-wc-product-data-store-cpt.php
2 months ago
class-wc-product-grouped-data-store-cpt.php
3 months ago
class-wc-product-variable-data-store-cpt.php
1 month ago
class-wc-product-variation-data-store-cpt.php
9 months ago
class-wc-shipping-zone-data-store.php
5 months ago
class-wc-webhook-data-store.php
2 years ago
class-wc-order-item-data-store.php
191 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Order_Item_Data_Store file. |
| 4 | * |
| 5 | * @package WooCommerce\DataStores |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC Order Item Data Store: Misc Order Item Data functions. |
| 14 | * |
| 15 | * @version 3.0.0 |
| 16 | */ |
| 17 | class WC_Order_Item_Data_Store implements WC_Order_Item_Data_Store_Interface { |
| 18 | |
| 19 | /** |
| 20 | * Add an order item to an order. |
| 21 | * |
| 22 | * @since 3.0.0 |
| 23 | * @param int $order_id Order ID. |
| 24 | * @param array $item order_item_name and order_item_type. |
| 25 | * @return int Order Item ID |
| 26 | */ |
| 27 | public function add_order_item( $order_id, $item ) { |
| 28 | global $wpdb; |
| 29 | $wpdb->insert( |
| 30 | $wpdb->prefix . 'woocommerce_order_items', |
| 31 | array( |
| 32 | 'order_item_name' => $item['order_item_name'], |
| 33 | 'order_item_type' => $item['order_item_type'], |
| 34 | 'order_id' => $order_id, |
| 35 | ), |
| 36 | array( |
| 37 | '%s', |
| 38 | '%s', |
| 39 | '%d', |
| 40 | ) |
| 41 | ); |
| 42 | |
| 43 | $item_id = absint( $wpdb->insert_id ); |
| 44 | |
| 45 | $this->clear_caches( $item_id, $order_id ); |
| 46 | |
| 47 | return $item_id; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Update an order item. |
| 52 | * |
| 53 | * @since 3.0.0 |
| 54 | * @param int $item_id Item ID. |
| 55 | * @param array $item order_item_name or order_item_type. |
| 56 | * @return boolean |
| 57 | */ |
| 58 | public function update_order_item( $item_id, $item ) { |
| 59 | global $wpdb; |
| 60 | $updated = $wpdb->update( $wpdb->prefix . 'woocommerce_order_items', $item, array( 'order_item_id' => $item_id ) ); |
| 61 | $this->clear_caches( $item_id, null ); |
| 62 | return $updated; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Delete an order item. |
| 67 | * |
| 68 | * @since 3.0.0 |
| 69 | * @param int $item_id Item ID. |
| 70 | */ |
| 71 | public function delete_order_item( $item_id ) { |
| 72 | // Load the order ID before the deletion, since after, it won't exist in the database. |
| 73 | $order_id = $this->get_order_id_by_order_item_id( $item_id ); |
| 74 | |
| 75 | global $wpdb; |
| 76 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) ); |
| 77 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d", $item_id ) ); |
| 78 | |
| 79 | $this->clear_caches( $item_id, $order_id ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Update term meta. |
| 84 | * |
| 85 | * @since 3.0.0 |
| 86 | * @param int $item_id Item ID. |
| 87 | * @param string $meta_key Meta key. |
| 88 | * @param mixed $meta_value Meta value. |
| 89 | * @param string $prev_value (default: ''). |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function update_metadata( $item_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 93 | return update_metadata( 'order_item', $item_id, $meta_key, is_string( $meta_value ) ? wp_slash( $meta_value ) : $meta_value, $prev_value ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Add term meta. |
| 98 | * |
| 99 | * @since 3.0.0 |
| 100 | * @param int $item_id Item ID. |
| 101 | * @param string $meta_key Meta key. |
| 102 | * @param mixed $meta_value Meta value. |
| 103 | * @param bool $unique (default: false). |
| 104 | * @return int New row ID or 0 |
| 105 | */ |
| 106 | public function add_metadata( $item_id, $meta_key, $meta_value, $unique = false ) { |
| 107 | return add_metadata( 'order_item', $item_id, wp_slash( $meta_key ), is_string( $meta_value ) ? wp_slash( $meta_value ) : $meta_value, $unique ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Delete term meta. |
| 112 | * |
| 113 | * @since 3.0.0 |
| 114 | * @param int $item_id Item ID. |
| 115 | * @param string $meta_key Meta key. |
| 116 | * @param mixed $meta_value (default: ''). |
| 117 | * @param bool $delete_all (default: false). |
| 118 | * @return bool |
| 119 | */ |
| 120 | public function delete_metadata( $item_id, $meta_key, $meta_value = '', $delete_all = false ) { |
| 121 | return delete_metadata( 'order_item', $item_id, $meta_key, is_string( $meta_value ) ? wp_slash( $meta_value ) : $meta_value, $delete_all ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get term meta. |
| 126 | * |
| 127 | * @since 3.0.0 |
| 128 | * @param int $item_id Item ID. |
| 129 | * @param string $key Meta key. |
| 130 | * @param bool $single (default: true). |
| 131 | * @return mixed |
| 132 | */ |
| 133 | public function get_metadata( $item_id, $key, $single = true ) { |
| 134 | return get_metadata( 'order_item', $item_id, $key, $single ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get order ID by order item ID. |
| 139 | * |
| 140 | * @since 3.0.0 |
| 141 | * @param int $item_id Item ID. |
| 142 | * @return int |
| 143 | */ |
| 144 | public function get_order_id_by_order_item_id( $item_id ) { |
| 145 | global $wpdb; |
| 146 | return (int) $wpdb->get_var( |
| 147 | $wpdb->prepare( |
| 148 | "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", |
| 149 | $item_id |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get the order item type based on Item ID. |
| 156 | * |
| 157 | * @since 3.0.0 |
| 158 | * @param int $item_id Item ID. |
| 159 | * @return string|null Order item type or null if no order item entry found. |
| 160 | */ |
| 161 | public function get_order_item_type( $item_id ) { |
| 162 | global $wpdb; |
| 163 | $order_item_type = $wpdb->get_var( |
| 164 | $wpdb->prepare( |
| 165 | "SELECT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", |
| 166 | $item_id |
| 167 | ) |
| 168 | ); |
| 169 | |
| 170 | return $order_item_type; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Clear meta cache. |
| 175 | * |
| 176 | * @param int $item_id Item ID. |
| 177 | * @param int|null $order_id Order ID. If not set, it will be loaded using the item ID. |
| 178 | */ |
| 179 | protected function clear_caches( $item_id, $order_id ) { |
| 180 | wp_cache_delete( 'item-' . $item_id, 'order-items' ); |
| 181 | |
| 182 | if ( ! $order_id ) { |
| 183 | $order_id = $this->get_order_id_by_order_item_id( $item_id ); |
| 184 | } |
| 185 | if ( $order_id ) { |
| 186 | wp_cache_delete( 'order-items-' . $order_id, 'orders' ); |
| 187 | wp_cache_delete( 'order-needs-processing-' . $order_id, 'orders' ); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 |