How to control number of related products

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 лет назад
In NOP1.8 what is the easiest way to control the number of related products displayed?  In our case we would like to, for example, show three related products in order of 'displayorder'.

Thanks,

baaba
13 лет назад
Very easy. Just add three related product to your primary product =))
13 лет назад
That indeed would be easy except that we have more than three related products in general.  It would be really great if we can control this.

Is there a way to make mods to the following to limit number of items returned (i.e. 'top x' number of items)?

var query = from rp in context.RelatedProducts
                        join p in context.Products on rp.ProductId2 equals p.ProductId
                        where rp.ProductId1 == productId1 &&
                        !p.Deleted &&
                        (showHidden || p.Published)
                        orderby rp.DisplayOrder
                        select rp;
            var relatedProducts = query.ToList();
13 лет назад
as andrei said, you can choose as many or as few  related products as you want for each individual product - you do it when you edit the main product

you have to do it manually, how else could the software know which product was related to which other product ?
13 лет назад
I think the point is being missed.

Take this scenario for example:  Each product has 10 related products assigned.  However interested in showing only 3 related products on the main product detail page.  Over time the system admin will change display order of the 10 related products so that new list of 'related products' will be shown, etc.

The point is the system does not seem to offer this flexibility.  Controlling the number of 'related products' list by always ensuring just the right number of 'related products' are assigned is not alway practical nor desirable.  For a moment consider that there is background sql job that does the 'related product' assignment automatically and more often than not will end up with more 'related products' than one would want to show on the site at any given time.


Bazman
13 лет назад
There doesn't seem to be an easy way to do this because the control module RelateProducts.ascx is used throughout the application including the administration side.

You can however duplicate the RelatedProducts.ascx module and corresponding code-behind . Name it RelatedProducts2.ascx or something and then modify the code behind to only take the top 3 records from the RelatedProducts datasource BEFORE it binds it to the control.

In the code-behind, modify the BindData routine as follows:

protected void BindData()
        {
            var product = ProductManager.GetProductById(this.ProductId);
            if (product != null)
            {
                var relatedProducts = product.RelatedProducts.GetRange(0,3);  //Get the first three related products
                if (relatedProducts.Count > 0)
                {
                    this.Visible = true;
                    dlRelatedProducts.DataSource = relatedProducts;
                    dlRelatedProducts.DataBind();
                }
                else
                    this.Visible = false;
            }
            else
                this.Visible = false;
        }

Once you do this, go back to your Product Templates (OneVariant and VariantsInGrid) and make sure you change the control registration to the new "RelatedProducts2.ascx"rather than the old RelatedProducts.ascx.

Rebuild and you should be good to go.

EDIT:  Almost forgot, the code above is from version 1.6.
13 лет назад
Thanks very much.

I can see that this will work just fine.


Regards,


bazman
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.