abstracts
5 years ago
admin
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
5 years ago
emails
5 years ago
export
5 years ago
gateways
5 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
6 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
rest-api
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
5 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
5 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
5 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
wc-order-item-functions.php
182 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Order Item Functions |
| 4 | * |
| 5 | * Functions for order specific things. |
| 6 | * |
| 7 | * @package WooCommerce\Functions |
| 8 | * @version 3.4.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Add a item to an order (for example a line item). |
| 15 | * |
| 16 | * @param int $order_id Order ID. |
| 17 | * @param array $item_array Items list. |
| 18 | * |
| 19 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 20 | * @return int|bool Item ID or false |
| 21 | */ |
| 22 | function wc_add_order_item( $order_id, $item_array ) { |
| 23 | $order_id = absint( $order_id ); |
| 24 | |
| 25 | if ( ! $order_id ) { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | $defaults = array( |
| 30 | 'order_item_name' => '', |
| 31 | 'order_item_type' => 'line_item', |
| 32 | ); |
| 33 | |
| 34 | $item_array = wp_parse_args( $item_array, $defaults ); |
| 35 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 36 | $item_id = $data_store->add_order_item( $order_id, $item_array ); |
| 37 | $item = WC_Order_Factory::get_order_item( $item_id ); |
| 38 | |
| 39 | do_action( 'woocommerce_new_order_item', $item_id, $item, $order_id ); |
| 40 | |
| 41 | return $item_id; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Update an item for an order. |
| 46 | * |
| 47 | * @since 2.2 |
| 48 | * @param int $item_id Item ID. |
| 49 | * @param array $args Either `order_item_type` or `order_item_name`. |
| 50 | * |
| 51 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 52 | * @return bool True if successfully updated, false otherwise. |
| 53 | */ |
| 54 | function wc_update_order_item( $item_id, $args ) { |
| 55 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 56 | $update = $data_store->update_order_item( $item_id, $args ); |
| 57 | |
| 58 | if ( false === $update ) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | do_action( 'woocommerce_update_order_item', $item_id, $args ); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Delete an item from the order it belongs to based on item id. |
| 69 | * |
| 70 | * @param int $item_id Item ID. |
| 71 | * |
| 72 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 73 | * @return bool |
| 74 | */ |
| 75 | function wc_delete_order_item( $item_id ) { |
| 76 | $item_id = absint( $item_id ); |
| 77 | |
| 78 | if ( ! $item_id ) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 83 | |
| 84 | do_action( 'woocommerce_before_delete_order_item', $item_id ); |
| 85 | |
| 86 | $data_store->delete_order_item( $item_id ); |
| 87 | |
| 88 | do_action( 'woocommerce_delete_order_item', $item_id ); |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * WooCommerce Order Item Meta API - Update term meta. |
| 95 | * |
| 96 | * @param int $item_id Item ID. |
| 97 | * @param string $meta_key Meta key. |
| 98 | * @param string $meta_value Meta value. |
| 99 | * @param string $prev_value Previous value (default: ''). |
| 100 | * |
| 101 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 102 | * @return bool |
| 103 | */ |
| 104 | function wc_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 105 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 106 | if ( $data_store->update_metadata( $item_id, $meta_key, $meta_value, $prev_value ) ) { |
| 107 | WC_Cache_Helper::invalidate_cache_group( 'object_' . $item_id ); // Invalidate cache. |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * WooCommerce Order Item Meta API - Add term meta. |
| 115 | * |
| 116 | * @param int $item_id Item ID. |
| 117 | * @param string $meta_key Meta key. |
| 118 | * @param string $meta_value Meta value. |
| 119 | * @param bool $unique If meta data should be unique (default: false). |
| 120 | * |
| 121 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 122 | * @return int New row ID or 0. |
| 123 | */ |
| 124 | function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) { |
| 125 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 126 | $meta_id = $data_store->add_metadata( $item_id, $meta_key, $meta_value, $unique ); |
| 127 | |
| 128 | if ( $meta_id ) { |
| 129 | WC_Cache_Helper::invalidate_cache_group( 'object_' . $item_id ); // Invalidate cache. |
| 130 | return $meta_id; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * WooCommerce Order Item Meta API - Delete term meta. |
| 137 | * |
| 138 | * @param int $item_id Item ID. |
| 139 | * @param string $meta_key Meta key. |
| 140 | * @param string $meta_value Meta value (default: ''). |
| 141 | * @param bool $delete_all Delete all meta data, defaults to `false`. |
| 142 | * |
| 143 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 144 | * @return bool |
| 145 | */ |
| 146 | function wc_delete_order_item_meta( $item_id, $meta_key, $meta_value = '', $delete_all = false ) { |
| 147 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 148 | if ( $data_store->delete_metadata( $item_id, $meta_key, $meta_value, $delete_all ) ) { |
| 149 | WC_Cache_Helper::invalidate_cache_group( 'object_' . $item_id ); // Invalidate cache. |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * WooCommerce Order Item Meta API - Get term meta. |
| 157 | * |
| 158 | * @param int $item_id Item ID. |
| 159 | * @param string $key Meta key. |
| 160 | * @param bool $single Whether to return a single value. (default: true). |
| 161 | * |
| 162 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 163 | * @return mixed |
| 164 | */ |
| 165 | function wc_get_order_item_meta( $item_id, $key, $single = true ) { |
| 166 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 167 | return $data_store->get_metadata( $item_id, $key, $single ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get order ID by order item ID. |
| 172 | * |
| 173 | * @param int $item_id Item ID. |
| 174 | * |
| 175 | * @throws Exception When `WC_Data_Store::load` validation fails. |
| 176 | * @return int |
| 177 | */ |
| 178 | function wc_get_order_id_by_order_item_id( $item_id ) { |
| 179 | $data_store = WC_Data_Store::load( 'order-item' ); |
| 180 | return $data_store->get_order_id_by_order_item_id( $item_id ); |
| 181 | } |
| 182 |