Manufacturer Picture instead of default picture

5 months ago
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?
5 months ago
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
5 months ago
did not work.
manufacturer started to produce different pictures instead of pictures, but these are pictures of other products
Thank you for your help.
5 months ago
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;
}
5 months ago
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.
5 months ago
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;
}
5 months ago
Very good
Thanks for your help.