Comparing products of specific categories

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hello guys,
In my store a got phones and accessories and i would like the comparing module to work only for the phones, because the accessories dont have specifications to be compared with. So is there a way that I can make the module to work only for products in specific categories (in my case only for phones, tablets, smartphones etc...)

Thanks.
11 years ago
scarface90 wrote:
Hello guys,
In my store a got phones and accessories and i would like the comparing module to work only for the phones, because the accessories dont have specifications to be compared with. So is there a way that I can make the module to work only for products in specific categories (in my case only for phones, tablets, smartphones etc...)

Thanks.


Hi,

You can take a look on my solution for this same issue at following post:
https://www.nopcommerce.com/boards/t/31749/category.aspx

Hope this help :)
11 years ago
Thanks, I'm sure that its gonna work perfectly, but the problem is that I'm using the non source platform and folders like Presentation, modules, libraries etc... doesnt exist in my directory. Is there a simple way to add those folders (or upgrade it)
11 years ago
scarface90 wrote:
Thanks, I'm sure that its gonna work perfectly, but the problem is that I'm using the non source platform and folders like Presentation, modules, libraries etc... doesnt exist in my directory. Is there a simple way to add those folders (or upgrade it)


Sure, you can.

- Go to the non source folders of nopCommerce that you deployed.
- And then navigate to the "_CompareProductsButton.cshtml" file.
  (in my case, it is: "C:\inetpub\wwwroot\nopCommerce_3.40_NoSource\Views\Product\_CompareProductsButton.cshtml")
- You have 2 options to modify this view:
  + Modify directly this file
  + Create a new overrided view of this in the "DefaultClean" theme folder (I like this way)
     1) Copy above "_CompareProductsButton.cshtml" file to the "Themes/DefaultClean/Views/Product" folder.
     (in my case, it is: "C:\inetpub\wwwroot\nopCommerce_3.40_NoSource\Themes\DefaultClean\Views\Product")
     2) Modify the copied file.
11 years ago
The modification code of the "_CompareProductsButton.cshtml" view:
(replace entire "_CompareProductsButton.cshtml" code with following code)

@using Nop.Core.Infrastructure;
@using Nop.Services.Catalog;
@using Nop.Web.Models.Catalog;
@model ProductDetailsModel
@functions {
    public static bool HasSameCategory(int productId)
    {
        var productService = EngineContext.Current.Resolve<IProductService>();
        var compareProductsService = EngineContext.Current.Resolve<ICompareProductsService>();

        var oldProducts = compareProductsService.GetComparedProducts();
        var newProduct = productService.GetProductById(productId);
        var hasSameCategory = !oldProducts.Any();

        foreach (var oldProduct in oldProducts)
        {
            if (oldProduct.ProductCategories.Any(x => newProduct.ProductCategories.Any(y => x.CategoryId.Equals(y.CategoryId))))
            {
                hasSameCategory = true;

                break;
            }
        }

        return hasSameCategory;
    }
}
@if (Model.CompareProductsEnabled)
{
    <div class="compare-products">
        <input id="[email protected]" type="button" value="@T("Products.Compare.AddToCompareList")"
               class="button-2 add-to-compare-list-button" />
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#[email protected]').on('click', function () {
                var hasSameCategory = '@HasSameCategory(Model.Id)';
                if (hasSameCategory === 'True') {
                    setLocation('@Url.RouteUrl("AddProductToCompare", new { productId = Model.Id })');
                } else {
                    alert('These two products can not be compared');
                }
            });
        });
    </script>
}

Hope this help :)
11 years ago
Thanks ima9ines,
The solution you provide works perfect but if you can make it to show a popup message (not an alert messege but like the one that says "the product is added to the wishlist successfully") instead of redirecting you right away to compare list. I have added the compare list to the header links, so thats why I need this kind of message. If you manage to do that, they should put your code in the installation package ;) Thanks again!
11 years ago
scarface90 wrote:
Thanks ima9ines,
The solution you provide works perfect but if you can make it to show a popup message (not an alert messege but like the one that says "the product is added to the wishlist successfully") instead of redirecting you right away to compare list. I have added the compare list to the header links, so thats why I need this kind of message. If you manage to do that, they should put your code in the installation package ;) Thanks again!


Hi,

Update entire "_CompareProductsButton.cshtml" file with following code:

@using Nop.Core.Infrastructure;
@using Nop.Services.Catalog;
@using Nop.Web.Models.Catalog;
@model ProductDetailsModel
@functions {
    public static bool HasSameCategory(int productId)
    {
        var productService = EngineContext.Current.Resolve<IProductService>();
        var compareProductsService = EngineContext.Current.Resolve<ICompareProductsService>();

        var oldProducts = compareProductsService.GetComparedProducts();
        var newProduct = productService.GetProductById(productId);
        var hasSameCategory = !oldProducts.Any();

        foreach (var oldProduct in oldProducts)
        {
            if (oldProduct.ProductCategories.Any(x => newProduct.ProductCategories.Any(y => x.CategoryId.Equals(y.CategoryId))))
            {
                hasSameCategory = true;

                break;
            }
        }

        return hasSameCategory;
    }
}
@if (Model.CompareProductsEnabled)
{
    <div class="compare-products">
        <input id="[email protected]" type="button" value="@T("Products.Compare.AddToCompareList")"
               class="button-2 add-to-compare-list-button" />
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#[email protected]').on('click', function () {
                var hasSameCategory = '@HasSameCategory(Model.Id)';
                if (hasSameCategory === 'True') {
                    addProductToCompare('@Url.RouteUrl("AddProductToCompare", new { productId = Model.Id })');
                } else {
                    displayBarNotification('These two products can not be compared', 'error', 0);
                }
            });

            //add a product to the compare products from the product details page
            function addProductToCompare(urlAdd) {
                if (AjaxCart.loadWaiting != false) {
                    return;
                }

                AjaxCart.setLoadWaiting(true);

                $.ajax({
                    cache: false,
                    url: urlAdd,
                    type: 'post',
                    success: function (response) {
                        if ($('.page.compare-products-page', response).length > 0) {
                            displayBarNotification('Product added to compare list', 'success', 3500);
                        }
                    },
                    complete: AjaxCart.resetLoadWaiting,
                    error: AjaxCart.ajaxFailure
                });
            }
        });
    </script>
}

Hope this help :)
11 years ago
Thanks mate.
Cant describe how much happy I am. This should be included in the next update!
Cheers.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.