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)
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | 2699611-commerce_stock_enforcement-fix-21.patch | 1.5 KB | p4trizio |
| #17 | commerce_stock_fix_get_order_id.patch | 887 bytes | Christophe Mollet |
| #17 | Capture du 2018-07-20 14-03-47.png | 231.67 KB | Christophe Mollet |
Comments
Comment #2
guy_schneerson commentedComment #3
guy_schneerson commentedthe 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.
Comment #4
guy_schneerson commentedI 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.
Comment #5
guy_schneerson commentedComment #6
steveoliver commentedThis 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.
Comment #7
steveoliver commentedWIP!
Started work at https://github.com/BBGuy/commerce_stock/pull/12. NOTE: Require Drupal Commerce PR 593.
Comment #8
olafkarsten commentedI'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?
Comment #9
guy_schneerson commentedLooks like the availability manager will not be ready soon, so we are going to have to implement the UI enforcement ourselves.
Comment #10
guy_schneerson commentedProposed 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?
Comment #11
guy_schneerson commentedComment #12
guy_schneerson commentedComment #13
guy_schneerson commentedI 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.
Comment #14
guy_schneerson commentedI have created a pull request PR #55 with basic UI enforcement (Phase one)
It should protect the following:
The code does not:
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 !
Comment #16
guy_schneerson commentedI have updated PR #55 basic UI enforcement is now fully working but don't take my word for it give it a go.
Comment #17
Christophe MolletHi,
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.
Comment #18
guy_schneerson commentedThanks @Christophe Mollet, great work.
That looks good. Ill try and test next week. Do let me know if you find any other issues.
Comment #19
p4trizio commentedGreat 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!
Comment #20
guy_schneerson commentedThanks @p4trizio
Comment #21
p4trizio commented@guy_schneerson I just noticed a problem on admin checkout flow forms:
The attached patch should fix it.
Please tell us how can we contribute these small fixes, do you prefer pull requests on Github?
Comment #22
guy_schneerson commentedHi @p4trizio Thanks for that.
A patch is fine for this one. Ill try and review in the next week or so.
Comment #23
guy_schneerson commentedHi @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.
Comment #25
guy_schneerson commentedBig 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.