Question

When specifying an incorrect product_id="" value (non existent product etc.) in a CMS page, it renders a PHP error on the page rather than any much less ugly alternatives. Is there a built in method to suppress these errors, or a clean way to have it redirect to a 404 page if the product isn't found?

      {{block type="catalog/product_view" product_id="12130" template="catalog/product/cms_piece/button.phtml"}}

Above is an example of how the product is being called. What's actually causing the error is that $product->getTypeId() is returning NULL, and Magento is trying to use that as an index for $typeModel in Catalog/Model/Product/Type.php.

The error can be solved by creating a conditional to check for null before allowing it execute:

            $typeModel->setConfig($types[$product->getTypeId()]);

This is a bit different of an issue than my original problem, but my concern is that this my not be the best way to solve this problem, as I don't know what effects this may have elsewhere.

Was it helpful?

Solution

Just like runamok's answer you should add a check to the top of your template 'catalog/product/cms_piece/button.phtml' somthing like

<?php if(!$this->getProduct()->getId()) {
    Mage::app()->getFrontController()->getResponse()->setRedirect('no-route', 404);
    Mage::app()->getFrontController()->getResponse()->sendResponse();
    exit;
} ?>

OTHER TIPS

I would recommend copying the catalog/product/cms_piece/button.phtml to your theme (so you are not modifying core code) and put in some code validating that the product object is not null before trying to use it. Then whoever is editing CMS content won't have to worry about it.

Additionally there are multiple ways to turn off displaying errors but you should fix the root cause instead.

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