TransWikia.com

Accessing Commerce ‘Product Fields’ in events hooks

Craft CMS Asked on January 1, 2022

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?

One Answer

I believe you're looking for something like:

$purchasable->product->yourEntriesFieldHandle->one()->title

Answered by James Smith on January 1, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP