Developer forum

Forum » Development » Overrule stock check

Overrule stock check

Anders Ebdrup
Anders Ebdrup
Reply

Hi Dynamicweb,

 

We have two shops in the same solution, where we need different stock checks; one should reserve when items are added to basket and you are not allowed to order more than we have in stock. I the other shop you can as many items as you want no matter what is in stock.

So in the OrderLineService: OrderLineBuilder I hope that you can add a new notifier where I can overrule the variable: doCheckStock? The notifier should have the value for doCheckStock and the current product to be added to the cart.



            ' Check stock and reserve
            Dim doCheckStock As Boolean = Converter.ToBoolean(SystemConfiguration.Instance.GetValue("/Globalsettings/Ecom/Product/DontShowProductIfNotOnStock"))
            If ProductReserve.Enabled AndAlso doCheckStock AndAlso productToPutInCart.Type <> ProductType.Service AndAlso productToPutInCart.Type <> ProductType.GiftCard AndAlso theOrderLine.HasType({OrderLineType.Product, OrderLineType.PointProduct}) Then
                Dim cartStock As Double = ProductReserve.GetReservedAmount(theOrderLine.ProductId, theOrderLine.ProductVariantId) + quantityOrderLine
                If ProductReserve.Mode = ProductReserveMode.Checkout Then
                    ' When products is reserved only in checkout step we should take into account is there such product exist in cart
                    cartStock += order.OrderLines.
                        Where(Function(cartLine) Not String.IsNullOrEmpty(cartLine.ProductId) AndAlso cartLine.ProductId.Equals(productId, StringComparison.OrdinalIgnoreCase) AndAlso
                        ((String.IsNullOrEmpty(cartLine.ProductVariantId) AndAlso String.IsNullOrEmpty(variantId)) OrElse cartLine.ProductVariantId.Equals(variantId, StringComparison.OrdinalIgnoreCase))).
                        Select(Function(cartLine) cartLine.Quantity).FirstOrDefault()
                End If

                If productToPutInCart.UnitStock < cartStock Then
                    Return Nothing
                End If
            End If

 

Please add this in a near hotfix :-)

 

Best regards, Anders


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

I like Anders solution. We were actually going a little further, to have a checkbox on the product level to enable or disable stock control. Most of our customers have several thousands of products. Some of them are always being produced/orders, so they can take backorders (where this "Disable stock control" checkbox would come in handy), where other products in the same catalog are no longer being produced, so stock control is important.

 

Best Regards,

Nuno Aguiar

 
Anders Ebdrup
Anders Ebdrup
Reply

Is it in any way possible to have the suggested notifier added in the next hotfix?

 
Martin Vang
Martin Vang
Reply

 

Hi Anders,

Good question - I do have some concerns, though.

We use this global configuration in a number of locations to defined how the website behaves as a whole:

CartService.vb(516)
OrderLineService.vb(529)
OrderService.vb(770)
ProductCollection.vb(274)
ProductRepository.vb(1381)
ProductService.vb(195)
ProductService.vb(1235)

Are you sure that it's enough to only add a nofitication in that single location?

BR

Martin

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi Anders

We added the OrderLineService extension to Dynamicweb 9.5+ versions

To upgrade please choose this version from download:

http://doc.dynamicweb.com/releases-and-downloads/releases

Let me know if you need any more help regarding this

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 
Anders Ebdrup
Anders Ebdrup
Reply

Hi Kristian,

 

Can you please add how we can use this new functionality?

 

Best regards, Anders

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi Anders

Nicolai will explain how this is done 

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Anders

It is the old case from back in may:

[Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Notifications.Standard.Application.BeforeDynamicwebStart)]
    public class BeforeStartObserver : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            Dynamicweb.Configuration.SystemConfiguration.Instance.AddProviderPriority(new MyConfigurationProvider());
        }
    }

And the config provider you created:

public class MyConfigurationProvider : IConfigurationProvider
    {
        public bool TryGet(string key, out string value)
        {
            if (key.Equals("/Globalsettings/Ecom/Product/DontShowProductIfNotOnStock", System.StringComparison.InvariantCultureIgnoreCase))
            {
                var pageview = Dynamicweb.Frontend.PageView.Current();
                if (pageview != null && pageview.Area.Item != null && pageview.Area.Item.ContainsKey("OverrideStockLogic"))
                {
                    value = pageview.Area.Item["OverrideStockLogic"].ToString();
                    return true;
                }
            }

            value = null;
            return false;
        }

        public bool Contains(string key)
        {
            return false;
        }

        public ICollection<string> GetKeys()
        {
            return new List<string>();
        }

        public void Persist()
        {
            // Do nothing in here
        }

        public void Reload()
        {
            // Do nothing in here
        }

        public void SetValue(string key, string value)
        {
            // Do nothing in here
        }
    }

 

Votes for this answer: 1
 
Anders Ebdrup
Anders Ebdrup
Reply

It looks like some code I already know :-) Thanks for clarifying

 

You must be logged in to post in the forum