hook form_commerce_order_item_add_to_cart_form_alter() should not put form access to FALSE

Created on 23 May 2024, about 1 month ago
Updated 18 June 2024, 10 days ago

Problem/Motivation

Stock validation "$stock <= 0" on the commerce_simple_stock_form_commerce_order_item_add_to_cart_form_alter() hook changes the form submit access. Since there are other modules that can interact with this hook we should make a validation on the submit action and let the other modules do their own stuff.

Actual validation:

    if ($stock <= 0) {
      $form['quantity'] = NULL;
      $form['actions']['#access'] = FALSE;
      $form['outofstock'] = [
        '#type' => 'textfield',
        '#value' => t('Out of Stock'),
        '#weight' => 0,
        '#disabled' => TRUE,
      ];
    }

Example module: https://www.drupal.org/project/commerce_wishlist - adds the WISHLIST button in the form actions. If we put '#access' to FALSE the wishlist button is not rendered.

Steps to reproduce

Install and configure wishlist module with simple_stock and add a configure a product without stock.

Proposed resolution

Change the submit button attributes.

Actual validation:

    if ($stock <= 0) {
      $form['quantity'] = NULL;
      $form['actions']['submit']['#attributes']['disabled']  = 'disabled';
      $form['outofstock'] = [
        '#type' => 'textfield',
        '#value' => t('Out of Stock'),
        '#weight' => 0,
        '#disabled' => TRUE,
      ];
    }
🐛 Bug report
Status

Active

Version

1.2

Component

Code

Created by

🇵🇹Portugal chicodasilva

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024