Jump to content

Ps 1.5.3.1 - How Do I Redirect Disabled Product To Category


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. But I can't seem to find any post or anyone who's done this before? Is it possible to redirect to a url or even just another category? If you have done this before please share some of your ideas?

post-341448-0-10184900-1360889907_thumb.png

Link to comment
Share on other sites

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. But I can't seem to find any post or anyone who's done this before? Is it possible to redirect to a url or even just another category? If you have done this before please share some of your ideas?

You can do this by adding the line

redirectMatch 301 ^/link-to-your-product.html$ http://yourdomain/any-link

into your .htaccess file for example after:

RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]

Edited by doubleD (see edit history)
  • Like 1
Link to comment
Share on other sites

You can do this by adding the line

redirectMatch 301 ^/link-to-your-product.html$ http://yourdomain/any-link

into your .htaccess file for example after:

RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]

 

Yes that works fine however didn't want to be messing with httaccess files I can do it but other people using back office will have no clue. Would be good if there's a way to fix the backoffice. In that attached image before you can use 301 redirect but it keeps doing the instant search for matching products if non products found it will make the field empty thus preventing you from redirecting to a link.

 

Is there a way to edit the controller to allow redirecting to any link from the back office?

Link to comment
Share on other sites

Is there a way to edit the controller to allow redirecting to any link from the back office?

I'm pretty sure there is )

But i think, it won't be a few lines of code

It's not just a controller:

controllers/admin/AdminProductsController.php

admin/ajax_products_list.php

admin/themes/default/template/controllers/products/informations.tpl

js/admin-products.js

maybe more...

Link to comment
Share on other sites

I've got one more a bit crazy :rolleyes: idea how to accomplish this, but still htaccess modification will be required.

Create a product(s) with whatever name you want(Category name for ex.) and set Visibility to None.

Then modify htaccess and redirect newly created product to the corresponding category.

After that, users/employees cat find product with category name, and it will redirect there...

:)

Link to comment
Share on other sites

I've got one more a bit crazy :rolleyes: idea how to accomplish this, but still htaccess modification will be required.

Create a product(s) with whatever name you want(Category name for ex.) and set Visibility to None.

Then modify htaccess and redirect newly created product to the corresponding category.

After that, users/employees cat find product with category name, and it will redirect there...

:)

 

Nice, but I think it would be best to do actual instant search for category like for product, if that could be fixed with presta development team great if not this is a good module for someone to create :rolleyes:

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

There is something in the core. There is two variables: id_product_redirected and redirect_type that allows you to redirect a disabled product to another one. Everything is coded for it to work properlly at the frontend but there is nothing at the backend which is quite sad.

id_product_redirected takes the new product to redirect to and takes the redirection type (404 for not found, 301 for permanent or 302 for temporarily).

 

Looks like it's an unfinished feature.

 

Edit : in fact it's working but you need to disable the product by its info page and not the category view. When you select disabled, there is dropdown that appears and you can select the redirection type and the destination product.

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

  • 2 years later...
  • 5 months later...

I'm sorry for resurrecting an old thread, but I have a solution to the mentioned problem, which may save some time for somebody else.

 

It's tested on Prestashop 1.6.1.5, but I think it will be compatible at least with the rest of 1.6 versions.

 

Inside the header.tpl file, add the following code - it will redirect all disabled products to the product's category page.

    {if $page_name == 'product'}
      {if $product->active != 1}
        {foreach from=Product::getProductCategoriesFull(Tools::getValue('id_product')) item=cat name=cats}
          {if $smarty.foreach.cats.last}
            {assign var="cat_url" value="{$link->getCategoryLink({$cat.id_category})}"}
          {/if}    
        {/foreach}
        {php}            
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: {$cat_url}");    
        {/php}  
      {/if}
    {/if}

Note:

You also have to enable the {php}{/php} tag support, which has been disabled in some prestashop versions:

 

File config/smarty.config.inc.php

Change the following:

define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');

require_once(_PS_SMARTY_DIR_.'Smarty.class.php');

global $smarty;

$smarty = new SmartyCustom();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');

to

define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');

require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');

global $smarty;
$smarty = new SmartyBC();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');

I hope this will help.

 

Link to comment
Share on other sites

hi cceloo

tried this tweak. it works but redirects to the homepage. 

check it 

this is a disabled product

http://www.kathmanduclothing.com/woolen-gloves/33-fingerless-cum-cover-mittens.html

 

but i also another product and it redirects to category page

http://www.kathmanduclothing.com/hippie-jackets/173-ssd-703-.html

 

 

Anyways thanks for the great help.

Link to comment
Share on other sites

Hi dorje!

 

Try to replace these lines:

{if $product->active != 1}
        {foreach from=Product::getProductCategoriesFull(Tools::getValue('id_product')) item=cat name=cats}
          {if $smarty.foreach.cats.last}
            {assign var="cat_url" value="{$link->getCategoryLink({$cat.id_category})}"}
          {/if}    
        {/foreach}
        {php}            
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: {$cat_url}");    
        {/php}  
      {/if}

with something like this:

{if $product->active != 1}
        {foreach from=Product::getProductCategoriesFull(Tools::getValue('id_product')) item=cat name=cats}
            {assign var="cat_url" value="{$link->getCategoryLink({$cat.id_category})}"}
             <meta name="cat_id" content="{$cat.id_category}" /> 
        {/foreach}
      {/if}

Then, there should be no redirect, but in the <head></head> section of your page, you should see meta tags with category ids. The last meta tag with cat_id would contain the id of category, to which the page would be redirected. I hope this will help you debug the problem yourself.

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

  • 1 month later...

Nice trick but consider to not use {php} smarty tag. So instead of this use:

{if $page_name == 'product'}
  {if $product->active != 1}
    {foreach from=Product::getProductCategoriesFull(Tools::getValue('id_product')) item=cat name=cats}
      {if $smarty.foreach.cats.last}
        {assign var="cat_url" value="{$link->getCategoryLink({$cat.id_category})}"}
      {/if}    
    {/foreach}
    <meta http-equiv="refresh" content="0; url={$cat_url}" />
   {/if}
{/if}
Link to comment
Share on other sites

I'm agree it is nice solution, but you lose the redirection function available in the product tab,

I made an override of productController.php, by changing init() function on ps 1.6.1.5

work like this :

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)

 

ciao

 

devi decorare una festa? visita: http://www.wimipops.com

           if (!$this->product->isAssociatedToShop() || !$this->product->active) {
                if (Tools::getValue('adtoken') == Tools::getAdminToken('AdminProducts'.(int)Tab::getIdFromClassName('AdminProducts').(int)Tools::getValue('id_employee')) && $this->product->isAssociatedToShop()) {
                    // If the product is not active, it's the admin preview mode
                    $this->context->smarty->assign('adminActionDisplay', true);
                } else {
                    $this->context->smarty->assign('adminActionDisplay', false);
//                    if (!$this->product->id_product_redirected || $this->product->id_product_redirected == $this->product->id) {
//					sd+ avoid original to 404 if id_product_redirected is empty
                    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');
                            //header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));
                            // sd+ if id_product_redirected is empty (0) redirect to default category of the product
                            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');
                            //header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));
                            // sd+ if id_product_redirected is empty (0) redirect to default category of the product
                            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;
                    }
                }
  • Like 1
Link to comment
Share on other sites

 

 

I'm agree it is nice solution, but you lose the redirection function available in the product tab,

 

Hi sd+,

 

I don't know if you were saying that for Redirection Manager but in case :

The module obviously check first if a redirection had been made in product tab ;)

Link to comment
Share on other sites

  • 3 months later...

 

I'm agree it is nice solution, but you lose the redirection function available in the product tab,

I made an override of productController.php, by changing init() function on ps 1.6.1.5

work like this :

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)

 

ciao

 

devi decorare una festa? visita: http://www.wimipops.com

           if (!$this->product->isAssociatedToShop() || !$this->product->active) {
                if (Tools::getValue('adtoken') == Tools::getAdminToken('AdminProducts'.(int)Tab::getIdFromClassName('AdminProducts').(int)Tools::getValue('id_employee')) && $this->product->isAssociatedToShop()) {
                    // If the product is not active, it's the admin preview mode
                    $this->context->smarty->assign('adminActionDisplay', true);
                } else {
                    $this->context->smarty->assign('adminActionDisplay', false);
//                    if (!$this->product->id_product_redirected || $this->product->id_product_redirected == $this->product->id) {
//					sd+ avoid original to 404 if id_product_redirected is empty
                    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');
                            //header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));
                            // sd+ if id_product_redirected is empty (0) redirect to default category of the product
                            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');
                            //header('Location: '.$this->context->link->getProductLink($this->product->id_product_redirected));
                            // sd+ if id_product_redirected is empty (0) redirect to default category of the product
                            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;
                    }
                }

Hi Sd+,

where do I have to write this code? Do you think It can works with 1.6.0.9?

Thanks a lot

Stefano

Link to comment
Share on other sites

×
×
  • Create New...