Developer forum

Forum » Ecommerce - Standard features » Back in Stock Notification with ProductViewModel

Back in Stock Notification with ProductViewModel

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

I'm planning for a new project that requires Back in Stock Notifications, but as far as I can tell, the ProductViewModel does not support them yet. Is that accurate?

 

I looked into DW's source code that generates the tags and looks like we can ultimately use the API if needed, but wanted to confirm before going down this path

            double unitStock = product.GetUnitStock(renderingContext.StockLocation.Value, unitId);
            (...)
            if (template.TagExists("Ecom:Product.NotificationRegistered"))
            {
                if (unitStock <= 0d && ProductBackInStockNotification.BackInStockNotificationExists(product, ""))
                {
                    template.SetTag("Ecom:Product.NotificationRegistered", true);
                }
            }

 

Best Regards,

Nuno Aguiar


Replies

 
Nicolai Pedersen
Reply

Yes, you can do it using the api. Be aware of performance.

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

Thanks. Do I need to post a feature request for this, or is the plan for the ViewModels to implement all of the current features? I guess I can wait a couple of months until I need to implement it, meaning I can wait for the ViewModel to support it if that is coming "soon".

 

Best Regards,

Nuno Aguiar

 
Nicolai Pedersen
Reply
This post has been marked as an answer

It will never make it to the viewmodels as they are context independent and user is context.

I have created an extension method that you can call on a productviewmode: BackInStockRegisteredForUser in namespace Dynamicweb.Ecommerce.ProductCatalog

Never call it from a product list because of performance.

BR Nicolai

The implementation:

public static bool BackInStockRegisteredForUser(this ProductViewModel product, string unitId = "", long stocklocationId = 0)
        {
            if (Security.UserManagement.User.IsExtranetUserLoggedIn())
            {
                var p = Services.Products.GetProductById(product.Id, product.VariantId, product.LanguageId);
                var stockLocation = Services.StockLocationService.GetStockLocation(stocklocationId);
                double unitStock = p.GetUnitStock(stockLocation, unitId);
                if (unitStock <= 0d && ProductBackInStockNotification.BackInStockNotificationExists(p, unitId))
                {
                    return true;
                }
            }
            return false;
        }
Votes for this answer: 1
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

This is perfect. I understand and am aware of performance issues.

 

Thank you,

Nuno Aguiar

 

You must be logged in to post in the forum