disable the "Add to Cart"

Created on 27 December 2024, 3 months ago

"Wouldn't it be better if the 'Add to Cart' button is completely disabled and a message appears saying that the product has already been purchased?"

And here's the updated code based on that suggestion:

This code will disable the "Add to Cart" button and show a message saying "Already purchased this product" if the user has already purchased the product.

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function commerce_product_limits_form_commerce_order_item_add_to_cart_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Limit quantity on the first loading of the page.
  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = $form_state->getFormObject()->getEntity();
  $entity = $order_item->getPurchasedEntity();
  if ($form_state->has('selected_variation')) {
    $variation_storage = \Drupal::entityTypeManager()->getStorage('commerce_product_variation');
    $entity = $variation_storage->load($form_state->get('selected_variation'));
  }
  if (!$entity instanceof PurchasableEntityInterface) {
    return;
  }
  
  // Get the current user ID.
  $current_user = \Drupal::currentUser();
  $uid = $current_user->id();

  // Query the orders made by the current user.
  $orders = \Drupal::entityTypeManager()->getStorage('commerce_order')->loadByProperties([
    'uid' => $uid,
    'state' => 'completed', // Only check completed orders
  ]);

  // Check if the user has already purchased this product.
  foreach ($orders as $order) {
    foreach ($order->getItems() as $order_item) {
      if ($order_item->getPurchasedEntity()->id() == $entity->id()) {
        // If the product has been purchased, disable the "Add to cart" button.
        $form['actions']['submit']['#disabled'] = TRUE;
        $form['actions']['submit']['#value'] = t('Already purchased this product');
        break 2; // Exit both loops as we found the product
      }
    }
  }

  // Get limit fields from product variation.
  if ($entity->hasField('minimum_order_quantity') && !$entity->get('minimum_order_quantity')->isEmpty()) {
    $form['quantity']['widget'][0]['value']['#default_value'] = $entity->get('minimum_order_quantity')->value;
    $form['quantity']['widget'][0]['value']['#min'] = $entity->get('minimum_order_quantity')->value;
  }
  if ($entity->hasField('maximum_order_quantity') && !$entity->get('maximum_order_quantity')->isEmpty()) {
    $form['quantity']['widget'][0]['value']['#max'] = $entity->get('maximum_order_quantity')->value;
  }

  if ($entity->hasField('step_order_quantity') && !$entity->get('step_order_quantity')->isEmpty()) {
    $form['quantity']['widget'][0]['value']['#step'] = $entity->get('step_order_quantity')->value;
  }
}
Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇷Iran hadiseh

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

Comments & Activities

Production build 0.71.5 2024