Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Disallow selecting quantity of stock past what's available. #1905

Merged
merged 13 commits into from
Mar 10, 2020

Conversation

nerrad
Copy link
Contributor

@nerrad nerrad commented Mar 9, 2020

This addresses #1887

This pull does the following:

  • useStoreCartItemQuantity now receives the cartItem instead of the cartItemKey as an argument. I didn't notice in previous reviews how it's used in the context where we already have a cartItem so implementing this reduces complexity and makes the hook more precise for it's purpose.
  • Add backorders_allowed to the CartItem schema for the API. This allows for client to have correct logic for maximum quantity when this value is true.
  • Implement the above in the CartLineItemRow so that quantity picker is limited by the amount of stock remaining if that is available (lowStockRemaining) and the backorders_allowed is false.
  • maximum quantity is currently hardcoded to a (filtered) value of 99 when other conditions don't apply (see related issue in Introduce per product setting for maximum quantity selected for product. #1913)

To Test

  • Verify quantity selections work as expected.
  • Verify if you have a product with stock set, and the amount remaining is within the lowStockRemaining threshold that you see the low stock badge and the quantity is limited by the amount remaining.
  • Verify that if the above is true but the product also has backorders_allowed set to true that the quantity limit is 99.
  • Verify for all other conditions the quantity limit maxes out at 99.

Potential followup.

Should we automatically reduce the quantity selected for an item in the cart if the stock has gone down? Since we're updating the cart on every request, it's possible for us to do this (and we could trigger a notice when this happens). This could entice folks to move quicker on high turnover items.

We could do the above as a stretch goal, but it's nice knowing it's doable.

@nerrad nerrad requested a review from a team as a code owner March 9, 2020 12:49
@nerrad nerrad requested review from Aljullu and mikejolley and removed request for a team March 9, 2020 12:49
@nerrad nerrad self-assigned this Mar 9, 2020
@nerrad nerrad added the type: enhancement The issue is a request for an enhancement. label Mar 9, 2020
@nerrad nerrad added this to the Future Release milestone Mar 9, 2020
@nerrad
Copy link
Contributor Author

nerrad commented Mar 9, 2020

doh I need to fix php unit tests!

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I added some feedback and a question. Question regarding exposing stock via the API is the main blocker for me. Thoughts?

@@ -20,28 +20,93 @@ import ProductLowStockBadge from './product-low-stock-badge';
/**
* Cart line item table row component.
*/
const CartLineItemRow = ( { lineItem = {} } ) => {
const CartLineItemRow = ( {
lineItem = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we define these default objects somewhere for reuse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see how often it comes up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up removing this and making lineItem (via propTypes). That way there's no need for defaults because it's clear via the propTypes that the properties are required.

assets/js/type-defs/cart.js Show resolved Hide resolved
src/RestApi/StoreApi/Schemas/CartItemSchema.php Outdated Show resolved Hide resolved
@nerrad nerrad force-pushed the update/disallow-quantity-past-max-avaialble branch from fef3019 to 75fdddd Compare March 9, 2020 19:20
@nerrad
Copy link
Contributor Author

nerrad commented Mar 9, 2020

Ok @mikejolley I updated the description of the pull to account for the changes and this is ready for another review.

Copy link
Member

@mikejolley mikejolley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Pre-approving. One small comment about an unused new method.

*
* @param \WC_Product $product Product instance.
* @return integer|null
*/
protected function get_low_stock_remaining( \WC_Product $product ) {
protected function get_remaining_stock( \WC_Product $product ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed now right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It abstracted out the logic from get_low_stock_remaining (which calls this). I thought it'd be fine to leave it. It makes get_low_stock_remaining) a bit easier to follow and if this is needed in the future (or if the logic for getting remaining stock changes) we already have this available.

@nerrad nerrad deleted the update/disallow-quantity-past-max-avaialble branch March 10, 2020 11:44
@nerrad nerrad modified the milestones: Future Release, 2.6.0 Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants