Question

There are a number of conditions that determine if a product can be displayed in a certain store view. From the top of my head, I know 3:

  • Is the product associated with the website?
  • Is the product configured as enabled/active for that website?
  • Does the product have a non-empty price field for that store view?

Background is that I have an external search function using a Solr server in the backend. That search function has been altered so that when a product number is entered into the search function and enter is pressed and there is a product matching that product number close to 100% (capitalization is ignored, but otherwise it needs to be a match), then it jumps directly to the product detail page, otherwise a normal search is performed.

Now, it dawned on me that I could in that way find a product that is not visible to the store view (see the conditions above). The result is that the URL is called correctly, but results in a 404.

Is there a transparent way of testing if a product could theoretically be viewed for a certain store view without finding and spelling out the exhaustive list as it currently is? I imagine something like

Mage::Helper('something')->canViewInStoreview($productId, $storeviewId)

would be right on the spot. If spelled out, the conditions in the next version could change, and I would not want to re-check with every update if the list is still complete.

Was it helpful?

Solution

I think you got it all covered.

  • The product needs to be assigned to the current website
  • The product needs to be enabled.
  • The product needs to have the visibility catalog or catalog & search in order to appear when browsing the catalog. And search or catalog & search when using the search functionality.

I don't know about the 0 price. I think products with 0 price can be displayed.

But to make sure, you can follow the trace starting from the product controller view action.

Mage_Catalog_ProductController::viewAction.
This one calls $viewHelper->prepareAndRender($productId, $this, $params); which means Mage_Catalog_Helper_Product_View::prepareAndRender().
This one calls Mage_Catalog_Helper_Product::initProduct.

and this one contains:

    if (!$this->canShow($product)) {
        return false;
    }
    if (!in_array(Mage::app()->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
        return false;
    }

Which basically means (canShow) if the product exists, is enabled and has the proper visibility it's OK. The next if checks if the product is assigned to the current website.

But you should also be careful at the custom code you have that hook onto some events dispatched in prepareAndRender method and the ones that are called from it.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top