OrderPlacedSubscriber (Problem when changing subscribed event)

Created on 2 February 2023, over 1 year ago
Updated 6 February 2023, over 1 year ago

Problem/Motivation

I can't have the invoice being generated whenever an order is made, so i modified "OrderPlacedSubscriber.php" to

$events = [
      'commerce_order.payment_accepted_transition.post_transition' => ['onPlace'],
    ];

It works when the user clicks to change the status of the order, but when drupal does it automatically, it doesn't generate the pdf file and the invoice is not sent to the customer:


namespace Drupal\workflow_takanap\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Drupal\commerce_order\Event\OrderEvents;
    use Drupal\state_machine\Event\WorkflowTransitionEvent;
	
	use Drupal\commerce_order\Event\OrderEvent;
	use Drupal\Core\Language\LanguageManagerInterface;
    use Drupal\Core\Mail\MailManagerInterface;
    use Drupal\Core\StringTranslation\StringTranslationTrait;
	use Drupal\Core\DestructableInterface;
	use Drupal\commerce_order\Entity\OrderInterface;
	use Drupal\commerce_price\Price;
	use Drupal\Core\Entity\EntityTypeManagerInterface;
	use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\HasPaymentInstructionsInterface;
	use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface;
	use Drupal\Core\Entity\EntityInterface;
	use Drupal\Core\Entity\EntityTypeInterface;
	use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
	use Drupal\Core\Field\BaseFieldDefinition;
	use Drupal\Core\Form\FormStateInterface;
	use Drupal\Core\Render\Element;
	use Drupal\Core\Url;
	use Drupal\commerce\EntityOwnerTrait;
	use Drupal\Core\Entity\ContentEntityBase;
	use Drupal\Core\Entity\EntityChangedTrait;
	use Drupal\Core\Entity\EntityMalformedException;
	use Drupal\Core\Entity\EntityStorageInterface;
	use Drupal\profile\Entity\ProfileInterface;
	use Drupal\commerce_payment\Entity;
    class PaymentOrderEventSubscriber implements EventSubscriberInterface {

      public static function getSubscribedEvents() {

        $events = ['commerce_order.order.paid' => 'onOrderPaid'];
        return $events;
      }

      public function onOrderPaid(OrderEvent $event) {

		 $order_ent = $event->getOrder();
       $payment = json_encode($order_ent->getPay());
	   
			//if payment state is completed
		$order = $event->getOrder();
			
        // apply transition to order
        $order_state = $order->getState();
		$order_state->applyTransitionById('payment_accepted_transition');
		//$order->save();
        }
      
      public function onPlacePreTransition(WorkflowTransitionEvent $event) {
      
      }
    }

Is it some problem with permissions ? can you tell me if i'm doing something wrong?

πŸ’¬ Support request
Status

Active

Component

Code

Created by

πŸ‡΅πŸ‡ΉPortugal filipetakanap

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

Comments & Activities

  • Issue created by @filipetakanap
  • πŸ‡΅πŸ‡ΉPortugal filipetakanap

    I'm still having issues... Think the easiest way would be something like this in invoice.php

    $orderA = $this->get('orders')->first()->entity->getState();
    	
        if ($this->getState()->getId() == 'pending' && $orderA == "payment_accepted") {
          /** @var \Drupal\commerce_number_pattern\Entity\NumberPatternInterface $number_pattern */
          $number_pattern = $invoice_type->getNumberPattern();
          if ($number_pattern) {
            $invoice_number = $number_pattern->getPlugin()->generate($this);
            $this->setInvoiceNumber($invoice_number);
          }
        }

    I'm trying to get the order state "payment accepted" to generate a new invoice with a number.
    But i don't know the code to get the order state

Production build 0.69.0 2024