当前位置: 首页>>代码示例>>PHP>>正文


PHP WC_Product::has_enough_stock方法代码示例

本文整理汇总了PHP中WC_Product::has_enough_stock方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product::has_enough_stock方法的具体用法?PHP WC_Product::has_enough_stock怎么用?PHP WC_Product::has_enough_stock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WC_Product的用法示例。


在下文中一共展示了WC_Product::has_enough_stock方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: validate_checkout_listener

 /**
  * 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());
     }
 }
开发者ID:NoviumDesign,项目名称:polefitness,代码行数:31,代码来源:class-klarna-validate.php

示例2: has_enough_stock

 /**
  * Returns whether or not the product has enough stock for the order.
  *
  * @param mixed $quantity
  * @return bool
  */
 public function has_enough_stock($quantity)
 {
     if (true === $this->managing_stock()) {
         return parent::has_enough_stock($quantity);
     } else {
         return $this->parent->has_enough_stock($quantity);
     }
 }
开发者ID:noman1059,项目名称:woocommerce,代码行数:14,代码来源:class-wc-product-variation.php

示例3: array

 /**
  * Add a product to the cart
  *
  * @param   string	product_id	contains the id of the product to add to the cart
  * @param   string	quantity	contains the quantity of the item to add
  * @param   int     variation_id
  * @param   array   variation attribute values
  */
 function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = '')
 {
     global $woocommerce;
     if ($quantity < 1) {
         return false;
     }
     // Load cart item data - may be added by other plugins
     $cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', array(), $product_id);
     // Generate a ID based on product ID, variation ID, variation data, and other cart item data
     $cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
     // See if this product and its options is already in the cart
     $cart_item_key = $this->find_product_in_cart($cart_id);
     if ($variation_id > 0) {
         $product_data = new WC_Product_Variation($variation_id);
     } else {
         $product_data = new WC_Product($product_id);
     }
     // Type/Exists check
     if ($product_data->is_type('external') || !$product_data->exists()) {
         $woocommerce->add_error(__('This product cannot be purchased.', 'woocommerce'));
         return false;
     }
     // Price set check
     if ($product_data->get_price() === '') {
         $woocommerce->add_error(__('This product cannot be purchased - the price is not yet set.', 'woocommerce'));
         return false;
     }
     // Stock check - only check if we're managing stock and backorders are not allowed
     if (!$product_data->has_enough_stock($quantity)) {
         $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woocommerce'), $product_data->get_stock_quantity()));
         return false;
     } elseif (!$product_data->is_in_stock()) {
         $woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce'));
         return false;
     }
     if ($cart_item_key) {
         $quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
         // Stock check - this time accounting for whats already in-cart
         if (!$product_data->has_enough_stock($quantity)) {
             $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock and you already have %s in your cart.', 'woocommerce'), $product_data->get_stock_quantity(), $this->cart_contents[$cart_item_key]['quantity']));
             return false;
         } elseif (!$product_data->is_in_stock()) {
             $woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce'));
             return false;
         }
         $this->set_quantity($cart_item_key, $quantity);
     } else {
         // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
         $this->cart_contents[$cart_id] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)));
     }
     $this->set_session();
     return true;
 }
开发者ID:bidhanbaral,项目名称:fotodep_store,代码行数:61,代码来源:class-wc-cart.php

示例4: array

 /**
  * Check stock before attempting to call the add_to_cart function
  * Some double checking happens, but it's better than partially adding items to the cart
  **/
 function validate_stock($product_id, $variation_id, $quantity, $exclude_cart, $silent)
 {
     global $woocommerce;
     if ($variation_id > 0) {
         if ($this->is_wc_v2()) {
             $product_data = get_product($variation_id, array('product_type' => 'variation'));
         } else {
             $product_data = new WC_Product_Variation($variation_id);
         }
     } else {
         if ($this->is_wc_v2()) {
             $product_data = get_product($product_id, array('product_type' => 'simple'));
         } else {
             $product_data = new WC_Product($product_id);
         }
     }
     // Stock check - only check if we're managing stock and backorders are not allowed.
     if (!$product_data->is_in_stock()) {
         if (!$silent) {
             $woocommerce->add_error(sprintf(__('You cannot add this product to the cart since "%s" is out of stock.', 'woo-bundles'), $product_data->get_title()));
         }
         return false;
     } elseif (!$product_data->has_enough_stock($quantity)) {
         if (!$silent) {
             $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock of "%s". We have %s in stock.', 'woo-bundles'), $product_data->get_title(), $product_data->get_stock_quantity()));
         }
         return false;
     }
     // Stock check - this time accounting for whats already in-cart.
     if ($exclude_cart) {
         return true;
     }
     $product_qty_in_cart = $woocommerce->cart->get_cart_item_quantities();
     if ($product_data->managing_stock()) {
         // Variations
         if ($variation_id && $product_data->variation_has_stock) {
             if (isset($product_qty_in_cart[$variation_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$variation_id] + $quantity)) {
                 if (!$silent) {
                     $woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a>You cannot add that amount to the cart since there is not enough stock of "%s" &mdash; we have %s in stock and you already have %s in your cart.', 'woo-bundles'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), $product_data->get_title(), $product_data->get_stock_quantity(), $product_qty_in_cart[$variation_id]));
                 }
                 return false;
             }
             // Products
         } else {
             if (isset($product_qty_in_cart[$product_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$product_id] + $quantity)) {
                 if (!$silent) {
                     $woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a>You cannot add that amount to the cart since there is not enough stock of "%s" &mdash; we have %s in stock and you already have %s in your cart.', 'woo-bundles'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), $product_data->get_title(), $product_data->get_stock_quantity(), $product_qty_in_cart[$product_id]));
                 }
                 return false;
             }
         }
     }
     return true;
 }
开发者ID:iplaydu,项目名称:Bob-Ellis-Shoes,代码行数:58,代码来源:woocommerce-product-bundles.php

示例5: array

 /**
  * Add a product to the cart
  *
  * @param string $product_id contains the id of the product to add to the cart
  * @param string $quantity contains the quantity of the item to add
  * @param int $variation_id
  * @param array $variation attribute values
  * @param array $cart_item_data extra cart item data we want to pass into the item
  * @return bool
  */
 function add_to_cart($product_id, $quantity = 1, $variation_id = '', $variation = '', $cart_item_data = array())
 {
     global $woocommerce;
     if ($quantity < 1) {
         return false;
     }
     // Load cart item data - may be added by other plugins
     $cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', $cart_item_data, $product_id);
     // Generate a ID based on product ID, variation ID, variation data, and other cart item data
     $cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
     // See if this product and its options is already in the cart
     $cart_item_key = $this->find_product_in_cart($cart_id);
     if ($variation_id > 0) {
         $product_data = new WC_Product_Variation($variation_id);
     } else {
         $product_data = new WC_Product($product_id);
     }
     // Force quantity to 1 if sold individually
     if ($product_data->is_sold_individually()) {
         $quantity = 1;
     }
     // Type/Exists check
     if ($product_data->is_type('external') || !$product_data->exists()) {
         $woocommerce->add_error(__('This product cannot be purchased.', 'woocommerce'));
         return false;
     }
     // Price set check
     if ($product_data->get_price() === '') {
         $woocommerce->add_error(__('This product cannot be purchased - the price is not yet set.', 'woocommerce'));
         return false;
     }
     // Stock check - only check if we're managing stock and backorders are not allowed
     if (!$product_data->has_enough_stock($quantity)) {
         $woocommerce->add_error(sprintf(__('You cannot add that amount to the cart since there is not enough stock. We have %s in stock.', 'woocommerce'), $product_data->get_stock_quantity()));
         return false;
     } elseif (!$product_data->is_in_stock()) {
         $woocommerce->add_error(__('You cannot add that product to the cart since the product is out of stock.', 'woocommerce'));
         return false;
     }
     // Downloadable/virtual qty check
     if ($product_data->is_sold_individually()) {
         $in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] + $quantity : $quantity;
         // If its greater than 1, its already in the cart
         if ($in_cart_quantity > 1) {
             $woocommerce->add_error(sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('You already have this item in your cart.', 'woocommerce')));
             return false;
         }
     }
     // Stock check - this time accounting for whats already in-cart
     $product_qty_in_cart = $this->get_cart_item_quantities();
     if ($product_data->managing_stock()) {
         // Variations
         if ($variation_id && $product_data->variation_has_stock) {
             if (isset($product_qty_in_cart[$variation_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$variation_id] + $quantity)) {
                 $woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a> You cannot add that amount to the cart &mdash; we have %s in stock and you already have %s in your cart.', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), $product_data->get_stock_quantity(), $product_qty_in_cart[$variation_id]));
                 return false;
             }
             // Products
         } else {
             if (isset($product_qty_in_cart[$product_id]) && !$product_data->has_enough_stock($product_qty_in_cart[$product_id] + $quantity)) {
                 $woocommerce->add_error(sprintf(__('<a href="%s" class="button">%s</a> You cannot add that amount to the cart &mdash; we have %s in stock and you already have %s in your cart.', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), $product_data->get_stock_quantity(), $product_qty_in_cart[$product_id]));
                 return false;
             }
         }
     }
     // If cart_item_key is set, the item is already in the cart
     if ($cart_item_key) {
         $new_quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
         $this->set_quantity($cart_item_key, $new_quantity);
     } else {
         $cart_item_key = $cart_id;
         // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
         $this->cart_contents[$cart_item_key] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)), $cart_item_key);
     }
     do_action('woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data);
     $woocommerce->cart_has_contents_cookie(true);
     $this->set_session();
     return true;
 }
开发者ID:eddiewilson,项目名称:new-ke,代码行数:89,代码来源:class-wc-cart.php


注:本文中的WC_Product::has_enough_stock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。