Skip to content

Unable to add product to cart if stock is not configured. #698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nickfrederiksen opened this issue Apr 30, 2025 · 39 comments
Closed

Unable to add product to cart if stock is not configured. #698

nickfrederiksen opened this issue Apr 30, 2025 · 39 comments
Labels

Comments

@nickfrederiksen
Copy link

Which component is this issue related to?

Umbraco Commerce (Core)

Which Umbraco Commerce version are you using? (Please write the exact version, example: 10.1.0)

15.3.1

Bug summary

There's an issue where a product that has no stock information configured, can't be added to the cart.

I get this error:
Insufficient stock: Product '9bdde03e-7339-4e58-9321-f03c92d3c9f4' has 0,000000 item(s) left, but 1 were requested.

We have no custom validators and this issue seems to have been introduced with the 15.3.0 update. The product does not have stock and should therefore not be validated as such.

It's rather critical for this to be fixed.

Specifics

No response

Steps to reproduce

Install 15.3.0 (or 15.3.1).

Create product with no stock property.

Add that product to the basket.

Expected result / actual result

Product should be added. With no error.

Dependencies

No response

@nickfrederiksen nickfrederiksen added the bug Something isn't working label Apr 30, 2025
@mattbrailsford
Copy link
Contributor

Does the product node have an actual stock property on it?

@nickfrederiksen
Copy link
Author

It does not.

@niekvanderreest
Copy link

Having similar problems, there seems to be a major bug or breaking change introduced in 15.3.

I today updated a project to use Commerce 15.3.1 from 15.2.2 and suddenly it's impossible to add products, any product to the cart, also a workaround with adding a stock property doesn't work, it seems the Stock property isn't binding to IPublishedContent...

I do have a custom productadapter in place, removing it for the default makes adding products work again

@mattbrailsford
Copy link
Contributor

Yea, in 15.3.0 some extra validation handlers were added to ensure an item being added to the cart doesn't exceed the stock of an item, however these shouldn't trigger if there is no stock property editor on your node.

@nickfrederiksen are you also using a custom product adapter?

I've got a workaround I can give you but I'd like to figure out what the actually bug is here as on my local environment, a product with no stock property editor on the product doc type doesn't trigger the validators

@nickfrederiksen
Copy link
Author

@mattbrailsford
We are indeed using a custom product adapter. It only handles prices, why I thought it wasn't relevant to this.

@mattbrailsford
Copy link
Contributor

Yea, product adapters aren't really responsible for managing stock levels so it's weird if they are connected 🤔

@mattbrailsford
Copy link
Contributor

Ahh, ok, I think I might have found it

@niekvanderreest
Copy link

@mattbrailsford Calling GetProductSnapshotAsync on my product adapter calls the base class method:
await TryGetProductNodeAsync(productReference, productVariantReference);

The result has an iPublishedContent for item1 (the product). When i add a stock property to my document type i see the value correctly in the properties collection, but the actual property Stock on the model shows 0M as value, maybe a valuebinder problem?

@mattbrailsford
Copy link
Contributor

mattbrailsford commented Apr 30, 2025

So I think the problem is when we work out the stock, we have to try and work out where the stock is stored because we can have nodes with no variants, nodes with child variants, and nodes with multi variants. When we work that out, we request the a product snapshot and see if it is IUmbracoProductSnapshot and from there we can get the stock source. But if the product snapshot doesn't implement IUmbracoProductSnapshot then it will return a default stock of 0.

Ultimately I think this needs updating to return a default stock of null as it's more the case that no stock was found, than there actually being zero items.

A quick test your end might be to have your product adapter return a product snapshot that implements IUmbracoProductSnapshot either directly, or by extending the default UmbracoProductSnapshot.

I'll do a quick build though with the default stock defaulting to null and see if that works for you too.

@mattbrailsford
Copy link
Contributor

There should be a 15.3.2--preview.4 build on the nightly feed that sets the default stock to null if either of you wants to try that.

@niekvanderreest
Copy link

@mattbrailsford Thanks for the quick action on this, just to make everything complete, I implemented the IUmbracoProductSnapshot interface on my custom ProductSnapshot that previously only implemented IProductSnapshot.

This also fixes the issue and makes adding products (without stock property) work again

@nickfrederiksen
Copy link
Author

@mattbrailsford

Same issue.

All I do in my adapter, is to get the snapshot using base.GetProductSnapshotAsync, and then manipulate the Prices property.
If the base.GetProductSnapshotAsync does not return a UmbracoProductSnapshot, i will ignore it and just return it as is.

@mattbrailsford
Copy link
Contributor

mattbrailsford commented Apr 30, 2025

@nickfrederiksen by "same issue" you mean running 15.3.2--preview.4 still throws the same exception?

@nickfrederiksen
Copy link
Author

@mattbrailsford correct.

@mattbrailsford
Copy link
Contributor

mattbrailsford commented Apr 30, 2025

@nickfrederiksen ok, so is there anything else custom about your install? Have you implemented a custom IStockService?

@nickfrederiksen
Copy link
Author

@mattbrailsford tons, but nothing I can see is related. We have some notification handlers for OrderLineAddingNotification, but no custom validation or anything else. Most customizations relates to checkout.

We do have a custom discount rule. But that only checks for the current session and some custom database entries.

@mattbrailsford
Copy link
Contributor

Is the product being added to the cart a simple product? or a bundle?

@nickfrederiksen
Copy link
Author

Simple product. Nothing fancy.

@mattbrailsford
Copy link
Contributor

Did your node EVER have a stock property on it?

@nickfrederiksen
Copy link
Author

Never.

@mattbrailsford
Copy link
Contributor

if you look in the umbracoCommerceStock database table, is there an entry for your nodes key?

@nickfrederiksen
Copy link
Author

It does. And it is set to 0.

Could it be a bug where a default 0-value has been added on save if no stock property has present?

@mattbrailsford
Copy link
Contributor

It very well could be as it shouldn't be storing a zero value, and it definitely shouldn't be storing a value for a product that doesn't have a stock property on it

@mattbrailsford
Copy link
Contributor

Do you have other entries with zero values?

@nickfrederiksen
Copy link
Author

I do. I'm currently trying to get a list. But it seems to only be a few products. Those that has been saved after the upgrade.

@mattbrailsford
Copy link
Contributor

You mean republishing the product content node?

@nickfrederiksen
Copy link
Author

Yes.

@mattbrailsford
Copy link
Contributor

Have you been experimenting with the partial refunds feature at all? I may have found an edge case here

@nickfrederiksen
Copy link
Author

No. I haven't. Not yest.

@mattbrailsford
Copy link
Contributor

Shoot! Was hoping that was it. Struggling to think of other scenarios that would cause the stock to get updated in the stock table.

@mattbrailsford
Copy link
Contributor

I've raised an issue here with details of the edge case I've found #699

@mattbrailsford
Copy link
Contributor

So @nickfrederiksen I can give you some code to disable the new validation event handlers which would solve the problem for you for now, but I really would like to figure out why you have stock levels in the DB at zero. The issue above is the only way I've managed to replicate it at the moment.

@nickfrederiksen
Copy link
Author

@mattbrailsford Sure, let me have it. At least it will make things work for now.

@mattbrailsford
Copy link
Contributor

You can add this in your AddUmbracoCommerce

builder.WithValidationEvent<ValidateOrderProductAdd>()
    .RemoveHandler<ValidateProductStockOnOrderProductAdd>();

builder.WithValidationEvent<ValidateOrderFinalize>()
    .RemoveHandler<ValidateProductStockOnOrderFinalize>();

builder.WithValidationEvent<ValidateOrderLineQuantityChange>()
    .RemoveHandler<ValidateProductStockOnOrderLineQuantityChange>();

@nickfrederiksen
Copy link
Author

Seems to be working now.

@mattbrailsford
Copy link
Contributor

Ok, I've pushed a 15.3.2--preview.6 to the nightly feed (should be there shortly). It would be good to know if you comment that code back out, and then maybe delete any stock db entries with a value of 0, can you still replicate the issue.

@nickfrederiksen
Copy link
Author

@mattbrailsford Seems to be working now. Also after a save-and-publish on a non-stock product.

@mattbrailsford
Copy link
Contributor

Ok great. Then I'll consider it fixed, but if you do come across it again, please let me know and you've always got the code snippet in your back pocket to get it back to working.

@nickfrederiksen
Copy link
Author

Thanks! I have kept a note on the workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants