1

I am developing a logic to accept sku of any product as value of a parameter skuval. In view.phtml

$spProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$skuval);

   if(!$spProduct) {
        Mage::app()->getFrontController()->getResponse()->setRedirect("test");
        echo "This gets printed when sku is incorrect";
    }

Then redirecting user to product page. If the product does not exist, it shows a blank page instead of 404 page not found error

How do I redirect user forcefully to 404 page?

2

You don't want to add logic like this to a template file. Template files are only for displaying your block (in this case Mage_Catalog_Block_Product_View). So what you want is a rewrite of the Mage_Catalog_ProductController class, since this handles all requests to the product detail page.

This also has another huge benefit. As you can see in that class, it does an initProduct call on the product helper. This method loads a product. So if you don't rewrite the controller before that point (or maybe rewrite this method itself), you first load a product, which might not exist, and then load the actual product you want, based on $skuval. That's a performance issue, since loading a product is slow.

So if you need your product pages to "listen" to the product's SKU to load, you should rewrite the Mage_Catalog_ProductController::_initProduct() method or the Mage_Catalog_Helper_Product::initProduct() method, so it will also check if it can load a product based on the SKU. Keep in mind that when you rewrite the helper, the added functionality will be added to all code that uses this functionality (like Mage_XmlConnect_CatalogController).

If you want to know how to rewrite stuff in Magento, Inchoo wrote a pretty useful blogpost about it back in 2013.

0

The following code can redirect you to 404 from controller. Check if it will be helpful:

$this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
$this->getResponse()->setHeader('Status','404 File not found');
$this->_forward('defaultNoRoute');
0

Please try this

if (/** your custom logic goes here **/) {
$this->norouteAction();
return;
}
  • It says "There has been an error processing your request" Invalid method Mage_Catalog_Block_Product_View::norouteAction(Array ( ) ) – Rakesh Shewale Jun 29 '16 at 15:12

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.