Score:0

How can show a message for products that are not in stock?

ye flag

I am implementing custom stock management in Commerce. I followed the following steps.

  1. I have added a stock integer field in product variant.

  2. I have written a event subscriber which reduce this stock field value after purchasing.

    public static function getSubscribedEvents() {
      $events['commerce_order.place.post_transition'] = ['orderCompleteHandler'];
    
      return $events;
    }
    
    /**
     * Reduces the stock value after purchase.
     *
     * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
     *   Event object.
     */
     public function orderCompleteHandler(WorkflowTransitionEvent $event) {
       /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
       $order = $event->getEntity();
       // Order items in the cart.
       $items = $order->getItems();
    
       foreach ($items as $key => $item) {
         $entity = $item->getPurchasedEntity();
         $quantity = $item->getQuantity();
         $stock_field_value = $entity->field_stock->value;
         $entity->field_stock->value = $stock_field_value - $quantity;
         $entity->save();
       }
     }
    
  3. Then I have a stock checker which implements AvailabilityCheckerInterface.

    namespace Drupal\commerce_custom_stock;
    
    use Drupal\commerce\Context;
    use Drupal\commerce_order\AvailabilityCheckerInterface;
    use Drupal\commerce_order\AvailabilityResult;
    use Drupal\commerce_order\Entity\OrderItemInterface;
    
    /**
     * Class to check stock.
     */
    class VariationAvailabilityChecker implements AvailabilityCheckerInterface {
    
      /**
       * {@inheritdoc}
       */
      public function applies(OrderItemInterface $order_item) {
        return TRUE;
      }
    
      /**
       * {@inheritdoc}
       */
      public function check(OrderItemInterface $order_item, Context $context) {
        $entity = $order_item->getPurchasedEntity();
        $quantity = $order_item->getQuantity();
        // Order should not be placed if Stock is zero.
        if ($entity->field_stock->value <= 0) {
          $result = t('This product is out of stock.');
          return AvailabilityResult::unavailable($result);
        }
        // Cart quantity should not be more than stock value.
        if ($quantity > $entity->field_stock->value) {
          $result = t('The quantity is more than the stock quantity');
          return AvailabilityResult::unavailable($result);
        }
    
        return AvailabilityResult::neutral();
      }
    
    }
    
    

Two customers add the same product in the cart; one of them purchased and finished the stock. If the person tries to purchase it, a 403 error is shown. I want to show some message instead of the 403 error.

Is there any way to achieve this?

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.