1

I'm looking to fetch all line items and send it as a POST request to an endpoint. I've had a look through the class references but can't seem to find a way to get all the line items that are currently active.

Event::on(
  Payments::class,
  Payments::EVENT_BEFORE_CAPTURE_TRANSACTION,
  function(TransactionEvent $event) {

        $transaction = $event->transaction;
        // @var LineItem $lineItem
        $lineItem = $event->lineItem;

        $client = new \Guzzle\Http\Client([
            // You can set any number of default request options.
            'Authorization'  => 'qdaiciDiyMaTjxMt, 74026b3dc2c6db6a30a73e71cdb138b1e1b5eb7a97ced46689e2d28db1050875',
        ]);


        // Add Cart Data to an Array
        $order_data = array(
            'product_id' => $lineItem->id,
            'quantity' => $lineItem->quantity
        );
...
2

Your error is in this bit:

        // @var LineItem $lineItem
        $lineItem = $event->lineItem;

Looking at the event in question:

https://craftcms.com/docs/commerce/3.x/events.html#beforecapturetransaction

...this shows that you get a Transaction model supplied in this event:

https://docs.craftcms.com/commerce/api/v3/craft-commerce-models-transaction.html#public-properties

...note that model gives you direct access to the related order element:

https://docs.craftcms.com/commerce/api/v3/craft-commerce-elements-order.html

...from which you can then retrieve all the line items (and products etc associated with them), by looping over order.lineItems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.