WC_Cart::check_cart_item_stock PHP Method

check_cart_item_stock() public method

Looks through the cart to check each item is in stock. If not, add an error.
public check_cart_item_stock ( ) : boolean | WP_Error
return boolean | WP_Error
    public function check_cart_item_stock()
    {
        global $wpdb;
        $error = new WP_Error();
        $product_qty_in_cart = $this->get_cart_item_quantities();
        // First stock check loop
        foreach ($this->get_cart() as $cart_item_key => $values) {
            $product = $values['data'];
            /**
             * Check stock based on stock-status.
             */
            if (!$product->is_in_stock()) {
                /* translators: %s: product name */
                $error->add('out-of-stock', sprintf(__('Sorry, "%s" is not in stock. Please edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce'), $product->get_name()));
                return $error;
            }
            if (!$product->managing_stock()) {
                continue;
            }
            /**
             * Check stock based on all items in the cart.
             */
            if (!$product->has_enough_stock($product_qty_in_cart[$product->get_stock_managed_by_id()])) {
                /* translators: 1: product name 2: quantity in stock */
                $error->add('out-of-stock', sprintf(__('Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce'), $product->get_name(), $product->get_stock_quantity()));
                return $error;
            }
            /**
             * Finally consider any held stock, from pending orders.
             */
            if (get_option('woocommerce_hold_stock_minutes') > 0 && !$product->backorders_allowed()) {
                $order_id = isset(WC()->session->order_awaiting_payment) ? absint(WC()->session->order_awaiting_payment) : 0;
                $held_stock = $wpdb->get_var($wpdb->prepare("\n\t\t\t\t\t\tSELECT SUM( order_item_meta.meta_value ) AS held_qty\n\t\t\t\t\t\tFROM {$wpdb->posts} AS posts\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id\n\t\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id\n\t\t\t\t\t\tWHERE \torder_item_meta.meta_key   = '_qty'\n\t\t\t\t\t\tAND \torder_item_meta2.meta_key  = %s AND order_item_meta2.meta_value  = %d\n\t\t\t\t\t\tAND \tposts.post_type            IN ( '" . implode("','", wc_get_order_types()) . "' )\n\t\t\t\t\t\tAND \tposts.post_status          = 'wc-pending'\n\t\t\t\t\t\tAND\t\tposts.ID                   != %d;", 'variation' === get_post_type($product->get_stock_managed_by_id()) ? '_variation_id' : '_product_id', $product->get_stock_managed_by_id(), $order_id));
                if ($product->get_stock_quantity() < $held_stock + $product_qty_in_cart[$product->get_stock_managed_by_id()]) {
                    /* translators: 1: product name 2: minutes */
                    $error->add('out-of-stock', sprintf(__('Sorry, we do not have enough "%1$s" in stock to fulfill your order right now. Please try again in %2$d minutes or edit your cart and try again. We apologise for any inconvenience caused.', 'woocommerce'), $product->get_name(), get_option('woocommerce_hold_stock_minutes')));
                    return $error;
                }
            }
        }
        return true;
    }
WC_Cart