Skip to content

Instantly share code, notes, and snippets.

@rakshazi
Created July 18, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakshazi/593978d3e90dbafa599f881688b5dd0f to your computer and use it in GitHub Desktop.
Save rakshazi/593978d3e90dbafa599f881688b5dd0f to your computer and use it in GitHub Desktop.
<?php
public function viewAction()
{
// Get initial data from request
$categoryId = (int) $this->getRequest()->getParam('category', false);
$productId = (int) $this->getRequest()->getParam('id');
$specifyOptions = $this->getRequest()->getParam('options');
/////////////////////////////////////// START WITH 301 REDIRECT
$product = Mage::getModel('catalog/product')->load($productId);
$currentURL = Mage::helper('core/url')->getCurrentUrl();
$getParams = '';
$params = $this->getRequest()->getParams();
if (isset($params['id'])) {
unset($params['id']);
}
if ($params) {
$getParams = '?' . http_build_query($params);
}
if ($currentURL != $product->getProductUrl() . $getParams) {
$this->getResponse()->setRedirect($product->getProductUrl() . $getParams, 301)->sendResponse();
}
/////////////////////////////////////// END WITH 301 REDIRECT
// Prepare helper and params
$viewHelper = Mage::helper('catalog/product_view');
$params = new Varien_Object();
$params->setCategoryId($categoryId);
$params->setSpecifyOptions($specifyOptions);
// Render page
try {
$viewHelper->prepareAndRender($productId, $this, $params);
} catch (Exception $e) {
if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {
if (isset($_GET['store']) && !$this->getResponse()->isRedirect()) {
$this->_redirect('');
} elseif (!$this->getResponse()->isRedirect()) {
$this->_forward('noRoute');
}
} else {
Mage::logException($e);
$this->_forward('noRoute');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment