2016-11-18 14:07:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Order Item Functions
|
|
|
|
*
|
|
|
|
* Functions for order specific things.
|
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Functions
|
2018-03-08 19:13:51 +00:00
|
|
|
* @version 3.4.0
|
2016-11-18 14:07:21 +00:00
|
|
|
*/
|
2018-03-08 19:13:51 +00:00
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
2016-11-18 14:07:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a item to an order (for example a line item).
|
|
|
|
*
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $order_id Order ID.
|
|
|
|
* @param array $item_array Items list.
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2018-06-25 06:05:42 +00:00
|
|
|
* @return int|bool Item ID or false
|
2016-11-18 14:07:21 +00:00
|
|
|
*/
|
2017-04-19 12:55:49 +00:00
|
|
|
function wc_add_order_item( $order_id, $item_array ) {
|
2016-11-18 14:07:21 +00:00
|
|
|
$order_id = absint( $order_id );
|
|
|
|
|
2017-03-07 20:24:24 +00:00
|
|
|
if ( ! $order_id ) {
|
2016-11-18 14:07:21 +00:00
|
|
|
return false;
|
2017-04-17 11:50:27 +00:00
|
|
|
}
|
2016-11-18 14:07:21 +00:00
|
|
|
|
|
|
|
$defaults = array(
|
2017-04-17 11:50:27 +00:00
|
|
|
'order_item_name' => '',
|
|
|
|
'order_item_type' => 'line_item',
|
2016-11-18 14:07:21 +00:00
|
|
|
);
|
|
|
|
|
2017-04-17 11:50:27 +00:00
|
|
|
$item_array = wp_parse_args( $item_array, $defaults );
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
2017-04-17 11:50:27 +00:00
|
|
|
$item_id = $data_store->add_order_item( $order_id, $item_array );
|
|
|
|
$item = WC_Order_Factory::get_order_item( $item_id );
|
2016-11-18 14:07:21 +00:00
|
|
|
|
|
|
|
do_action( 'woocommerce_new_order_item', $item_id, $item, $order_id );
|
|
|
|
|
|
|
|
return $item_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an item for an order.
|
|
|
|
*
|
|
|
|
* @since 2.2
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param array $args Either `order_item_type` or `order_item_name`.
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2018-03-08 19:13:51 +00:00
|
|
|
* @return bool True if successfully updated, false otherwise.
|
2016-11-18 14:07:21 +00:00
|
|
|
*/
|
|
|
|
function wc_update_order_item( $item_id, $args ) {
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
|
|
|
$update = $data_store->update_order_item( $item_id, $args );
|
2016-11-18 14:07:21 +00:00
|
|
|
|
|
|
|
if ( false === $update ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_action( 'woocommerce_update_order_item', $item_id, $args );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an item from the order it belongs to based on item id.
|
|
|
|
*
|
2018-06-25 06:05:42 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2016-11-18 14:07:21 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_delete_order_item( $item_id ) {
|
2018-03-08 19:13:51 +00:00
|
|
|
$item_id = absint( $item_id );
|
2016-11-18 14:07:21 +00:00
|
|
|
|
2018-03-08 19:13:51 +00:00
|
|
|
if ( ! $item_id ) {
|
2016-11-18 14:07:21 +00:00
|
|
|
return false;
|
2016-11-21 19:38:07 +00:00
|
|
|
}
|
2016-11-18 14:07:21 +00:00
|
|
|
|
2017-03-20 10:03:14 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
|
|
|
|
2016-11-18 14:07:21 +00:00
|
|
|
do_action( 'woocommerce_before_delete_order_item', $item_id );
|
|
|
|
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store->delete_order_item( $item_id );
|
2016-11-18 14:07:21 +00:00
|
|
|
|
|
|
|
do_action( 'woocommerce_delete_order_item', $item_id );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Order Item Meta API - Update term meta.
|
|
|
|
*
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $meta_key Meta key.
|
|
|
|
* @param string $meta_value Meta value.
|
|
|
|
* @param string $prev_value Previous value (default: '').
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2016-11-18 14:07:21 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) {
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
|
|
|
if ( $data_store->update_metadata( $item_id, $meta_key, $meta_value, $prev_value ) ) {
|
2019-11-28 13:03:57 +00:00
|
|
|
WC_Cache_Helper::invalidate_cache_group( 'object_' . $item_id ); // Invalidate cache.
|
2016-11-18 14:07:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Order Item Meta API - Add term meta.
|
|
|
|
*
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $meta_key Meta key.
|
|
|
|
* @param string $meta_value Meta value.
|
|
|
|
* @param bool $unique If meta data should be unique (default: false).
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2018-03-08 19:13:51 +00:00
|
|
|
* @return int New row ID or 0.
|
2016-11-18 14:07:21 +00:00
|
|
|
*/
|
|
|
|
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
2018-03-08 19:13:51 +00:00
|
|
|
$meta_id = $data_store->add_metadata( $item_id, $meta_key, $meta_value, $unique );
|
|
|
|
|
|
|
|
if ( $meta_id ) {
|
2019-11-28 13:03:57 +00:00
|
|
|
WC_Cache_Helper::invalidate_cache_group( 'object_' . $item_id ); // Invalidate cache.
|
2016-11-18 14:07:21 +00:00
|
|
|
return $meta_id;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Order Item Meta API - Delete term meta.
|
|
|
|
*
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $meta_key Meta key.
|
|
|
|
* @param string $meta_value Meta value (default: '').
|
|
|
|
* @param bool $delete_all Delete all meta data, defaults to `false`.
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2016-11-18 14:07:21 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_delete_order_item_meta( $item_id, $meta_key, $meta_value = '', $delete_all = false ) {
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
|
|
|
if ( $data_store->delete_metadata( $item_id, $meta_key, $meta_value, $delete_all ) ) {
|
2019-11-28 13:03:57 +00:00
|
|
|
WC_Cache_Helper::invalidate_cache_group( 'object_' . $item_id ); // Invalidate cache.
|
2016-11-18 14:07:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Order Item Meta API - Get term meta.
|
|
|
|
*
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $item_id Item ID.
|
|
|
|
* @param string $key Meta key.
|
|
|
|
* @param bool $single Whether to return a single value. (default: true).
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2016-11-18 14:07:21 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function wc_get_order_item_meta( $item_id, $key, $single = true ) {
|
2016-11-21 19:38:07 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
|
|
|
return $data_store->get_metadata( $item_id, $key, $single );
|
2016-11-18 14:07:21 +00:00
|
|
|
}
|
2017-01-18 22:05:50 +00:00
|
|
|
|
2017-01-19 17:38:23 +00:00
|
|
|
/**
|
|
|
|
* Get order ID by order item ID.
|
|
|
|
*
|
2018-03-08 19:13:51 +00:00
|
|
|
* @param int $item_id Item ID.
|
2018-06-25 06:05:42 +00:00
|
|
|
*
|
2018-06-25 09:04:49 +00:00
|
|
|
* @throws Exception When `WC_Data_Store::load` validation fails.
|
2017-01-19 17:38:23 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-18 22:05:50 +00:00
|
|
|
function wc_get_order_id_by_order_item_id( $item_id ) {
|
|
|
|
$data_store = WC_Data_Store::load( 'order-item' );
|
|
|
|
return $data_store->get_order_id_by_order_item_id( $item_id );
|
|
|
|
}
|