2

I have 3 observers set up that execute a 301 redirect via this method:

Mage::app()->getResponse()
  ->setRedirect($url, 301)
  ->sendResponse(); 

2 of the observer events execute the redirect perfectly but the 3rd one does not. I tested to ensure that my 'if' conditions are all met and the redirect function is called but it seems that the redirect is simply ignored on the last observer. Here are my events:

controller_action_predispatch_catalog_product_view - 301 redirect works
controller_action_predispatch_review_product_list - 301 redirect works
controller_action_predispatch_review_product_view - 301 redirect ignored

I took a look at the controller and couldn't determine whats preventing the 301 redirect from working on controller_action_predispatch_review_product_view. I also use Alan Storm's Commerce Bug 2 and went through the useful list of events that his module includes. Something in the Mage_Review_ProductController must be preventing the function from working but I cant put my finger on it. Please help...Thank you!

<events>
  <controller_action_predispatch_review_product_view>
    <observers>
      <commerceextensions_groupedproductredirect_observer>
        <class>CommerceExtensions_GroupedProductRedirect_Model_Observer</class>
        <method>redirectReviewView</method>
      </commerceextensions_groupedproductredirect_observer>
    </observers>
  </controller_action_predispatch_review_product_view>            
</events>

The redirect part of this function does get reached, I have verified it many times.

  public function redirectReviewView($observer)
  {
    if(Mage::getStoreConfig('groupedproductredirect/active/overall')){
      $id = Mage::app()->getRequest()->getParam('id');
      $productId = Mage::getModel('review/review')->load($id)->getEntityPkValue(); // get product id
      $product   = Mage::getModel('catalog/product')->load($productId); // load the product   
      if($product->getTypeId() == 'simple' && $product->getVisibility() == 1){
        $parentIds = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($product->getId());
        if(!empty($parentIds)){ 
          $parentProduct = Mage::getModel('catalog/product')->load($parentIds[0]); 
          if(Mage::getConfig()->getModuleConfig('CommerceExtensions_BetterReviews')->is('active', 'true')){ // IF BETTERREVIEWS EXTENSION INSTALLED   
            if(Mage::getStoreConfig('productpagereviews/active/overall')){ // IF PRODUCTPAGEREVIEWS IS ENABLED            
              // go the product page reviews section of the parent grouped product            
              $url = $parentProduct->getProductUrl().'#customer-reviews';  
            } else { // IF PRODUCTPAGEREVIEWS IS NOT ENABLED
              // go the reviews page of the parent grouped product
              $url = Mage::getUrl('review/product/list', array('id'=> $parentProduct->getId()));  
            }                   
          } else {// IF BETTERREVIEWS EXTENSION IS NOT INSTALLED
            // go the reviews page of the parent grouped product
            $url = Mage::getUrl('review/product/list', array('id'=> $parentProduct->getId()));
          }
          Mage::app()->getResponse()
            ->setRedirect($url, 301)
            ->sendResponse();                 
        }               
      }
    }
  }
  • have you tried putting an exit(); after the redirect code? – Sander Mangel Feb 18 '14 at 21:15
  • i have, its completely ignored unfortunately. – Shawn Abramson Feb 19 '14 at 1:02
  • sounds like the script never reaches that point. The only other thing I can think of is the headers already being sent preventing the redirect – Sander Mangel Feb 19 '14 at 8:43
  • it it definitely getting there, its just being ignored. – Shawn Abramson Feb 19 '14 at 13:58
  • and using a plain header('location: '.$url);, does that work? – Sander Mangel Feb 19 '14 at 14:04
2

Something is wrong or customized in your install; I've tested your approach and it should work fine. You could test for sure using the scoped postdispatch event.

However, depending on configuration, you should terminate execution after sending the response, otherwise the \Mage_Review_ProductController::preDispatch() method may set additional headers:

public function preDispatch()
{
    parent::preDispatch();

    $allowGuest = Mage::helper('review')->getIsGuestAllowToWrite();
    if (!$this->getRequest()->isDispatched()) {
        return;
    }

    $action = $this->getRequest()->getActionName();
    if (!$allowGuest && $action == 'post' && $this->getRequest()->isPost()) {
        if (!Mage::getSingleton('customer/session')->isLoggedIn()) {
            $this->setFlag('', self::FLAG_NO_DISPATCH, true);
            Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('*/*/*', array('_current' => true)));
            Mage::getSingleton('review/session')->setFormData($this->getRequest()->getPost())
                ->setRedirectUrl($this->_getRefererUrl());
            $this->_redirectUrl(Mage::helper('customer')->getLoginUrl());
        }
    }

    return $this;
}
  • Thanks Ben. Based on your advice, I added an exit; just after the redirect. There must have been headers being set elsewhere in the site, so this worked like a charm. Thanks! – Shawn Abramson Feb 26 '14 at 2:16

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.