Нема описа

wc-stock-functions.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Update a product's stock amount.
  13. *
  14. * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues).
  15. *
  16. * @since 3.0.0 this supports set, increase and decrease.
  17. *
  18. * @param int|WC_Product $product Product ID or product instance.
  19. * @param int|null $stock_quantity Stock quantity.
  20. * @param string $operation Type of opertion, allows 'set', 'increase' and 'decrease'.
  21. * @param bool $updating If true, the product object won't be saved here as it will be updated later.
  22. * @return bool|int|null
  23. */
  24. function wc_update_product_stock( $product, $stock_quantity = null, $operation = 'set', $updating = false ) {
  25. if ( ! is_a( $product, 'WC_Product' ) ) {
  26. $product = wc_get_product( $product );
  27. }
  28. if ( ! $product ) {
  29. return false;
  30. }
  31. if ( ! is_null( $stock_quantity ) && $product->managing_stock() ) {
  32. // Some products (variations) can have their stock managed by their parent. Get the correct object to be updated here.
  33. $product_id_with_stock = $product->get_stock_managed_by_id();
  34. $product_with_stock = $product_id_with_stock !== $product->get_id() ? wc_get_product( $product_id_with_stock ) : $product;
  35. $data_store = WC_Data_Store::load( 'product' );
  36. // Fire actions to let 3rd parties know the stock is about to be changed.
  37. if ( $product_with_stock->is_type( 'variation' ) ) {
  38. do_action( 'woocommerce_variation_before_set_stock', $product_with_stock );
  39. } else {
  40. do_action( 'woocommerce_product_before_set_stock', $product_with_stock );
  41. }
  42. // Update the database.
  43. $new_stock = $data_store->update_product_stock( $product_id_with_stock, $stock_quantity, $operation );
  44. // Update the product object.
  45. $data_store->read_stock_quantity( $product_with_stock, $new_stock );
  46. // If this is not being called during an update routine, save the product so stock status etc is in sync, and caches are cleared.
  47. if ( ! $updating ) {
  48. $product_with_stock->save();
  49. }
  50. // Fire actions to let 3rd parties know the stock changed.
  51. if ( $product_with_stock->is_type( 'variation' ) ) {
  52. do_action( 'woocommerce_variation_set_stock', $product_with_stock );
  53. } else {
  54. do_action( 'woocommerce_product_set_stock', $product_with_stock );
  55. }
  56. return $product_with_stock->get_stock_quantity();
  57. }
  58. return $product->get_stock_quantity();
  59. }
  60. /**
  61. * Update a product's stock status.
  62. *
  63. * @param int $product_id Product ID.
  64. * @param string $status Status.
  65. */
  66. function wc_update_product_stock_status( $product_id, $status ) {
  67. $product = wc_get_product( $product_id );
  68. if ( $product ) {
  69. $product->set_stock_status( $status );
  70. $product->save();
  71. }
  72. }
  73. /**
  74. * When a payment is complete, we can reduce stock levels for items within an order.
  75. *
  76. * @since 3.0.0
  77. * @param int $order_id Order ID.
  78. */
  79. function wc_maybe_reduce_stock_levels( $order_id ) {
  80. $order = wc_get_order( $order_id );
  81. if ( ! $order ) {
  82. return;
  83. }
  84. $stock_reduced = $order->get_data_store()->get_stock_reduced( $order_id );
  85. $trigger_reduce = apply_filters( 'woocommerce_payment_complete_reduce_order_stock', ! $stock_reduced, $order_id );
  86. // Only continue if we're reducing stock.
  87. if ( ! $trigger_reduce ) {
  88. return;
  89. }
  90. wc_reduce_stock_levels( $order );
  91. // Ensure stock is marked as "reduced" in case payment complete or other stock actions are called.
  92. $order->get_data_store()->set_stock_reduced( $order_id, true );
  93. }
  94. add_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' );
  95. add_action( 'woocommerce_order_status_completed', 'wc_maybe_reduce_stock_levels' );
  96. add_action( 'woocommerce_order_status_processing', 'wc_maybe_reduce_stock_levels' );
  97. add_action( 'woocommerce_order_status_on-hold', 'wc_maybe_reduce_stock_levels' );
  98. /**
  99. * When a payment is cancelled, restore stock.
  100. *
  101. * @since 3.0.0
  102. * @param int $order_id Order ID.
  103. */
  104. function wc_maybe_increase_stock_levels( $order_id ) {
  105. $order = wc_get_order( $order_id );
  106. if ( ! $order ) {
  107. return;
  108. }
  109. $stock_reduced = $order->get_data_store()->get_stock_reduced( $order_id );
  110. $trigger_increase = (bool) $stock_reduced;
  111. // Only continue if we're increasing stock.
  112. if ( ! $trigger_increase ) {
  113. return;
  114. }
  115. wc_increase_stock_levels( $order );
  116. // Ensure stock is not marked as "reduced" anymore.
  117. $order->get_data_store()->set_stock_reduced( $order_id, false );
  118. }
  119. add_action( 'woocommerce_order_status_cancelled', 'wc_maybe_increase_stock_levels' );
  120. add_action( 'woocommerce_order_status_pending', 'wc_maybe_increase_stock_levels' );
  121. /**
  122. * Reduce stock levels for items within an order, if stock has not already been reduced for the items.
  123. *
  124. * @since 3.0.0
  125. * @param int|WC_Order $order_id Order ID or order instance.
  126. */
  127. function wc_reduce_stock_levels( $order_id ) {
  128. if ( is_a( $order_id, 'WC_Order' ) ) {
  129. $order = $order_id;
  130. $order_id = $order->get_id();
  131. } else {
  132. $order = wc_get_order( $order_id );
  133. }
  134. // We need an order, and a store with stock management to continue.
  135. if ( ! $order || 'yes' !== get_option( 'woocommerce_manage_stock' ) || ! apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) ) {
  136. return;
  137. }
  138. $changes = array();
  139. // Loop over all items.
  140. foreach ( $order->get_items() as $item ) {
  141. if ( ! $item->is_type( 'line_item' ) ) {
  142. continue;
  143. }
  144. // Only reduce stock once for each item.
  145. $product = $item->get_product();
  146. $item_stock_reduced = $item->get_meta( '_reduced_stock', true );
  147. if ( $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
  148. continue;
  149. }
  150. /**
  151. * Filter order item quantity.
  152. *
  153. * @param int|float $quantity Quantity.
  154. * @param WC_Order $order Order data.
  155. * @param WC_Order_Item_Product $item Order item data.
  156. */
  157. $qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
  158. $item_name = $product->get_formatted_name();
  159. $new_stock = wc_update_product_stock( $product, $qty, 'decrease' );
  160. if ( is_wp_error( $new_stock ) ) {
  161. /* translators: %s item name. */
  162. $order->add_order_note( sprintf( __( 'Unable to reduce stock for item %s.', 'woocommerce' ), $item_name ) );
  163. continue;
  164. }
  165. $item->add_meta_data( '_reduced_stock', $qty, true );
  166. $item->save();
  167. $changes[] = array(
  168. 'product' => $product,
  169. 'from' => $new_stock + $qty,
  170. 'to' => $new_stock,
  171. );
  172. }
  173. wc_trigger_stock_change_notifications( $order, $changes );
  174. do_action( 'woocommerce_reduce_order_stock', $order );
  175. }
  176. /**
  177. * After stock change events, triggers emails and adds order notes.
  178. *
  179. * @since 3.5.0
  180. * @param WC_Order $order order object.
  181. * @param array $changes Array of changes.
  182. */
  183. function wc_trigger_stock_change_notifications( $order, $changes ) {
  184. if ( empty( $changes ) ) {
  185. return;
  186. }
  187. $order_notes = array();
  188. $no_stock_amount = absint( get_option( 'woocommerce_notify_no_stock_amount', 0 ) );
  189. foreach ( $changes as $change ) {
  190. $order_notes[] = $change['product']->get_formatted_name() . ' ' . $change['from'] . '&rarr;' . $change['to'];
  191. $low_stock_amount = absint( wc_get_low_stock_amount( wc_get_product( $change['product']->get_id() ) ) );
  192. if ( $change['to'] <= $no_stock_amount ) {
  193. do_action( 'woocommerce_no_stock', wc_get_product( $change['product']->get_id() ) );
  194. } elseif ( $change['to'] <= $low_stock_amount ) {
  195. do_action( 'woocommerce_low_stock', wc_get_product( $change['product']->get_id() ) );
  196. }
  197. if ( $change['to'] < 0 ) {
  198. do_action(
  199. 'woocommerce_product_on_backorder',
  200. array(
  201. 'product' => wc_get_product( $change['product']->get_id() ),
  202. 'order_id' => $order->get_id(),
  203. 'quantity' => abs( $change['from'] - $change['to'] ),
  204. )
  205. );
  206. }
  207. }
  208. $order->add_order_note( __( 'Stock levels reduced:', 'woocommerce' ) . ' ' . implode( ', ', $order_notes ) );
  209. }
  210. /**
  211. * Increase stock levels for items within an order.
  212. *
  213. * @since 3.0.0
  214. * @param int|WC_Order $order_id Order ID or order instance.
  215. */
  216. function wc_increase_stock_levels( $order_id ) {
  217. if ( is_a( $order_id, 'WC_Order' ) ) {
  218. $order = $order_id;
  219. $order_id = $order->get_id();
  220. } else {
  221. $order = wc_get_order( $order_id );
  222. }
  223. // We need an order, and a store with stock management to continue.
  224. if ( ! $order || 'yes' !== get_option( 'woocommerce_manage_stock' ) || ! apply_filters( 'woocommerce_can_restore_order_stock', true, $order ) ) {
  225. return;
  226. }
  227. $changes = array();
  228. // Loop over all items.
  229. foreach ( $order->get_items() as $item ) {
  230. if ( ! $item->is_type( 'line_item' ) ) {
  231. continue;
  232. }
  233. // Only increase stock once for each item.
  234. $product = $item->get_product();
  235. $item_stock_reduced = $item->get_meta( '_reduced_stock', true );
  236. if ( ! $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
  237. continue;
  238. }
  239. $item_name = $product->get_formatted_name();
  240. $new_stock = wc_update_product_stock( $product, $item_stock_reduced, 'increase' );
  241. if ( is_wp_error( $new_stock ) ) {
  242. /* translators: %s item name. */
  243. $order->add_order_note( sprintf( __( 'Unable to restore stock for item %s.', 'woocommerce' ), $item_name ) );
  244. continue;
  245. }
  246. $item->delete_meta_data( '_reduced_stock' );
  247. $item->save();
  248. $changes[] = $item_name . ' ' . ( $new_stock - $item_stock_reduced ) . '&rarr;' . $new_stock;
  249. }
  250. if ( $changes ) {
  251. $order->add_order_note( __( 'Stock levels increased:', 'woocommerce' ) . ' ' . implode( ', ', $changes ) );
  252. }
  253. do_action( 'woocommerce_restore_order_stock', $order );
  254. }
  255. /**
  256. * See how much stock is being held in pending orders.
  257. *
  258. * @since 3.5.0
  259. * @param WC_Product $product Product to check.
  260. * @param integer $exclude_order_id Order ID to exclude.
  261. * @return int
  262. */
  263. function wc_get_held_stock_quantity( WC_Product $product, $exclude_order_id = 0 ) {
  264. /**
  265. * Filter: woocommerce_hold_stock_for_checkout
  266. * Allows enable/disable hold stock functionality on checkout.
  267. *
  268. * @since 4.3.0
  269. * @param bool $enabled Default to true if managing stock globally.
  270. */
  271. if ( ! apply_filters( 'woocommerce_hold_stock_for_checkout', wc_string_to_bool( get_option( 'woocommerce_manage_stock', 'yes' ) ) ) ) {
  272. return 0;
  273. }
  274. return ( new \Automattic\WooCommerce\Checkout\Helpers\ReserveStock() )->get_reserved_stock( $product, $exclude_order_id );
  275. }
  276. /**
  277. * Hold stock for an order.
  278. *
  279. * @throws ReserveStockException If reserve stock fails.
  280. *
  281. * @since 4.1.0
  282. * @param \WC_Order|int $order Order ID or instance.
  283. */
  284. function wc_reserve_stock_for_order( $order ) {
  285. /**
  286. * Filter: woocommerce_hold_stock_for_checkout
  287. * Allows enable/disable hold stock functionality on checkout.
  288. *
  289. * @since @since 4.1.0
  290. * @param bool $enabled Default to true if managing stock globally.
  291. */
  292. if ( ! apply_filters( 'woocommerce_hold_stock_for_checkout', wc_string_to_bool( get_option( 'woocommerce_manage_stock', 'yes' ) ) ) ) {
  293. return;
  294. }
  295. $order = $order instanceof WC_Order ? $order : wc_get_order( $order );
  296. if ( $order ) {
  297. ( new \Automattic\WooCommerce\Checkout\Helpers\ReserveStock() )->reserve_stock_for_order( $order );
  298. }
  299. }
  300. add_action( 'woocommerce_checkout_order_created', 'wc_reserve_stock_for_order' );
  301. /**
  302. * Release held stock for an order.
  303. *
  304. * @since 4.3.0
  305. * @param \WC_Order|int $order Order ID or instance.
  306. */
  307. function wc_release_stock_for_order( $order ) {
  308. /**
  309. * Filter: woocommerce_hold_stock_for_checkout
  310. * Allows enable/disable hold stock functionality on checkout.
  311. *
  312. * @since 4.3.0
  313. * @param bool $enabled Default to true if managing stock globally.
  314. */
  315. if ( ! apply_filters( 'woocommerce_hold_stock_for_checkout', wc_string_to_bool( get_option( 'woocommerce_manage_stock', 'yes' ) ) ) ) {
  316. return;
  317. }
  318. $order = $order instanceof WC_Order ? $order : wc_get_order( $order );
  319. if ( $order ) {
  320. ( new \Automattic\WooCommerce\Checkout\Helpers\ReserveStock() )->release_stock_for_order( $order );
  321. }
  322. }
  323. add_action( 'woocommerce_checkout_order_exception', 'wc_release_stock_for_order' );
  324. add_action( 'woocommerce_payment_complete', 'wc_release_stock_for_order', 11 );
  325. add_action( 'woocommerce_order_status_cancelled', 'wc_release_stock_for_order', 11 );
  326. add_action( 'woocommerce_order_status_completed', 'wc_release_stock_for_order', 11 );
  327. add_action( 'woocommerce_order_status_processing', 'wc_release_stock_for_order', 11 );
  328. add_action( 'woocommerce_order_status_on-hold', 'wc_release_stock_for_order', 11 );
  329. /**
  330. * Return low stock amount to determine if notification needs to be sent
  331. *
  332. * Since 5.2.0, this function no longer redirects from variation to its parent product.
  333. * Low stock amount can now be attached to the variation itself and if it isn't, only
  334. * then we check the parent product, and if it's not there, then we take the default
  335. * from the store-wide setting.
  336. *
  337. * @param WC_Product $product Product to get data from.
  338. * @since 3.5.0
  339. * @return int
  340. */
  341. function wc_get_low_stock_amount( WC_Product $product ) {
  342. $low_stock_amount = $product->get_low_stock_amount();
  343. if ( '' === $low_stock_amount && $product->is_type( 'variation' ) ) {
  344. $product = wc_get_product( $product->get_parent_id() );
  345. $low_stock_amount = $product->get_low_stock_amount();
  346. }
  347. if ( '' === $low_stock_amount ) {
  348. $low_stock_amount = get_option( 'woocommerce_notify_low_stock_amount', 2 );
  349. }
  350. return (int) $low_stock_amount;
  351. }