Show a Text like Prices only available after Login

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

I make a B2B Shop with NopCommerce
I deactivate Show Prices in the ACL for Guest Users

Ok this works i see no Price as Guest User

now i want to show a Text for Guest Users
like "Prices only available after Login.. please Login"
on the Product where shows normal the Price...

how can i do this ?

which File i must edit and which peace of Code i must use?

Thanks
Benny
6 years ago
You can do it by following steps:

*** example code for nopCommerce 3.90 ***

### STEP 1 ###

Create custom model factory for product, it named CustomProductModelFactory.


public class CustomProductModelFactory : ProductModelFactory
    {
        private readonly IPermissionService _permissionService;

        public CustomProductModelFactory(
            ISpecificationAttributeService specificationAttributeService, ICategoryService categoryService, IManufacturerService manufacturerService,
            IProductService productService, IVendorService vendorService, IProductTemplateService productTemplateService,
            IProductAttributeService productAttributeService, IWorkContext workContext, IStoreContext storeContext,
            ITaxService taxService, ICurrencyService currencyService, IPictureService pictureService,
            ILocalizationService localizationService, IMeasureService measureService, IPriceCalculationService priceCalculationService,
            IPriceFormatter priceFormatter, IWebHelper webHelper, IDateTimeHelper dateTimeHelper,
            IProductTagService productTagService, IAclService aclService, IStoreMappingService storeMappingService,
            IPermissionService permissionService, IDownloadService downloadService, IProductAttributeParser productAttributeParser,
            IDateRangeService dateRangeService, MediaSettings mediaSettings, CatalogSettings catalogSettings,
            VendorSettings vendorSettings, CustomerSettings customerSettings, CaptchaSettings captchaSettings, SeoSettings seoSettings, ICacheManager cacheManager)
            : base(
                specificationAttributeService, categoryService, manufacturerService,
                productService, vendorService, productTemplateService,
                productAttributeService, workContext, storeContext,
                taxService, currencyService, pictureService,
                localizationService, measureService, priceCalculationService,
                priceFormatter, webHelper, dateTimeHelper,
                productTagService, aclService, storeMappingService,
                permissionService, downloadService, productAttributeParser,
                dateRangeService, mediaSettings, catalogSettings,
                vendorSettings, customerSettings, captchaSettings, seoSettings, cacheManager)
        {
            _permissionService = permissionService;
        }

        protected override ProductOverviewModel.ProductPriceModel PrepareProductOverviewPriceModel(Product product, bool forceRedirectionAfterAddingToCart = false)
        {
            var priceModel = base.PrepareProductOverviewPriceModel(product, forceRedirectionAfterAddingToCart);
            if (!_permissionService.Authorize(StandardPermissionProvider.DisplayPrices))
            {
                priceModel.Price = "Prices only available after Login ... please Login";
            }

            return priceModel;
        }

        protected override ProductDetailsModel.ProductPriceModel PrepareProductPriceModel(Product product)
        {
            var priceModel = base.PrepareProductPriceModel(product);
            if (!_permissionService.Authorize(StandardPermissionProvider.DisplayPrices))
            {
                priceModel.Price = "Prices only available after Login ... please Login";
            }

            return priceModel;
        }
    }


### STEP 2 ###

Register the created custom product model factory


public partial class DependencyRegistrar : IDependencyRegistrar
    {
        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<CustomProductModelFactory>().As<IProductModelFactory>()
                .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static")).InstancePerLifetimeScope();
        }

        public int Order { get { return 100; } }
    }


Hope that help :)
6 years ago
Hi Tuấn Đỗ

i test this but i use nopcommerce version 4.0

I create a Class under Factories CustomProductModelFactory with your Code and correct al Using Statements

but under :base i become an Error

is there some different with your code for version 3.9 and the nopcommerce version 4.0

and where i must register the new
created custom product model factory?

thanks
Benny
6 years ago
websystem2610 wrote:
Hi Tuấn Đỗ

i test this but i use nopcommerce version 4.0

I create a Class under Factories CustomProductModelFactory with your Code and correct al Using Statements

but under :base i become an Error

is there some different with your code for version 3.9 and the nopcommerce version 4.0

and where i must register the new
created custom product model factory?

thanks
Benny


I did other version of code for nopCommerce 4.0
Please read it here (with embedded code from GitHub).

Hope that help :)
6 years ago
Hi Tuấn Đỗ

wow thank you it works perfect!!

but one Problem i have
when shows the Text Message instead the price
in my skin looks not good... my font for the Price is to large
to show the Text nice it cutes the text....

it is posible to show the Text in a separate Text Field and not instead the Price ?

or i must found a way to change the style of the Text where is instead the Price
to look nice and the long text is not cutted...

Thanks so much Tuấn Đỗ ;-)
6 years ago
The Price field supported to render as raw HTML, so that you can add markup and CSS class for it in order to have your desired styles.
4 years ago
I did it the following way...
At ACL (Access Control List settings) I have Disabled viewing prices by GUESTS role.

Then I used CSS pseudo-element :EMPTY

<span class="price actual-price"></span>

Like following:

span.actual-price:empty:before {
content:"Register to view prices!";
}


It puts CONTENT text to those SPAN tags which are empty inside.

Maybe it helps someone.

Thanks.
3 years ago
zaf wrote:
I did it the following way...
At ACL (Access Control List settings) I have Disabled viewing prices by GUESTS role.

Then I used CSS pseudo-element :EMPTY

<span class="price actual-price"></span>

Like following:

span.actual-price:empty:before {
content:"Register to view prices!";
}


It puts CONTENT text to those SPAN tags which are empty inside.

Maybe it helps someone.

Thanks.


The above CSS solution works great, but if you want a slightly more interactive experience using JS the script below replaces the empty tag with a button which links to the login page, hope this is useful for somebody:
    function isEmpty( el ){
        return !$.trim(el.html())
    }
    if (isEmpty($('.actual-price'))) {
        $('.actual-price').append('<a href="/login" class="btn-secondary login-button title=">Login to buy</a>');      
    }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.