Developer forum

Forum » Development » Redirect for inactive product?

Redirect for inactive product?

Anders Ebdrup
Anders Ebdrup
Reply

Hello,

 

Does anyone know how to make a redirect for an inactive product to another product? I cannot fiture out which notifier to use in this occasion...

 

Best regards, Anders


Replies

 
Nicolai Pedersen
Reply

You probably want to use Notifications.Ecommerce.Product.BeforeRender and check if it is a product list or product detail page that is being rendered (If ?ProductID=XX)

You can then make a field on the product to choose which product should be redirect to.

BR Nicolai

 
Anders Ebdrup
Anders Ebdrup
Reply

Hi Nicolai,

 

This notifier is not hit when a product is deactivated. Do you have any other hints? :-)

 

Best regards, Anders

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Would Dynamicweb.Notifications.Ecommerce.Product.AfterSave be of use? You can see if the product us deactivated there and create a redirect.

 
Nicolai Pedersen
Reply

@Anders: Because you use 404 on deactivated products? Then you need to go with Imars suggestion.... Or we need to add a BeforeBeforeRender notification - or a product not found notification with a cause...

BR Nicolai

 
Anders Ebdrup
Anders Ebdrup
Reply

Hi Nicolai & Imar,

 

For some products I need to have 404, and for the deactivated product it needs to be controlled runtime as it can vary as it depends on the active assortment...

@Nicolai: yes, we have activated 404 of deactivted products. Should we twist this and do the redirect our self for inactive products?

 

Best regards, Anders

 
Anders Ebdrup
Anders Ebdrup
Reply

Any thoughts on how to solve this one? :-)

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Ander

You have to disable the DontAllowLinksToProductIfNotActive setting and make the decision your self using the BeforeRender notification. If you have an alternative product, do the redirect, otherwise repsonse 404

BR Nicolai

Votes for this answer: 1
 
Tomas Gomez
Reply

Hi,

we tried the BeforeRender notification without success.

Finally, we did it following these steps:

  1. Disable the DontAllowLinksToProductIfNotActive setting
  2. Create a new custom field for products ("InactiveLink"). This way, when a product is inactive, the page will be redirected to this link, or to a 404 if this link is not defined.
  3. Modificate the Product.cshtml template. This is the code we inserted:

@{
   if(!@GetBoolean("Ecom:Product.IsActive")  ) {
       string altProduct = "";
       if (Loops.Contains("CustomFields")) {
           foreach (LoopItem item in GetLoop("CustomFields")) {
              altProduct = @item.GetString("InactiveLink.Value");
           }
       }
       if(!string.IsNullOrEmpty(altProduct)) {
          HttpContext.Current.Response.Redirect("~/default.aspx?id="+GetGlobalValue("Global:Page.ID")+"&productid="+altProduct);
       }
       else{
           HttpContext.Current.Response.Redirect("~/product-not-found");
       }       
   }
}

Regards,
Tom

 

You must be logged in to post in the forum