Developer forum

Forum » Development » BeforeRenderArgs and product list

BeforeRenderArgs and product list

Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi,

When a customer in Ecom makes a product search, I would like to intercept it.

If no result is given in the current productgroup, I would like to make a search on all groups.

To achieve this, I'm trying to hook into the NotificationSubscriber, to get a notification before the productlist is rendered to the frontend.
I'm doing this with the following code:

    [Subscribe(eCommerce.ProductList.BeforeRender)]
    public class ProductListBeforeRenderSubscriber : NotificationSubscriber
    {
        /// <summary>
        /// After output on page
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="args"></param>
        public override void OnNotify(string notification, NotificationArgs args)
        {
            eCommerce.ProductList.BeforeRenderArgs BeforeQuerArgs = args as eCommerce.ProductList.BeforeRenderArgs;
            if(BeforeQuerArgs != null)
            {
                if (BeforeQuerArgs.Products != null)
                {

My problem is, that if I check the BeforeQuerArgs.Products.Count, it is always empty, even though results have been found. If I iterate through the loop with the following code, the counter is incremented as it should for each product found:

foreach (var item in BeforeQuerArgs.Products)
{
countProducts += 1;
}

Is this the wrong approach to achieve this or am I missing something?


Best regards
Kurt Moskjaer Andersen


Replies

 
Nicolai Pedersen
Reply

Hi Kurt

The collection is a Collection(Of T), see https://msdn.microsoft.com/en-us/library/ms132397(v=vs.110).aspx so using .Count should work. can you post your full code that is not working?

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

using Dynamicweb.Notifications;
using Dynamicweb.Extensibility;
using Dynamicweb.eCommerce.Products;
using System;
using System.Web;

namespace VestjyskMarketing.Subscribers
{
    [Subscribe(eCommerce.ProductList.BeforeRender)]
    public class ProductListBeforeRenderSubscriber : NotificationSubscriber
    {
        /// <summary>
        /// After output on page
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="args"></param>
        public override void OnNotify(string notification, NotificationArgs args)
        {
            eCommerce.ProductList.BeforeRenderArgs BeforeQuerArgs = args as eCommerce.ProductList.BeforeRenderArgs;
            if(BeforeQuerArgs != null)
            {
                if (BeforeQuerArgs.Products != null)
                {
                    if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["qq"]))
                    {
                        int countProducts = BeforeQuerArgs.Products.Count;
                        string qq = "";

                        foreach (var item in BeforeQuerArgs.Products)
                        {
                            countProducts += 1;
                        }
                        
                        qq = HttpContext.Current.Request.QueryString["qq"];

                        if (countProducts == 0)
                        {
                            var product = Product.GetProductByNumber(qq);

                            if (product != null)
                            {
                                //redirect back
                                string url = "~/Webshop/" + product.DefaultGroup.Name + ".aspx";
                                HttpContext.Current.Response.Redirect(url, true);
                            }
                        }
                    }
                }
            }
            base.OnNotify(notification, args);
        }
    }
}

 

Please notice, that the foreach-loop on the products is only used because the count returned zero.

 
Nicolai Pedersen
Reply

This is with your 'fix'. Need to see the code where .Count does not work.

BR Nicolai

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Sorry, my mistake. The problem is, that it is always entering the condition with "countProducts==0", even though a result is found.

/Kurt

--

using Dynamicweb.Notifications;
using Dynamicweb.Extensibility;
using Dynamicweb.eCommerce.Products;
using System;
using System.Web;

namespace VestjyskMarketing.Subscribers
{
    [Subscribe(eCommerce.ProductList.BeforeRender)]
    public class ProductListBeforeRenderSubscriber : NotificationSubscriber
    {
        /// <summary>
        /// After output on page
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="args"></param>
        public override void OnNotify(string notification, NotificationArgs args)
        {
            eCommerce.ProductList.BeforeRenderArgs BeforeQuerArgs = args as eCommerce.ProductList.BeforeRenderArgs;
            if(BeforeQuerArgs != null)
            {
                if (BeforeQuerArgs.Products != null)
                {
                    if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["qq"]))
                    {
                        int countProducts = BeforeQuerArgs.Products.Count;
                        string qq = "";                       
                        qq = HttpContext.Current.Request.QueryString["qq"];

                        if (countProducts == 0)
                        {
                            var product = Product.GetProductByNumber(qq);

                            if (product != null)
                            {
                                //redirect back
                                string url = "~/Webshop/" + product.DefaultGroup.Name + ".aspx";
                                HttpContext.Current.Response.Redirect(url, true);
                            }
                        }
                    }
                }
            }
            base.OnNotify(notification, args);
        }
    }
}

 
Nicolai Pedersen
Reply
This post has been marked as an answer

ok, maybe the collection is null - and thus resulting in an exception when accessing the .Count property. So try adding a debugger or add a check on BeforeQuerArgs.Products is null

BR Nicolai

Votes for this answer: 1
 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Well, I'll try. Did some debugging, but without finding the reason.

Is this the best way of doing it, by subscribing to the eCommerce.ProductList.BeforeRender ?

/Kurt

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Kurt

I do not know what makes sense in your use case - this could be the way to do it - a more pragmatic way could be to do a JS redirect when there is no search results to a search with different parameters. Personally I prefer that searches show what I search for - and not all other kinds of things. Like Google - giving me random results without my search terms :-).

BR Nicolai

Votes for this answer: 1
 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi Nicolai,

Thank you for your feedback. It's much appreciated :)

/Kurt

 

You must be logged in to post in the forum