mehdi.7618 Posted September 14, 2020 Share Posted September 14, 2020 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 More sharing options...
F3f00cb0 Posted February 3, 2021 Share Posted February 3, 2021 (edited) 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 February 3, 2021 by F3f00cb0 (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now