/** * wpsc product has stock function * @return boolean - true if the product has stock or does not use stock, false if it does not */ function wpsc_product_has_stock($id = null) { // maybe do wpsc_clear_stock_claims first? if (is_numeric($id) && $id > 0) { $id = absint($id); } else { $id = get_the_ID(); } $stock = get_post_meta($id, '_wpsc_stock', true); if ($stock === '') { return true; } $variations = get_children(array("post_type" => "wpsc-product", "post_parent" => $id)); $filter_name = empty($variations) ? 'wpsc_product_stock' : 'wpsc_product_variation_stock'; $stock = apply_filters($filter_name, (int) $stock, $id); if (!empty($variations)) { foreach ($variations as $variation) { if (wpsc_product_has_stock($variation->ID)) { return true; } } } elseif ($stock > 0) { $claimed_query = new WPSC_Claimed_Stock(array('product_id' => $id)); $claimed_stock = $claimed_query->get_claimed_stock_count(); if ($stock - $claimed_stock > 0) { return true; } } return false; }
/** * Get remaining product quantity * * @since * @access public * * @param int $product_id Cart product ID. * @param array $variations Cart item parameters. * @param int $quantity Cart object. * * @uses wpsc_product_stock Filters and restricts the product cart quantity. */ function wpsc_get_remaining_quantity($product_id, $variations = array(), $quantity = 1) { $stock = get_post_meta($product_id, '_wpsc_stock', true); $stock = apply_filters('wpsc_product_stock', $stock, $product_id); $output = 0; // check to see if the product uses stock if (is_numeric($stock)) { if ($stock > 0) { $claimed_query = new WPSC_Claimed_Stock(array('product_id' => $product_id)); $claimed_stock = $claimed_query->get_claimed_stock_count(); $output = $stock - $claimed_stock; } } return $output; }