WC_Product::has_enough_stock PHP Метод

has_enough_stock() публичный Метод

Returns whether or not the product has enough stock for the order.
public has_enough_stock ( mixed $quantity ) : boolean
$quantity mixed
Результат boolean
    public function has_enough_stock($quantity)
    {
        return !$this->managing_stock() || $this->backorders_allowed() || $this->get_stock_quantity() >= $quantity;
    }

Usage Example

 /**
  * Validate Klarna order
  * Checks order items' stock status.
  *
  * @since 1.0.0
  */
 public static function validate_checkout_listener()
 {
     // Read the post body
     $post_body = file_get_contents('php://input');
     // Convert post body into native object
     $data = json_decode($post_body, true);
     $all_in_stock = true;
     if (get_option('woocommerce_manage_stock') == 'yes') {
         $cart_items = $data['cart']['items'];
         foreach ($cart_items as $cart_item) {
             if ('physical' == $cart_item['type']) {
                 $cart_item_product = new WC_Product($cart_item['reference']);
                 if (!$cart_item_product->has_enough_stock($cart_item['quantity'])) {
                     $all_in_stock = false;
                 }
             }
         }
     }
     if ($all_in_stock) {
         header('HTTP/1.0 200 OK');
     } else {
         header('HTTP/1.0 303 See Other');
         header('Location: ' . WC()->cart->get_cart_url());
     }
 }
All Usage Examples Of WC_Product::has_enough_stock
WC_Product