1

so I have added this code below inside Module.php and the purpose is to get specific order details and pass them to a trustpilot script code which is also pasted below. I am not sure what data $order variable will return so I did a dd() on that variable. But problem is, I am not allowed to place test orders. So can someone tell me how do I get email, full name & order id from $order variable and pass them to the trustpilot javascript ?

Event::on(
    Order::class,
    Order::EVENT_AFTER_COMPLETE_ORDER,
    function(Event $event) {
        // @var Order $order
        $order = $event->sender;
        Craft::dd($order);
    }
);

Trustpilot script:

<script> 
    document.addEventListener('DOMContentLoaded', function() {
        const trustpilot_invitation = {
               recipientEmail: 'john@gmail.com',
               recipientName: 'John',
               referenceId: 'Order_123',
               source: 'InvitationScript',
          };
          tp('createInvitation', trustpilot_invitation);
    });
</script>
aslamdoctor
  • 3,753
  • 11
  • 53
  • 95

1 Answers1

0

Best to check the Craft Commerce API as a reference but these should do the trick for you.

$order->reference
$order->email

With first and last names there are two possible ways to get these. Either from the User assigned to the Order (if any) or from the Customer assigned to the order (if any as people can be guests). Note if you want to get these details from the customer you'll have to get it from the primary address - try to just use the User if possible... much easier.

e.g.

$order->user->firstName
mylesthe.dev
  • 9,565
  • 4
  • 23
  • 34