Problem/Motivation

Right now if you add more products than the available it say that the product was added to the cart, but the cart is empty, the same happens when you update the cart.

Proposed resolution

For the CartForm we should add the validation in the EditQuantity class.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

edurenye created an issue. See original summary.

edurenye’s picture

Project: Commerce Stock » Commerce Core
Version: 8.x-1.x-dev » 8.x-2.x-dev
Component: Code » Cart

This affects the commerce stock module, but should be fixed in the commerce using the availability manager.

edurenye’s picture

Assigned: Unassigned » edurenye
edurenye’s picture

Status: Active » Needs review
StatusFileSize
new3.16 KB

Done.

drugan’s picture

The #4 is worked for me. Great addition!

joachim’s picture

Status: Needs review » Needs work

A few minor problems:

  1. +++ b/modules/cart/src/Form/AddToCartForm.php
    @@ -192,6 +192,24 @@ class AddToCartForm extends ContentEntityForm implements AddToCartFormInterface
    +    $availabilityManager = \Drupal::service('commerce.availability_manager');
    
    +++ b/modules/cart/src/Plugin/views/field/EditQuantity.php
    @@ -140,6 +141,31 @@ class EditQuantity extends FieldPluginBase {
    +    $availabilityManager = \Drupal::service('commerce.availability_manager');
    

    This should be injected.

  2. +++ b/modules/cart/src/Form/AddToCartForm.php
    @@ -192,6 +192,24 @@ class AddToCartForm extends ContentEntityForm implements AddToCartFormInterface
    +      $form_state->setErrorByName('quantity', t('This amount of purchasable entities are not available.'));
    
    +++ b/modules/cart/src/Plugin/views/field/EditQuantity.php
    @@ -140,6 +141,31 @@ class EditQuantity extends FieldPluginBase {
    +          $form_state->setErrorByName($this->options['id'], t('This amount of purchasable entities are not available.'));
    

    'This amount' is singular, but 'are not' is plural...

joachim’s picture

Status: Needs work » Needs review
StatusFileSize
new7.27 KB

Fixed the problems in #6, and also changed the message to mention the product variation name, so it's clearer.

drugan’s picture

This functionality is now implemented in the Commerce Extended Quantity module:

https://www.drupal.org/project/commerce_xquantity

Thanks to @edurenye and @joachim for the changes made in the EditQuantity.php file. Though in the AddToCartForm.php file I've done this a little differently:

http://cgit.drupalcode.org/commerce_xquantity/tree/src/Form/XquantityAdd...

mglaman’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#3088597: Add a constraint to purchased_entity which checks the availability manager

I'm going to close this as a duplicate of #3088597: Add a constraint to purchased_entity which checks the availability manager (even though it's newer!)

The linked issue puts the validation at the data layer level, so it'll work anywhere.

Feel free to comment if you try it out and find out I'm incorrect!

mglaman’s picture