Hello everyone, like every shop, it's important to hide items that are out of stock but spiders (google, yahoo, etc) can still find through backlinks.

I attempted to do this via Views 2 but had problems with arguments and then I had to recreate the catalog to get the specific links. So I tried to hack the 'core' of ubercart and this is what I came up with.

File edited: modules\ubercart\uc_catalog.pages.inc

After lines:
$result = pager_query($sql, variable_get('uc_product_nodes_per_page', 12), 0, $sql_count, $sql_args);
while ($node = db_fetch_object($result)) {

		// Check if primary model has any stock (if active)
		if (uc_stock_level($node->model) <= 0) {
			$product_out_of_stock = true;
			// If not, let's check other sku's associated with the product. 
			$models = module_invoke_all('uc_product_models', $node);
			foreach ($models as $sku) {
				// uc_stock_level only checks sku has been enabled.
				if (uc_stock_level($sku) > 0) { $product_out_of_stock = false; break; }
			}
		}
 		if ($product_out_of_stock) continue;


Before lines: $catalog->products[] = $node->nid;
}

This code simply stops the product being added to the view of the catalog listings for products that are not in stock. If you have alternative SKU's for the product, the linked stock will also be checked. If you have a direct link to the product, it will still work. Please note: this will stop listing any products products that do not have stock enabled.

This works well with the ajax add to cart module that does not allow customers to add items to the cart if it is not in stock (also works with alternative SKU's).

Comments

prakash.kumar’s picture

I have studied the post & apply it on uc_advanced_catalog but not working. Products list disappear.

My code:
$result = pager_query($sql, $limit, 0, $sql_count, $sql_args);
while ($node = db_fetch_object($result)) {
if (uc_stock_level($node->model) <= 0) {
$product_out_of_stock = true;
// If not, let's check other sku's associated with the product.
$models = module_invoke_all('uc_product_models', $node);
foreach ($models as $sku) {
// uc_stock_level only checks sku has been enabled.
if (uc_stock_level($sku) > 0) { $product_out_of_stock = false; break; }
}
}
if ($product_out_of_stock) continue;
$products[] = $node->nid;
}

plz check if possible & suggest.

Thanks
Prakash

Amarjit’s picture

Hello Prakash,

Sorry, this post was from 2009.

I do not follow Ubercart now but you are probably using a very late release compared the one I was using.
Please re-post this as a new issue or find a current thread.

Amarjit