Page 1 of 1

You took the ProductTemplateId out of the Products table !!!

Posted: Sun Jan 10, 2010 1:01 pm
by Mike718NY
I was hit with a shock when I (and my code) discovered the
ProductTemplateId was removed from the Products table !!!

In the ProductHelper.cs file I had:

if (product.ProductTemplateId == 5)
..........

But now I don't know how to access the ProductTemplateId of 5?

Can it be accessed through another field?
I know this is not right, but can one of these other fields be used instead:

if (product.TemplateFields == 5) ???

or

if (product.ProductProductTemplates == 5) ???

Re: You took the ProductTemplateId out of the Products table !!!

Posted: Sun Jan 10, 2010 1:17 pm
by jmestep
In 7.0.4, you can have more than one template apply to a product, so you need to get the collection, then select the one you want. I just had to do it on a site I upgraded from 7.0.3. The following will work if you want the first template id or if there is only one

Code: Select all

   private static int GetTemplateId(Product product)
    {
        int templateId = 0;
        ProductTemplateCollection templates = ProductTemplateDataSource.LoadForProduct(product.ProductId);
        if (templates != null)
        {
            //get from only one template for now

            templateId = product.ProductProductTemplates[0].ProductTemplateId;
        }
        return templateId;
    }

Re: You took the ProductTemplateId out of the Products table !!!

Posted: Mon Jan 11, 2010 10:21 am
by Mike718NY
thanks Judy, you saved the day again.

This works also (I used it in CategoryGridPage2.ascx.cs):

if (product.ProductProductTemplates[0].ProductTemplateId == 5)
...

but it only works if all the products for that Category have a Template defined for those
products, otherwise it gives the error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I'll have to check for null or something before using it.