Answers.Body: <p>I am pretty sure it's the same for a product as it is for any other Craft element, like an entry.</p>
  
  <p>To get simple fields:</p>
  
  <p><code>$lineItem-&gt;purchasable-&gt;product-&gt;myCustomFieldHandle</code></p>
  
  <p>To handle matrix fields, just loop through the matrix blocks:</p>
  
  <pre><code>$matrixBlocks = $lineItem-&gt;purchasable-&gt;product-&gt;matrixFieldHandle; 
  foreach ($matrixBlocks as $block){
      echo $block-&gt;id;
      echo $block-&gt;fieldHandle;
      if ($block-&gt;fieldHandle == "whatever"){
            etc
      }
  }
  </code></pre>
  
  <p>Certainly that works with SuperTable fields on a product, pretty sure it's the same with matrix fields.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-18T05:59:40.360Z
Answers.LastActivityDate: 2016-06-18T06:15:41.580Z
Answers.LastEditDate: 2016-06-18T06:15:41.580Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I'm writing a plugin for Craft Commerece that performs action on <code>commerce_orders.onOrderComplete</code></p>
  
  <p>I need to check the value of product custom field (Matrix) in order to determine action parameters.</p>
  
  <p>I tried to use <code>$lineItem-&gt;purchasable-&gt;product-&gt;myCustomFieldHandle</code>, but this returns me <code>ElementCriteriaModel</code> without any data entered for product in this field.</p>
  
  <p>Any ideas on how to get actual data saved in custom field for product?</p>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: Get product custom fields in plugin
ThreadText: # Get product custom fields in plugin
  
  (Asked by: username_0 on 2016-06-17)
  
  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?
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-06-18. Score: 1 | Accepted answer
  
  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.
  
  
  ---
  
  