Title: Managing stock progamatically
Last modified: October 11, 2020

---

# Managing stock progamatically

 *  Resolved [iqqsam182](https://wordpress.org/support/users/iqqsam182/)
 * (@iqqsam182)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/managing-stock-progamatically/)
 * Hi, I’m creating a custom checkout form and have come up with the following code,
   however this does not properly manage stock if two try and checkout the stock
   drops below 0 and becomes minus, how can I check if item is in stock and return
   a response that its out of stock? especially when two try and checkout simultaneously.
   Thanks, if there’s anything else that needs to be improved please tell me what
   should be done. (address filtering maybe?)
    function checkout{ $address = $_POST[‘
   address’]; if (!isset($address)) { return; } global $woocommerce; $address = 
   array( address fields here ); $coupon = WC()->cart->get_applied_coupons(); $order
   = wc_create_order(); $cart = WC()->cart; $order->set_currency( get_woocommerce_currency());
   $chosen_shipping_methods = WC()->session->get( ‘chosen_shipping_methods’ ); $
   cart_hash = md5( json_encode( wc_clean( $cart->get_cart_for_session() ) ) . $
   cart->total ); $order->set_cart_hash( $cart_hash ); $order->set_total( $cart-
   >total ); $order->set_shipping_total( $cart->shipping_total ); $order->set_discount_total(
   $cart->get_cart_discount_total() ); WC()->checkout->create_order_line_items( 
   $order, $cart ); WC()->checkout->create_order_shipping_lines( $order, $chosen_shipping_methods);
   WC()->checkout->create_order_coupon_lines( $order, $cart ); $cart->empty_cart();
   $order->set_address($address, ‘billing’); $order->calculate_totals(); $order-
   >save(); $order->payment_complete(); return; }

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Siew](https://wordpress.org/support/users/kosiew/)
 * (@kosiew)
 * [5 years, 7 months ago](https://wordpress.org/support/topic/managing-stock-progamatically/#post-13520664)
 * Hi there,
 * Safeguards for stocks being paid for have been implemented in [https://github.com/woocommerce/woocommerce/issues/24009](https://github.com/woocommerce/woocommerce/issues/24009).
 * You can check out the issue on how it safeguards negative stock.
 *  [dougaitken](https://wordpress.org/support/users/dougaitken/)
 * (@dougaitken)
 * Automattic Happiness Engineer
 * [5 years, 6 months ago](https://wordpress.org/support/topic/managing-stock-progamatically/#post-13567726)
 * HI [@iqqsam182](https://wordpress.org/support/users/iqqsam182/)
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.
 * Thanks,

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Managing stock progamatically’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [dougaitken](https://wordpress.org/support/users/dougaitken/)
 * Last activity: [5 years, 6 months ago](https://wordpress.org/support/topic/managing-stock-progamatically/#post-13567726)
 * Status: resolved