/**
  * Returns the availability of the product.
  *
  * @return string
  */
 public function get_availability()
 {
     // Default to in-stock
     $availability = __('In stock', 'woocommerce');
     $class = 'in-stock';
     // If out of stock, this takes priority over all other settings.
     if (!$this->is_in_stock()) {
         $availability = __('Out of stock', 'woocommerce');
         $class = 'out-of-stock';
         // Any further we can assume status is set to in stock.
     } elseif ($this->managing_stock() && $this->is_on_backorder(1)) {
         $availability = __('Available on backorder', 'woocommerce');
         $class = 'available-on-backorder';
     } elseif (true === $this->managing_stock()) {
         switch (get_option('woocommerce_stock_format')) {
             case 'no_amount':
                 $availability = __('In stock', 'woocommerce');
                 break;
             case 'low_amount':
                 if ($this->get_stock_quantity() <= get_option('woocommerce_notify_low_stock_amount')) {
                     $availability = sprintf(__('Only %s left in stock', 'woocommerce'), $this->get_stock_quantity());
                     if ($this->backorders_allowed() && $this->backorders_require_notification()) {
                         $availability .= ' ' . __('(also available on backorder)', 'woocommerce');
                     }
                 } else {
                     $availability = __('In stock', 'woocommerce');
                 }
                 break;
             default:
                 $availability = sprintf(__('%s in stock', 'woocommerce'), $this->get_stock_quantity());
                 if ($this->backorders_allowed() && $this->backorders_require_notification()) {
                     $availability .= ' ' . __('(also available on backorder)', 'woocommerce');
                 }
                 break;
         }
     } elseif ('parent' === $this->managing_stock()) {
         return parent::get_availability();
     }
     return apply_filters('woocommerce_get_availability', array('availability' => $availability, 'class' => $class), $this);
 }
Example #2
0
 /**
  * Availability of bundle based on bundle stock and stock of bundled items.
  * In the admin area, the availability of a bundle is based on the stock of the container, to allow shop admins to restrict sales of a bundle regardless of the bundled items' availability.
  * On the front end, customers need to see whether they can purchase the item or not, so the availability is based on the stock of the container item and the stock of the bundled items.
  * If any bundled item is available on backorder or out of stock, the container item inherits this status and is no longer listed as "in stock" on the front end.
  *
  * @return 	array    availability data array
  */
 public function get_availability()
 {
     $backend_availability_data = parent::get_availability();
     if (!is_admin()) {
         if (!$this->is_synced()) {
             $this->sync_bundle();
         }
         $availability = $class = '';
         if (!$this->all_items_in_stock()) {
             $availability = __('Insufficient stock', 'woocommerce-product-bundles');
             $class = 'out-of-stock';
         } elseif ($this->has_items_on_backorder) {
             $availability = __('Available on backorder', 'woocommerce');
             $class = 'available-on-backorder';
         }
         if ($backend_availability_data['class'] == 'out-of-stock' || $backend_availability_data['class'] == 'available-on-backorder') {
             return $backend_availability_data;
         } elseif ($class == 'out-of-stock' || $class == 'available-on-backorder') {
             return array('availability' => $availability, 'class' => $class);
         }
     }
     return $backend_availability_data;
 }
 function bundle_availability()
 {
     if (!is_admin()) {
         global $woocommerce_bundles;
         foreach ($this->get_bundled_products() as $bundled_item_id => $bundled_product) {
             $sep = explode('_', $bundled_item_id);
             $id = $sep[0];
             $availability = $bundled_product->get_availability();
             // for both simple & variable items
             if ($availability['class'] == 'out-of-stock') {
                 return array('availability' => $availability['availability'], 'class' => $availability['class']);
             }
             // if any simple item is out of stock, mark bundle as out of stock
             if ($bundled_product->is_type('simple') && !$woocommerce_bundles->validate_stock($id, '', get_post_meta($this->id, 'bundle_quantity_' . $bundled_item_id, true), true, true)) {
                 return array('availability' => __('Out of stock', 'woocommerce'), 'class' => 'out-of-stock');
             } elseif ($bundled_product->is_type('variable')) {
                 $product_in_stock = false;
                 foreach ($bundled_product->get_children() as $variation_id) {
                     // stop here if this variation is not within the active set
                     if ($this->variation_filters_active[$bundled_item_id]) {
                         if (!is_array($this->allowed_variations[$bundled_item_id])) {
                             continue;
                         }
                         if (!in_array($variation_id, $this->allowed_variations[$bundled_item_id])) {
                             continue;
                         }
                     }
                     if (get_post_status($variation_id) != 'publish') {
                         continue;
                     }
                     // Disabled
                     if ($woocommerce_bundles->validate_stock($id, $variation_id, get_post_meta($this->id, 'bundle_quantity_' . $bundled_item_id, true), true, true)) {
                         $product_in_stock = true;
                     }
                 }
                 if (!$product_in_stock) {
                     return array('availability' => __('Out of stock', 'woocommerce'), 'class' => 'out-of-stock');
                 }
             }
         }
     }
     return parent::get_availability();
 }
 /**
  * Availability of bundle based on bundle-level stock and bundled-items-level stock.
  *
  * @return 	array    availability data array
  */
 public function get_availability()
 {
     $availability = parent::get_availability();
     if (is_woocommerce()) {
         if (!$this->is_synced()) {
             $this->sync_bundle();
         }
         if (parent::is_in_stock() && !$this->all_items_in_stock()) {
             $availability['availability'] = __('Insufficient stock', 'woocommerce-product-bundles');
         } elseif (parent::is_in_stock() && $this->has_items_on_backorder) {
             $availability['availability'] = __('Available on backorder', 'woocommerce');
             $availability['class'] = 'available-on-backorder';
         }
     }
     return $availability;
 }
/**
 * Get HTML to show product stock.
 * @since  2.7.0
 * @param  WC_Product $product
 * @return string
 */
function wc_get_stock_html($product)
{
    ob_start();
    $availability = $product->get_availability();
    wc_get_template('single-product/stock.php', array('product' => $product, 'class' => $availability['class'], 'availability' => $availability['availability']));
    $html = ob_get_clean();
    if (has_filter('woocommerce_stock_html')) {
        wc_deprecated_function('The woocommerce_stock_html filter', '', 'woocommerce_get_stock_html');
        $html = apply_filters('woocommerce_stock_html', $html, $availability['availability'], $product);
    }
    return apply_filters('woocommerce_get_stock_html', $html, $product);
}