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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / wc-stock-functions.php
woocommerce / includes Last commit date
abstracts 5 years ago admin 5 years ago cli 5 years ago customizer 5 years ago data-stores 4 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 5 years ago log-handlers 5 years ago payment-tokens 5 years ago queue 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 5 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 5 years ago class-wc-data-store.php 5 years ago class-wc-datetime.php 5 years ago class-wc-deprecated-action-hooks.php 5 years ago class-wc-deprecated-filter-hooks.php 5 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 5 years ago class-wc-geolocation.php 5 years ago class-wc-https.php 5 years ago class-wc-install.php 4 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 5 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 5 years ago class-wc-privacy-exporters.php 5 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 5 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 4 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 4 years ago wc-user-functions.php 5 years ago wc-webhook-functions.php 5 years ago wc-widget-functions.php 5 years ago
wc-stock-functions.php
347 lines
1 <?php
2 /**
3 * WooCommerce Stock Functions
4 *
5 * Functions used to manage product stock levels.
6 *
7 * @package WooCommerce/Functions
8 * @version 3.4.0
9 */
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Update a product's stock amount.
15 *
16 * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues).
17 *
18 * @since 3.0.0 this supports set, increase and decrease.
19 *
20 * @param int|WC_Product $product Product ID or product instance.
21 * @param int|null $stock_quantity Stock quantity.
22 * @param string $operation Type of opertion, allows 'set', 'increase' and 'decrease'.
23 * @param bool $updating If true, the product object won't be saved here as it will be updated later.
24 * @return bool|int|null
25 */
26 function wc_update_product_stock( $product, $stock_quantity = null, $operation = 'set', $updating = false ) {
27 if ( ! is_a( $product, 'WC_Product' ) ) {
28 $product = wc_get_product( $product );
29 }
30
31 if ( ! $product ) {
32 return false;
33 }
34
35 if ( ! is_null( $stock_quantity ) && $product->managing_stock() ) {
36 // Some products (variations) can have their stock managed by their parent. Get the correct object to be updated here.
37 $product_id_with_stock = $product->get_stock_managed_by_id();
38 $product_with_stock = $product_id_with_stock !== $product->get_id() ? wc_get_product( $product_id_with_stock ) : $product;
39 $data_store = WC_Data_Store::load( 'product' );
40
41 // Update the database.
42 $new_stock = $data_store->update_product_stock( $product_id_with_stock, $stock_quantity, $operation );
43
44 // Update the product object.
45 $data_store->read_stock_quantity( $product_with_stock, $new_stock );
46
47 // If this is not being called during an update routine, save the product so stock status etc is in sync, and caches are cleared.
48 if ( ! $updating ) {
49 $product_with_stock->set_stock_status();
50 $product_with_stock->save();
51 }
52
53 // Fire actions to let 3rd parties know the stock changed.
54 if ( $product_with_stock->is_type( 'variation' ) ) {
55 do_action( 'woocommerce_variation_set_stock', $product_with_stock );
56 } else {
57 do_action( 'woocommerce_product_set_stock', $product_with_stock );
58 }
59
60 return $product_with_stock->get_stock_quantity();
61 }
62 return $product->get_stock_quantity();
63 }
64
65 /**
66 * Update a product's stock status.
67 *
68 * @param int $product_id Product ID.
69 * @param int $status Status.
70 */
71 function wc_update_product_stock_status( $product_id, $status ) {
72 $product = wc_get_product( $product_id );
73 if ( $product ) {
74 $product->set_stock_status( $status );
75 $product->save();
76 }
77 }
78
79 /**
80 * When a payment is complete, we can reduce stock levels for items within an order.
81 *
82 * @since 3.0.0
83 * @param int $order_id Order ID.
84 */
85 function wc_maybe_reduce_stock_levels( $order_id ) {
86 $order = wc_get_order( $order_id );
87
88 if ( ! $order ) {
89 return;
90 }
91
92 $stock_reduced = $order->get_data_store()->get_stock_reduced( $order_id );
93 $trigger_reduce = apply_filters( 'woocommerce_payment_complete_reduce_order_stock', ! $stock_reduced, $order_id );
94
95 // Only continue if we're reducing stock.
96 if ( ! $trigger_reduce ) {
97 return;
98 }
99
100 wc_reduce_stock_levels( $order );
101
102 // Ensure stock is marked as "reduced" in case payment complete or other stock actions are called.
103 $order->get_data_store()->set_stock_reduced( $order_id, true );
104 }
105 add_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' );
106 add_action( 'woocommerce_order_status_completed', 'wc_maybe_reduce_stock_levels' );
107 add_action( 'woocommerce_order_status_processing', 'wc_maybe_reduce_stock_levels' );
108 add_action( 'woocommerce_order_status_on-hold', 'wc_maybe_reduce_stock_levels' );
109
110 /**
111 * When a payment is cancelled, restore stock.
112 *
113 * @since 3.0.0
114 * @param int $order_id Order ID.
115 */
116 function wc_maybe_increase_stock_levels( $order_id ) {
117 $order = wc_get_order( $order_id );
118
119 if ( ! $order ) {
120 return;
121 }
122
123 $stock_reduced = $order->get_data_store()->get_stock_reduced( $order_id );
124 $trigger_increase = (bool) $stock_reduced;
125
126 // Only continue if we're increasing stock.
127 if ( ! $trigger_increase ) {
128 return;
129 }
130
131 wc_increase_stock_levels( $order );
132
133 // Ensure stock is not marked as "reduced" anymore.
134 $order->get_data_store()->set_stock_reduced( $order_id, false );
135 }
136 add_action( 'woocommerce_order_status_cancelled', 'wc_maybe_increase_stock_levels' );
137 add_action( 'woocommerce_order_status_pending', 'wc_maybe_increase_stock_levels' );
138
139 /**
140 * Reduce stock levels for items within an order, if stock has not already been reduced for the items.
141 *
142 * @since 3.0.0
143 * @param int|WC_Order $order_id Order ID or order instance.
144 */
145 function wc_reduce_stock_levels( $order_id ) {
146 if ( is_a( $order_id, 'WC_Order' ) ) {
147 $order = $order_id;
148 $order_id = $order->get_id();
149 } else {
150 $order = wc_get_order( $order_id );
151 }
152 // We need an order, and a store with stock management to continue.
153 if ( ! $order || 'yes' !== get_option( 'woocommerce_manage_stock' ) || ! apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) ) {
154 return;
155 }
156
157 $changes = array();
158
159 // Loop over all items.
160 foreach ( $order->get_items() as $item ) {
161 if ( ! $item->is_type( 'line_item' ) ) {
162 continue;
163 }
164
165 // Only reduce stock once for each item.
166 $product = $item->get_product();
167 $item_stock_reduced = $item->get_meta( '_reduced_stock', true );
168
169 if ( $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
170 continue;
171 }
172
173 $qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
174 $item_name = $product->get_formatted_name();
175 $new_stock = wc_update_product_stock( $product, $qty, 'decrease' );
176
177 if ( is_wp_error( $new_stock ) ) {
178 /* translators: %s item name. */
179 $order->add_order_note( sprintf( __( 'Unable to reduce stock for item %s.', 'woocommerce' ), $item_name ) );
180 continue;
181 }
182
183 $item->add_meta_data( '_reduced_stock', $qty, true );
184 $item->save();
185
186 $changes[] = array(
187 'product' => $product,
188 'from' => $new_stock + $qty,
189 'to' => $new_stock,
190 );
191 }
192
193 wc_trigger_stock_change_notifications( $order, $changes );
194
195 do_action( 'woocommerce_reduce_order_stock', $order );
196 }
197
198 /**
199 * After stock change events, triggers emails and adds order notes.
200 *
201 * @since 3.5.0
202 * @param WC_Order $order order object.
203 * @param array $changes Array of changes.
204 */
205 function wc_trigger_stock_change_notifications( $order, $changes ) {
206 if ( empty( $changes ) ) {
207 return;
208 }
209
210 $order_notes = array();
211 $no_stock_amount = absint( get_option( 'woocommerce_notify_no_stock_amount', 0 ) );
212
213 foreach ( $changes as $change ) {
214 $order_notes[] = $change['product']->get_formatted_name() . ' ' . $change['from'] . '&rarr;' . $change['to'];
215 $low_stock_amount = absint( wc_get_low_stock_amount( wc_get_product( $change['product']->get_id() ) ) );
216 if ( $change['to'] <= $no_stock_amount ) {
217 do_action( 'woocommerce_no_stock', wc_get_product( $change['product']->get_id() ) );
218 } elseif ( $change['to'] <= $low_stock_amount ) {
219 do_action( 'woocommerce_low_stock', wc_get_product( $change['product']->get_id() ) );
220 }
221
222 if ( $change['to'] < 0 ) {
223 do_action(
224 'woocommerce_product_on_backorder',
225 array(
226 'product' => wc_get_product( $change['product']->get_id() ),
227 'order_id' => $order->get_id(),
228 'quantity' => abs( $change['from'] - $change['to'] ),
229 )
230 );
231 }
232 }
233
234 $order->add_order_note( __( 'Stock levels reduced:', 'woocommerce' ) . ' ' . implode( ', ', $order_notes ) );
235 }
236
237 /**
238 * Increase stock levels for items within an order.
239 *
240 * @since 3.0.0
241 * @param int|WC_Order $order_id Order ID or order instance.
242 */
243 function wc_increase_stock_levels( $order_id ) {
244 if ( is_a( $order_id, 'WC_Order' ) ) {
245 $order = $order_id;
246 $order_id = $order->get_id();
247 } else {
248 $order = wc_get_order( $order_id );
249 }
250
251 // We need an order, and a store with stock management to continue.
252 if ( ! $order || 'yes' !== get_option( 'woocommerce_manage_stock' ) || ! apply_filters( 'woocommerce_can_restore_order_stock', true, $order ) ) {
253 return;
254 }
255
256 $changes = array();
257
258 // Loop over all items.
259 foreach ( $order->get_items() as $item ) {
260 if ( ! $item->is_type( 'line_item' ) ) {
261 continue;
262 }
263
264 // Only increase stock once for each item.
265 $product = $item->get_product();
266 $item_stock_reduced = $item->get_meta( '_reduced_stock', true );
267
268 if ( ! $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
269 continue;
270 }
271
272 $item_name = $product->get_formatted_name();
273 $new_stock = wc_update_product_stock( $product, $item_stock_reduced, 'increase' );
274
275 if ( is_wp_error( $new_stock ) ) {
276 /* translators: %s item name. */
277 $order->add_order_note( sprintf( __( 'Unable to restore stock for item %s.', 'woocommerce' ), $item_name ) );
278 continue;
279 }
280
281 $item->delete_meta_data( '_reduced_stock' );
282 $item->save();
283
284 $changes[] = $item_name . ' ' . ( $new_stock - $item_stock_reduced ) . '&rarr;' . $new_stock;
285 }
286
287 if ( $changes ) {
288 $order->add_order_note( __( 'Stock levels increased:', 'woocommerce' ) . ' ' . implode( ', ', $changes ) );
289 }
290
291 do_action( 'woocommerce_restore_order_stock', $order );
292 }
293
294 /**
295 * See how much stock is being held in pending orders.
296 *
297 * @since 3.5.0
298 * @param WC_Product $product Product to check.
299 * @param integer $exclude_order_id Order ID to exclude.
300 * @return int
301 */
302 function wc_get_held_stock_quantity( $product, $exclude_order_id = 0 ) {
303 global $wpdb;
304
305 return $wpdb->get_var(
306 $wpdb->prepare(
307 "
308 SELECT SUM( order_item_meta.meta_value ) AS held_qty
309 FROM {$wpdb->posts} AS posts
310 LEFT JOIN {$wpdb->postmeta} as postmeta ON posts.ID = postmeta.post_id
311 LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id
312 LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
313 LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id
314 WHERE order_item_meta.meta_key = '_qty'
315 AND order_item_meta2.meta_key = %s
316 AND order_item_meta2.meta_value = %d
317 AND postmeta.meta_key = '_created_via'
318 AND postmeta.meta_value = 'checkout'
319 AND posts.post_type IN ( '" . implode( "','", wc_get_order_types() ) . "' )
320 AND posts.post_status = 'wc-pending'
321 AND posts.ID != %d;",
322 'product_variation' === get_post_type( $product->get_stock_managed_by_id() ) ? '_variation_id' : '_product_id',
323 $product->get_stock_managed_by_id(),
324 $exclude_order_id
325 )
326 ); // WPCS: unprepared SQL ok.
327 }
328
329 /**
330 * Return low stock amount to determine if notification needs to be sent
331 *
332 * @param WC_Product $product Product to get data from.
333 * @since 3.5.0
334 * @return int
335 */
336 function wc_get_low_stock_amount( WC_Product $product ) {
337 if ( $product->is_type( 'variation' ) ) {
338 $product = wc_get_product( $product->get_parent_id() );
339 }
340 $low_stock_amount = $product->get_low_stock_amount();
341 if ( '' === $low_stock_amount ) {
342 $low_stock_amount = get_option( 'woocommerce_notify_low_stock_amount', 2 );
343 }
344
345 return $low_stock_amount;
346 }
347