2

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.

  • what do you mean by 'When specifying an incorrect product_id="" value in a CMS page'? You mean when you add a widget, or a block, or you put 'product_id=...' in the url? Describe what you are doing to get the error. – Marius May 24 '13 at 18:56
  • @Marius Sorry about that. – Sturm May 24 '13 at 19:03
  • can you please also update your question with the error message you are getting? – user487772 May 24 '13 at 19:15
  • Sure, as soon as I get the chance, I've left the office for the evening (until Tuesday). From memory it was complaining about an undefined index on line 77 of Type.php, which is the line in the second excerpt. – Sturm May 24 '13 at 21:13
2

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;
} ?>
  • For this particular code, I've added a check to local/...Type.php to check if the product is valid, in a similar fashion to what you have posted here. You've both mentioned button.phtml, should I take a look at this instead of fixing it through Type.php? – Sturm May 25 '13 at 3:52
  • 1
    @Wrapids fixing it in the template file is a far better solution than using a local override. The use of local overrides will complicate things for you when it comes time to upgrade. – Joe Constant May 25 '13 at 18:30
  • Also, if this is a production environment, display_errors should be off in your php.ini file so that stack traces won't be shown. – Joe Constant May 25 '13 at 18:32
  • @JoeConstant even with display_errors off it redirects to one of those magento error pages. On the comment about when time to upgrade gets here, that day will never be seen. – Sturm May 26 '13 at 0:14
  • @Wrapids for that, edit errors/local.xml (may have to create it). I believe you change display to none (XML lists options as display, trash, or email. IIRC, "none" is undocumented but causes Magento to just list the report number and not display the error text.) Additionally, you can modify the templates under errors/ to customize the error screen shown when a 5xx error occurs. – Joe Constant May 27 '13 at 3:17
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.