The 5th annual nopCommerce conference will take place in India on the 10th-11th of October. Please find more about this event here

Manufacturer Picture instead of default picture

Posted: September 09, 2019 at 3:58 AM Quote #245978
Hello to everyone.

I am working on Nopcommerce 4.20 source codes.
What I'm trying to do.
the first picture of all products is the manufacturer's picture.
instead of the default image is also the manufacturer image.
can you help me?
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: September 09, 2019 at 4:27 AM Quote #245982
Change your code in ProductModelFactory.PrepareProductOverviewPictureModel as below.

protected virtual PictureModel PrepareProductOverviewPictureModel(Product product, int? productThumbPictureSize = null)
{
    if (product == null)
        throw new ArgumentNullException(nameof(product));

    var productName = _localizationService.GetLocalized(product, x => x.Name);
    //If a size has been set in the view, we use it in priority
    var pictureSize = productThumbPictureSize ?? _mediaSettings.ProductThumbPictureSize;

    //prepare picture model
    var cacheKey = string.Format(NopModelCacheDefaults.ProductDefaultPictureModelKey,
        product.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(),
        _storeContext.CurrentStore.Id);

    var defaultPictureModel = _cacheManager.Get(cacheKey, () =>
    {
        var picture = _pictureService.GetPicturesByProductId(product.Id, 1).FirstOrDefault();
        if (picture == null && product.ProductManufacturers.Any())
        {
            var manufacturer = product.ProductManufacturers.FirstOrDefault();
            picture = _pictureService.GetPictureById(manufacturer.ProductId);
        }


        var pictureModel = new PictureModel
        {
            ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize),
            FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
            //"title" attribute
            Title = (picture != null && !string.IsNullOrEmpty(picture.TitleAttribute))
                ? picture.TitleAttribute
                : string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"),
                    productName),
            //"alt" attribute
            AlternateText = (picture != null && !string.IsNullOrEmpty(picture.AltAttribute))
                ? picture.AltAttribute
                : string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"),
                    productName)
        };

        return pictureModel;
    });

    return defaultPictureModel;
}


Change as well as in PrepareProductDetailsPictureModel method
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Jaber Kibria
Software Engineer | nop-station.com
Email: [email protected]
----------------------------------------------------------------
Expertise in .NET Framework, Algolia, Plugins, Themes, Customization, Upgrade, API

Please upvote if it helps.
Posted: September 09, 2019 at 7:00 AM Quote #245996
did not work.
manufacturer started to produce different pictures instead of pictures, but these are pictures of other products
Thank you for your help.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: September 10, 2019 at 1:24 AM Quote #246032
cemalal wrote:
did not work.
manufacturer started to produce different pictures instead of pictures, but these are pictures of other products
Thank you for your help.


you are right. Small mistake was there. Try below code.

protected virtual PictureModel PrepareProductOverviewPictureModel(Product product, int? productThumbPictureSize = null)
{
    if (product == null)
        throw new ArgumentNullException(nameof(product));

    var productName = _localizationService.GetLocalized(product, x => x.Name);
    //If a size has been set in the view, we use it in priority
    var pictureSize = productThumbPictureSize ?? _mediaSettings.ProductThumbPictureSize;

    //prepare picture model
    var cacheKey = string.Format(NopModelCacheDefaults.ProductDefaultPictureModelKey,
        product.Id, pictureSize, true, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(),
        _storeContext.CurrentStore.Id);

    var defaultPictureModel = _cacheManager.Get(cacheKey, () =>
    {
        var picture = _pictureService.GetPicturesByProductId(product.Id, 1).FirstOrDefault();
        if (picture == null && product.ProductManufacturers.Any())
        {
            var pm = product.ProductManufacturers.FirstOrDefault();
            picture = _pictureService.GetPictureById(pm.Manufacturer.PictureId);
        }
        var pictureModel = new PictureModel
        {
            ImageUrl = _pictureService.GetPictureUrl(picture, pictureSize),
            FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
            //"title" attribute
            Title = (picture != null && !string.IsNullOrEmpty(picture.TitleAttribute))
                ? picture.TitleAttribute
                : string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat"),
                    productName),
            //"alt" attribute
            AlternateText = (picture != null && !string.IsNullOrEmpty(picture.AltAttribute))
                ? picture.AltAttribute
                : string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat"),
                    productName)
        };

        return pictureModel;
    });

    return defaultPictureModel;
}
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Jaber Kibria
Software Engineer | nop-station.com
Email: [email protected]
----------------------------------------------------------------
Expertise in .NET Framework, Algolia, Plugins, Themes, Customization, Upgrade, API

Please upvote if it helps.
Posted: September 10, 2019 at 3:32 AM Quote #246037
Thank you for the quick reply.

Worked Great.
Finally.

On the product details page, I wanted the manufacturer image to appear instead of the default image.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: September 10, 2019 at 4:28 AM Quote #246042
cemalal wrote:
On the product details page, I wanted the manufacturer image to appear instead of the default image.


Here is the updated PrepareProductDetailsPictureModel method.


protected virtual PictureModel PrepareProductDetailsPictureModel(Product product, bool isAssociatedProduct, out IList<PictureModel> allPictureModels)
{
    if (product == null)
        throw new ArgumentNullException(nameof(product));

    //default picture size
    var defaultPictureSize = isAssociatedProduct ?
        _mediaSettings.AssociatedProductPictureSize :
        _mediaSettings.ProductDetailsPictureSize;

    //prepare picture models
    var productPicturesCacheKey = string.Format(NopModelCacheDefaults.ProductDetailsPicturesModelKey, product.Id, defaultPictureSize, isAssociatedProduct, _workContext.WorkingLanguage.Id, _webHelper.IsCurrentConnectionSecured(), _storeContext.CurrentStore.Id);
    var cachedPictures = _cacheManager.Get(productPicturesCacheKey, () =>
    {
        var productName = _localizationService.GetLocalized(product, x => x.Name);

        var pictures = _pictureService.GetPicturesByProductId(product.Id);
        var defaultPicture = pictures.FirstOrDefault();
        if (defaultPicture == null && product.ProductManufacturers.Any())
        {
            var pm = product.ProductManufacturers.FirstOrDefault();
            defaultPicture = _pictureService.GetPictureById(pm.Manufacturer.PictureId);
        }
        var defaultPictureModel = new PictureModel
        {
            ImageUrl = _pictureService.GetPictureUrl(defaultPicture, defaultPictureSize, !isAssociatedProduct),
            FullSizeImageUrl = _pictureService.GetPictureUrl(defaultPicture, 0, !isAssociatedProduct)
        };
        //"title" attribute
        defaultPictureModel.Title = (defaultPicture != null && !string.IsNullOrEmpty(defaultPicture.TitleAttribute)) ?
            defaultPicture.TitleAttribute :
            string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"), productName);
        //"alt" attribute
        defaultPictureModel.AlternateText = (defaultPicture != null && !string.IsNullOrEmpty(defaultPicture.AltAttribute)) ?
            defaultPicture.AltAttribute :
            string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"), productName);

        //all pictures
        var pictureModels = new List<PictureModel>();
        foreach (var picture in pictures)
        {
            var pictureModel = new PictureModel
            {
                ImageUrl = _pictureService.GetPictureUrl(picture, defaultPictureSize, !isAssociatedProduct),
                ThumbImageUrl = _pictureService.GetPictureUrl(picture, _mediaSettings.ProductThumbPictureSizeOnProductDetailsPage),
                FullSizeImageUrl = _pictureService.GetPictureUrl(picture),
                Title = string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"), productName),
                AlternateText = string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"), productName),
            };
            //"title" attribute
            pictureModel.Title = !string.IsNullOrEmpty(picture.TitleAttribute) ?
                picture.TitleAttribute :
                string.Format(_localizationService.GetResource("Media.Product.ImageLinkTitleFormat.Details"), productName);
            //"alt" attribute
            pictureModel.AlternateText = !string.IsNullOrEmpty(picture.AltAttribute) ?
                picture.AltAttribute :
                string.Format(_localizationService.GetResource("Media.Product.ImageAlternateTextFormat.Details"), productName);

            pictureModels.Add(pictureModel);
        }

        return new { DefaultPictureModel = defaultPictureModel, PictureModels = pictureModels };
    });

    allPictureModels = cachedPictures.PictureModels;
    return cachedPictures.DefaultPictureModel;
}
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Jaber Kibria
Software Engineer | nop-station.com
Email: [email protected]
----------------------------------------------------------------
Expertise in .NET Framework, Algolia, Plugins, Themes, Customization, Upgrade, API

Please upvote if it helps.
Posted: September 10, 2019 at 4:36 AM Quote #246043
Very good
Thanks for your help.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.
eCommerce CONFERENCE 2019
Learn more