Proposed resolution

This will be the high level implantation actual checks will be done by the stock API classes.

User Interface enforcement - Control the user interface to enforce stock control
Objectives:

commerce interface: AvailabilityCheckerInterface.php

a) Don't allow the purchasing of products that are out of stock (this is for enforcement not the actual checking)
Proposed:

Disable the add to cart if needed
Check before adding to cart
Check before entering checkout
Check before payment

b) Allow for configuration of button and messages.
Proposed:

Text & class for disabled button.
Text for out of stock messages for the 3 other checks

c) Support for alternative workflows - Instead of just disabling the add to cart button provide alternative actions to the user.
This can be:"Let me know when back in stock", "purchase for later delivery", any other examples ?
Proposed:

TBA - we are looking at supporting those actions not developing them as part of the stock module.

Remaining tasks

This will be implemented in 2 stages
1. Create a new module commerce_stock_enforcement - use form alter and utility functions to prototype (a)
2. Refactor the code using OOP to make it easy to override functionality needed for (b) and (c)

Comments

guy_schneerson created an issue. See original summary.

guy_schneerson’s picture

Issue summary: View changes
guy_schneerson’s picture

the two interfaces we should be working with are:
AvailabilityManagerInterface
function addChecker(AvailabilityCheckerInterface $checker);
function check(PurchasableEntityInterface $entity, $quantity = 1);
function getCheckers();

AvailabilityCheckerInterface
function applies(PurchasableEntityInterface $entity);
function check(PurchasableEntityInterface $entity, $quantity = 1);

looking at the commerce code we have a class implementing the Manager interface: AvailabilityManager but this class is not used anywhere.

guy_schneerson’s picture

I have implanted the AvailabilityCheckerInterface using the class StockAvailabilityChecker created a configuration class to provide the locations to check and updated the test form to run the code for testing. I am now waiting for the interface to be used by the cart form in commerce to be able to test it properly.

steveoliver’s picture

Title: User Interface enforcement » [meta] Provide configurable stock transaction enforcement in UI
Priority: Normal » Major
Parent issue: #2613264: Drupal 8 port of Commerce Stock » #2834966: [META] Commerce_stock release 8.x-beta1

This is important for store usability and business logic. Let's keep discussing whole scope and needs of API/configuration for enforcement of stock transactions based on configuration of managed services, entity types, and entities in various situations.

steveoliver’s picture

Status: Active » Needs work
olafkarsten’s picture

I'm not sure that holds true anymore? Shouldn't stock simply tell - product is available from stock and leave the handling of that information to commerce core or other contrib?

guy_schneerson’s picture

Looks like the availability manager will not be ready soon, so we are going to have to implement the UI enforcement ourselves.

guy_schneerson’s picture

Proposed implementation:

Implement as a sub module - for now will handle common use case without advance features like "Let me know when back in stock". The module can always be disabled and a replacement module used. We can look at a better way of extending the behivier once we got basic functionality for beta ready.

StockAvailabilityChecker->check() - We need to remove the StockAvailabilityChecker or disabling it by always returning TRUE. We can make this a configuration option to enable the integration although it will likely change if it is implemented in commerce. I would like to keep it in as I hope the checker will evantuly move to core.

hook for alter for add to cart form - disable if no stock and use a submit handler to check again on submit. also consider hook_ENTITY_TYPE_view

Control /cart page using a hook for alter - to make sure adjusted quantities are ok. as it is a ViewsForm check for it being "commerce_cart_form"

Control entering checkout not sure whats the best way, possible options are:
* Altering the Routing system
* A commerce specific code for checkout ????
* A custom checkout pane - can be moved around the checkout steps to control how deep they can enter checkout with out of stock items.

final check before payment not sure form alter or a better commerce way?

guy_schneerson’s picture

Assigned: Unassigned » guy_schneerson
guy_schneerson’s picture

Issue summary: View changes
guy_schneerson’s picture

I have updated the issue with my plan moving forward.
I have also started work on this and hope to soon have a PR with a working stage 1.

guy_schneerson’s picture

Status: Needs work » Needs review

I have created a pull request PR #55 with basic UI enforcement (Phase one)
It should protect the following:

  • Add to cart,
  • Cart page
  • Checkout (all steps including submission of payment step)

The code does not:

  • provide any configuration.
  • provide some sort of plugin system or other way of extending the enforcement functionality.

The code is in a new sub module and can be disabled and an alternative module used.
We need to test the functionality and if works, than that is all that is needed for a beta version. Other improvements can come later.
needs testing !

  • guy_schneerson committed f1c949c on bbguy-2699611-stock_ui_enforcement
    Issue #2699611 - fix add to cart / cart issues
    
guy_schneerson’s picture

I have updated PR #55 basic UI enforcement is now fully working but don't take my word for it give it a go.

Christophe Mollet’s picture

Hi,

I have seen there is an error when we click on "update cart". (see attached file)

I have created a patch to fix this bug.

Feel free to contact me if you have any question.

guy_schneerson’s picture

Thanks @Christophe Mollet, great work.
That looks good. Ill try and test next week. Do let me know if you find any other issues.

p4trizio’s picture

Great job on commerce_stock_enforcement! I also found the error on update cart and the snippet suggested by @Christophe Mollet solves it

Time to merge it in dev!

guy_schneerson’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @p4trizio

p4trizio’s picture

@guy_schneerson I just noticed a problem on admin checkout flow forms:

Error: Call to undefined method Drupal\commerce_checkout\Form\CheckoutFlowForm::getOrder() in commerce_stock_enforcement_form_alter() line 76 

The attached patch should fix it.
Please tell us how can we contribute these small fixes, do you prefer pull requests on Github?

guy_schneerson’s picture

Status: Reviewed & tested by the community » Needs work

Hi @p4trizio Thanks for that.
A patch is fine for this one. Ill try and review in the next week or so.

guy_schneerson’s picture

Status: Needs work » Reviewed & tested by the community

Hi @p4trizio
That's a great find and the extra condition in the if statement fixes the issue.
I have committed both patches by you and Christophe Mollet to the PR and setting issue back to tested by the community.

guy_schneerson’s picture

Status: Reviewed & tested by the community » Fixed

Big thank you to Christophe Mollet and Patrizio Emili for their help.

This is now committed so anyone finding any issues please start a new issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.