wc-stock-functions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. *
  22. * @return bool|int|null
  23. */
  24. function wc_update_product_stock( $product, $stock_quantity = null, $operation = 'set' ) {
  25. $product = wc_get_product( $product );
  26. if ( ! $product ) {
  27. return false;
  28. }
  29. if ( ! is_null( $stock_quantity ) && $product->managing_stock() ) {
  30. // Some products (variations) can have their stock managed by their parent. Get the correct ID to reduce here.
  31. $product_id_with_stock = $product->get_stock_managed_by_id();
  32. $data_store = WC_Data_Store::load( 'product' );
  33. $data_store->update_product_stock( $product_id_with_stock, $stock_quantity, $operation );
  34. delete_transient( 'wc_low_stock_count' );
  35. delete_transient( 'wc_outofstock_count' );
  36. delete_transient( 'wc_product_children_' . ( $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id() ) );
  37. wp_cache_delete( 'product-' . $product_id_with_stock, 'products' );
  38. // Re-read product data after updating stock, then have stock status calculated and saved.
  39. $product_with_stock = wc_get_product( $product_id_with_stock );
  40. $product_with_stock->set_stock_status();
  41. $product_with_stock->set_date_modified( current_time( 'timestamp', true ) );
  42. $product_with_stock->save();
  43. if ( $product_with_stock->is_type( 'variation' ) ) {
  44. do_action( 'woocommerce_variation_set_stock', $product_with_stock );
  45. } else {
  46. do_action( 'woocommerce_product_set_stock', $product_with_stock );
  47. }
  48. return $product_with_stock->get_stock_quantity();
  49. }
  50. return $product->get_stock_quantity();
  51. }
  52. /**
  53. * Update a product's stock status.
  54. *
  55. * @param int $product_id Product ID.
  56. * @param int $status Status.
  57. */
  58. function wc_update_product_stock_status( $product_id, $status ) {
  59. $product = wc_get_product( $product_id );
  60. if ( $product ) {
  61. $product->set_stock_status( $status );
  62. $product->save();
  63. }
  64. }
  65. /**
  66. * When a payment is complete, we can reduce stock levels for items within an order.
  67. *
  68. * @since 3.0.0
  69. * @param int $order_id Order ID.
  70. */
  71. function wc_maybe_reduce_stock_levels( $order_id ) {
  72. $order = wc_get_order( $order_id );
  73. if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', $order && ! $order->get_data_store()->get_stock_reduced( $order_id ), $order_id ) ) {
  74. wc_reduce_stock_levels( $order );
  75. }
  76. }
  77. add_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' );
  78. /**
  79. * Reduce stock levels for items within an order.
  80. *
  81. * @since 3.0.0
  82. * @param int|WC_Order $order_id Order ID or order instance.
  83. */
  84. function wc_reduce_stock_levels( $order_id ) {
  85. if ( is_a( $order_id, 'WC_Order' ) ) {
  86. $order = $order_id;
  87. $order_id = $order->get_id();
  88. } else {
  89. $order = wc_get_order( $order_id );
  90. }
  91. if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && count( $order->get_items() ) > 0 ) {
  92. foreach ( $order->get_items() as $item ) {
  93. if ( ! $item->is_type( 'line_item' ) ) {
  94. continue;
  95. }
  96. $product = $item->get_product();
  97. if ( $product && $product->managing_stock() ) {
  98. $qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $order, $item );
  99. $item_name = $product->get_formatted_name();
  100. $new_stock = wc_update_product_stock( $product, $qty, 'decrease' );
  101. if ( ! is_wp_error( $new_stock ) ) {
  102. /* translators: 1: item name 2: old stock quantity 3: new stock quantity */
  103. $order->add_order_note( sprintf( __( '%1$s stock reduced from %2$s to %3$s.', 'woocommerce' ), $item_name, $new_stock + $qty, $new_stock ) );
  104. // Get the latest product data.
  105. $product = wc_get_product( $product->get_id() );
  106. if ( '' !== get_option( 'woocommerce_notify_no_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
  107. do_action( 'woocommerce_no_stock', $product );
  108. } elseif ( '' !== get_option( 'woocommerce_notify_low_stock_amount' ) && $new_stock <= get_option( 'woocommerce_notify_low_stock_amount' ) ) {
  109. do_action( 'woocommerce_low_stock', $product );
  110. }
  111. if ( $new_stock < 0 ) {
  112. do_action(
  113. 'woocommerce_product_on_backorder', array(
  114. 'product' => $product,
  115. 'order_id' => $order_id,
  116. 'quantity' => $qty,
  117. )
  118. );
  119. }
  120. }
  121. }
  122. }
  123. // Ensure stock is marked as "reduced" in case payment complete or other stock actions are called.
  124. $order->get_data_store()->set_stock_reduced( $order_id, true );
  125. do_action( 'woocommerce_reduce_order_stock', $order );
  126. }
  127. }