1

I'm writing a plugin for Craft Commerece that performs action on commerce_orders.onOrderComplete

I need to check the value of product custom field (Matrix) in order to determine action parameters.

I tried to use $lineItem->purchasable->product->myCustomFieldHandle, but this returns me ElementCriteriaModel without any data entered for product in this field.

Any ideas on how to get actual data saved in custom field for product?

1

I am pretty sure it's the same for a product as it is for any other Craft element, like an entry.

To get simple fields:

$lineItem->purchasable->product->myCustomFieldHandle

To handle matrix fields, just loop through the matrix blocks:

$matrixBlocks = $lineItem->purchasable->product->matrixFieldHandle; 
foreach ($matrixBlocks as $block){
    echo $block->id;
    echo $block->fieldHandle;
    if ($block->fieldHandle == "whatever"){
          etc
    }
}

Certainly that works with SuperTable fields on a product, pretty sure it's the same with matrix fields.

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.