Jump to content

How Do I Redirect Disabled Product To Category in prestashop 1.6


mehdi.7618

Recommended Posts

In backoffice when you edit a product.

Once you disabled the product you can redirect it to another product. But I'm trying to redirect it to a category instead. when the product is selected off (not active),

if you select 301 or 302 redirect

and

if related_product field is empty, redirect will be to a default category (in 301 or 302)

else to the product selected (as standard)

i write this code (ProductController Override) but not work, please help

 

class ProductController extends ProductControllerCore
{
    public function initContent()
    {
        if (!$this->product->active) 
        {
            if (Tools::getValue('adtoken') == Tools::getAdminToken('AdminProducts'.(int)Tab::getIdFromClassName('AdminProducts').(int)Tools::getValue('id_employee')) && $this->product->isAssociatedToShop()) {
                $this->context->smarty->assign('adminActionDisplay', true);
            }
            else 
            {
                $this->context->smarty->assign('adminActionDisplay', false);
                if ($this->product->id_product_redirected == $this->product->id) {
                    $this->product->redirect_type = '404';
                }
                switch ($this->product->redirect_type) 
                {
                    case '301':
                        header('HTTP/1.1 301 Moved Permanently');
                        if (!$this->product->id_product_redirected)
                            {header('Location: '.$this->context->link->getCategoryLink($this->product->id_category_default));}
                        else
                            {header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));}
                        exit;
                    break;
                    case '302':
                        header('HTTP/1.1 302 Moved Temporarily');
                        header('Cache-Control: no-cache');
                        if (!$this->product->id_product_redirected)
                            {header('Location: '.$this->context->link->getCategoryLink($this->product->id_category_default));}
                        else
                            {header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));}
                        exit;
                    break;
                    case '404':
                    default:
                        header('HTTP/1.1 404 Not Found');
                        header('Status: 404 Not Found');
                        $this->errors[] = Tools::displayError('This product is no longer available.');
                    break;
                }
            }
        }
    }
}

 

Link to comment
Share on other sites

  • 4 months later...

You do not create the $product object, so you can't verify here if it's active or not. 

 

if ($id_product = (int)Tools::getValue('id_product')) { 
    $this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id); 
}

 

Edited by F3f00cb0 (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...