1

I've added an Entries field called linkedCourseEntry to a Product (Product Type > Product Fields tab). In the Product itself I've assigned an entry.

I need to access linkedCourseEntry from one or more Events, but can’t work out how to do so.

For example when EVENT_POPULATE_LINE_ITEM is used:

Event::on(
    LineItems::class,
    LineItems::EVENT_POPULATE_LINE_ITEM,
    function(LineItemEvent $event) {

        $lineItem = $event->lineItem;
        $purchasable = $lineItem->getPurchasable();

        // Somehow access the 'linkedCourseEntry' data here... help! 
    }
);

Can anyone point me in the right direction please?

2

I believe you're looking for something like:

$purchasable->product->yourEntriesFieldHandle->one()->title
| improve this answer | |
  • Yes. Brilliant. Thank you. I'd love to know 'how' you know that? I've not been able to find this indicated in the docs, and of I take a look at the $purchasable object I don't see 'product' as an item. Cheers though! – Mere Development Jul 24 at 21:29
  • 1
    I knew this because $purchasable is an instance of a Variant, and all variants have a parent product. If you dump out $purchasable (Craft::dd($purchasable);) you can see that it's a Variant and has a nested object representing the Product. Here's the relevant part of the docs: docs.craftcms.com/commerce/api/v3/… – James Smith Jul 27 at 8:55

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.