2.2. searching products in sub-categories recursive view in stored procedure

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hello,

Contrary to this topic (https://www.nopcommerce.com/boards/t/12256/showing-products-from-sub-categories.aspx) I want not show all products from all categories when parent category is selected but in advanced search, when I select a category, I need to search in all child categories.

To do that, I modified the stored procedure [ProductLoadAllPaged] as below :

...
  CREATE TABLE #DisplayOrderTmp
  (
    [Id] int IDENTITY (1, 1) NOT NULL,
    [ProductId] int NOT NULL
  )

--
; With Cte_Cat_Hie As (
  SELECT ID
    FROM dbo.Category
    WHERE ID = Case When @CategoryId = 0 Then ID Else @CategoryId End
    UNION ALL
    SELECT Child.ID
    FROM Category AS Child INNER JOIN Cte_Cat_Hie AS Parent
        ON Child.ParentCategoryId = Parent.Id
)


--  
  INSERT INTO #DisplayOrderTmp ([ProductId])
  SELECT p.Id
  FROM Product p with (NOLOCK)
  LEFT OUTER JOIN Product_Category_Mapping pcm with (NOLOCK) ON p.Id=pcm.ProductId
  INNER JOIN Cte_Cat_Hie On Cte_Cat_Hie.Id = pcm.CategoryId
  LEFT OUTER JOIN Product_Manufacturer_Mapping pmm with (NOLOCK) ON p.Id=pmm.ProductId
  LEFT OUTER JOIN Product_ProductTag_Mapping pptm with (NOLOCK) ON p.Id=pptm.Product_Id
  LEFT OUTER JOIN ProductVariant pv with (NOLOCK) ON p.Id = pv.ProductId
  --searching of the localized values
  --comment the line below if you don't use it. It'll improve the performance
  LEFT OUTER JOIN LocalizedProperty lp with (NOLOCK) ON p.Id = lp.EntityId AND lp.LanguageId = @LanguageId AND lp.LocaleKeyGroup = N'Product'
  WHERE
    (
       (
--        @CategoryId IS NULL OR @CategoryId=0
--        OR (pcm.CategoryId =@CategoryId AND (@FeaturedProducts IS NULL OR pcm.IsFeaturedProduct=@FeaturedProducts))
--      )
--    AND (

        @ManufacturerId IS NULL OR @ManufacturerId=0
...


Hope this helps someone.
12 years ago
Thats exactly i was looking for , One more thing , I have got a keyword column in my products table , keyword column will have only 5-6 keywords for instance red , green , white , purple , black. Each product has a keyword. I was thinking of including a key word dropdownlist in the advance search so , that when black keyword is selected all the products which has a keyword black should be displayed, Is it possible to achieve something like this , if yes any assistance ??
12 years ago
BadFog wrote:
Hello,

Contrary to this topic (https://www.nopcommerce.com/boards/t/12256/showing-products-from-sub-categories.aspx) I want not show all products from all categories when parent category is selected but in advanced search, when I select a category, I need to search in all child categories.

To do that, I modified the stored procedure [ProductLoadAllPaged] as below :

...
  CREATE TABLE #DisplayOrderTmp
  (
    [Id] int IDENTITY (1, 1) NOT NULL,
    [ProductId] int NOT NULL
  )

--
; With Cte_Cat_Hie As (
  SELECT ID
    FROM dbo.Category
    WHERE ID = Case When @CategoryId = 0 Then ID Else @CategoryId End
    UNION ALL
    SELECT Child.ID
    FROM Category AS Child INNER JOIN Cte_Cat_Hie AS Parent
        ON Child.ParentCategoryId = Parent.Id
)


--  
  INSERT INTO #DisplayOrderTmp ([ProductId])
  SELECT p.Id
  FROM Product p with (NOLOCK)
  LEFT OUTER JOIN Product_Category_Mapping pcm with (NOLOCK) ON p.Id=pcm.ProductId
  INNER JOIN Cte_Cat_Hie On Cte_Cat_Hie.Id = pcm.CategoryId
  LEFT OUTER JOIN Product_Manufacturer_Mapping pmm with (NOLOCK) ON p.Id=pmm.ProductId
  LEFT OUTER JOIN Product_ProductTag_Mapping pptm with (NOLOCK) ON p.Id=pptm.Product_Id
  LEFT OUTER JOIN ProductVariant pv with (NOLOCK) ON p.Id = pv.ProductId
  --searching of the localized values
  --comment the line below if you don't use it. It'll improve the performance
  LEFT OUTER JOIN LocalizedProperty lp with (NOLOCK) ON p.Id = lp.EntityId AND lp.LanguageId = @LanguageId AND lp.LocaleKeyGroup = N'Product'
  WHERE
    (
       (
--        @CategoryId IS NULL OR @CategoryId=0
--        OR (pcm.CategoryId =@CategoryId AND (@FeaturedProducts IS NULL OR pcm.IsFeaturedProduct=@FeaturedProducts))
--      )
--    AND (

        @ManufacturerId IS NULL OR @ManufacturerId=0
...


Hope this helps someone.


It works fine for the search functionality but the procedure is used throughout the website , that's why it breaks when you have subcategories within categories.
12 years ago
Thanks a lot. Please see changeset ea1bde4dc483
12 years ago
Quick thought about impact of this changeset on performance ..

I think many NopCommerce users (including me) are in the group who want to see products from category and all of it's subcategories when they select a category. In other words when (using a bookstore example) when clicking on "children books" category user is expecting to see all children books including books from "books for infants", "books for schoolchildren", "books for teens" etc. Categories often serve as a kind of filters.

What I am suggesting is making a ShowProductsFromSubcategoriesInCatalog which would cause GetChildCategoryIds() method to be called to pass all subcategories to the the [ProductLoadAllPaged] procedure whevenver catalog controller is used to show contents of a category.

Currently to get Nop to show products from subcategories one has to manually (or via recursive PL/SQL) assign products from subcategories to parent categories. This causes my [Product_Category_Mapping] to increase few times in size to current 700.000 records. It would have increased more but because of performance concerns I had to flatten my category tree to 3 levels max..

Bottom line - Andrei - there is a quick win to be had here with 10 minutes of coding :-)

Filip
12 years ago
I've created a workitem for it here:

http://nopcommerce.codeplex.com/workitem/10594

.. in case someone want's to vote :-)

Filip
12 years ago
Done. Please see changeset 9dcb32dc04f6
12 years ago
I've upated my store to use it. It's a 100.000+ product / 260.000 product-to-category mappings set of products.

See it in action here:

http://ksiegarniainternetowa.de/c/213/historia-rosji

Suggestion:
I think this feature could benefit from GetChildCategoryIds() caching results in object cache..


Conclusion:
I think it works a bit faster than it did when I manually added all the products from subcategories to parent category. The P2C table droped now from 800.000 P2C mappings down to 260.000 mappings. Excellent!

Filip
12 years ago
fkierzek wrote:
I think this feature could benefit from GetChildCategoryIds() caching results in object cache..

Thanks. A work item is already created
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.