2016-11-21 19:38:07 +00:00
|
|
|
<?php
|
2018-03-08 19:28:40 +00:00
|
|
|
/**
|
|
|
|
* Class WC_Order_Item_Data_Store file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\DataStores
|
|
|
|
*/
|
|
|
|
|
2016-11-21 19:38:07 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-01 01:03:18 +00:00
|
|
|
* WC Order Item Data Store: Misc Order Item Data functions.
|
2016-11-21 19:38:07 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2016-11-21 19:38:07 +00:00
|
|
|
*/
|
|
|
|
class WC_Order_Item_Data_Store implements WC_Order_Item_Data_Store_Interface {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an order item to an order.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $order_id Order ID.
|
2017-03-20 10:03:14 +00:00
|
|
|
* @param array $item order_item_name and order_item_type.
|
2016-11-21 19:38:07 +00:00
|
|
|
* @return int Order Item ID
|
|
|
|
*/
|
|
|
|
public function add_order_item( $order_id, $item ) {
|
|
|
|
global $wpdb;
|
|
|
|
$wpdb->insert(
|
2018-03-07 19:16:01 +00:00
|
|
|
$wpdb->prefix . 'woocommerce_order_items',
|
2016-11-21 19:38:07 +00:00
|
|
|
array(
|
2018-03-07 19:16:01 +00:00
|
|
|
'order_item_name' => $item['order_item_name'],
|
|
|
|
'order_item_type' => $item['order_item_type'],
|
|
|
|
'order_id' => $order_id,
|
2016-11-21 19:38:07 +00:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%d',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2020-02-23 20:41:33 +00:00
|
|
|
$item_id = absint( $wpdb->insert_id );
|
|
|
|
|
2020-02-26 17:34:27 +00:00
|
|
|
$this->clear_caches( $item_id, $order_id );
|
2020-02-23 20:41:33 +00:00
|
|
|
|
|
|
|
return $item_id;
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an order item.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
2017-03-20 10:03:14 +00:00
|
|
|
* @param array $item order_item_name or order_item_type.
|
2016-11-21 19:38:07 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function update_order_item( $item_id, $item ) {
|
|
|
|
global $wpdb;
|
2020-02-23 20:41:33 +00:00
|
|
|
$updated = $wpdb->update( $wpdb->prefix . 'woocommerce_order_items', $item, array( 'order_item_id' => $item_id ) );
|
2020-02-26 17:34:27 +00:00
|
|
|
$this->clear_caches( $item_id, null );
|
2020-02-23 20:41:33 +00:00
|
|
|
return $updated;
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an order item.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
2016-11-21 19:38:07 +00:00
|
|
|
*/
|
|
|
|
public function delete_order_item( $item_id ) {
|
2020-02-26 17:36:28 +00:00
|
|
|
// Load the order ID before the deletion, since after, it won't exist in the database.
|
|
|
|
$order_id = $this->get_order_id_by_order_item_id( $item_id );
|
2020-02-23 20:41:33 +00:00
|
|
|
|
2016-11-21 19:38:07 +00:00
|
|
|
global $wpdb;
|
|
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) );
|
|
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d", $item_id ) );
|
2020-02-26 17:36:28 +00:00
|
|
|
|
|
|
|
$this->clear_caches( $item_id, $order_id );
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update term meta.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $meta_key Meta key.
|
|
|
|
* @param mixed $meta_value Meta value.
|
|
|
|
* @param string $prev_value (default: '').
|
2016-11-21 19:38:07 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-02-01 01:03:18 +00:00
|
|
|
public function update_metadata( $item_id, $meta_key, $meta_value, $prev_value = '' ) {
|
2017-07-04 15:09:21 +00:00
|
|
|
return update_metadata( 'order_item', $item_id, $meta_key, is_string( $meta_value ) ? wp_slash( $meta_value ) : $meta_value, $prev_value );
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add term meta.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $meta_key Meta key.
|
|
|
|
* @param mixed $meta_value Meta value.
|
|
|
|
* @param bool $unique (default: false).
|
2017-01-19 17:38:23 +00:00
|
|
|
* @return int New row ID or 0
|
2016-11-21 19:38:07 +00:00
|
|
|
*/
|
2017-02-01 01:03:18 +00:00
|
|
|
public function add_metadata( $item_id, $meta_key, $meta_value, $unique = false ) {
|
2020-03-11 20:56:02 +00:00
|
|
|
return add_metadata( 'order_item', $item_id, wp_slash( $meta_key ), is_string( $meta_value ) ? wp_slash( $meta_value ) : $meta_value, $unique );
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete term meta.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $meta_key Meta key.
|
|
|
|
* @param string $meta_value (default: '').
|
|
|
|
* @param bool $delete_all (default: false).
|
2016-11-21 19:38:07 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-02-01 01:03:18 +00:00
|
|
|
public function delete_metadata( $item_id, $meta_key, $meta_value = '', $delete_all = false ) {
|
2017-07-04 15:09:21 +00:00
|
|
|
return delete_metadata( 'order_item', $item_id, $meta_key, is_string( $meta_value ) ? wp_slash( $meta_value ) : $meta_value, $delete_all );
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get term meta.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $key Meta key.
|
|
|
|
* @param bool $single (default: true).
|
2016-11-21 19:38:07 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-02-01 01:03:18 +00:00
|
|
|
public function get_metadata( $item_id, $key, $single = true ) {
|
2016-11-21 19:38:07 +00:00
|
|
|
return get_metadata( 'order_item', $item_id, $key, $single );
|
|
|
|
}
|
|
|
|
|
2017-01-18 22:05:50 +00:00
|
|
|
/**
|
|
|
|
* Get order ID by order item ID.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
2017-01-18 22:05:50 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2018-03-08 19:28:40 +00:00
|
|
|
public function get_order_id_by_order_item_id( $item_id ) {
|
2017-01-18 22:05:50 +00:00
|
|
|
global $wpdb;
|
2018-03-07 19:16:01 +00:00
|
|
|
return (int) $wpdb->get_var(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d",
|
|
|
|
$item_id
|
|
|
|
)
|
|
|
|
);
|
2017-01-18 22:05:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 19:39:30 +00:00
|
|
|
/**
|
|
|
|
* Get the order item type based on Item ID.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:28:40 +00:00
|
|
|
* @param int $item_id Item ID.
|
2019-08-07 21:03:26 +00:00
|
|
|
* @return string|null Order item type or null if no order item entry found.
|
2017-02-08 19:39:30 +00:00
|
|
|
*/
|
|
|
|
public function get_order_item_type( $item_id ) {
|
|
|
|
global $wpdb;
|
2019-08-07 21:03:26 +00:00
|
|
|
$order_item_type = $wpdb->get_var(
|
|
|
|
$wpdb->prepare(
|
|
|
|
"SELECT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;",
|
|
|
|
$item_id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $order_item_type;
|
2017-02-08 19:39:30 +00:00
|
|
|
}
|
2020-02-23 20:11:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear meta cache.
|
|
|
|
*
|
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param int|null $order_id Order ID. If not set, it will be loaded using the item ID.
|
|
|
|
*/
|
2020-02-26 17:34:27 +00:00
|
|
|
protected function clear_caches( $item_id, $order_id ) {
|
2020-02-23 20:11:23 +00:00
|
|
|
wp_cache_delete( 'item-' . $item_id, 'order-items' );
|
|
|
|
|
|
|
|
if ( ! $order_id ) {
|
|
|
|
$order_id = $this->get_order_id_by_order_item_id( $item_id );
|
|
|
|
}
|
|
|
|
if ( $order_id ) {
|
|
|
|
wp_cache_delete( 'order-items-' . $order_id, 'orders' );
|
|
|
|
}
|
|
|
|
}
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|