Answers.Body: <p><strong>MyPlugin.php</strong></p>
  
  <pre><code>$record = MyPluginRecord::model()-&gt;findByPk($user-&gt;id);
  
  if ($record) {
      $field = $record-&gt;getAttributes(); // The plugin model.
  
      if ($lineItems) {
          foreach ($lineItems as $lineItem) {
              foreach ($lineItem-&gt;purchasable-&gt;product-&gt;myCategoryField as $category) {
                  error_log($category-&gt;title); // My Category
              }
          }
      } else {
          // The cart is empty.
      }
  } else {
      // Product not found in cart.
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-27T03:18:47.243Z
Answers.LastActivityDate: 2016-05-27T03:18:47.243Z
Answers.UserDisplayName: Damon
Body: <p>I have a purchasable that has a categories field. When the user visits the cart, I inspect the cart via my plugin.</p>
  
  <p>I am able to see the field, but I am stumbling on how to actually check the values of the field (get the chosen categories).</p>
  
  <p><strong>MyPlugin</strong></p>
  
  <pre><code>...
  
  $lineItems = $cart-&gt;getLineItems();
  
  if ($lineItems) {
      foreach ($lineItems as $lineItem) {
          error_log(print_r($lineItem-&gt;purchasable, true));
      }
  } else {
      // The cart is empty.
  }
  
  ...
  
  
  ...
  [myCategoryField] =&gt; Array
      (
          [model] =&gt; 
          [column] =&gt; text
          [type] =&gt; mixed
          [label] =&gt; My Category
      )
  ...
  </code></pre>
  
  <p>If I go a step further into the field:</p>
  
  <pre><code>...
  error_log(print_r($lineItem-&gt;purchasable-&gt;myCategoryField, true));
  </code></pre>
  
  <p>I can get something like this:</p>
  
  <pre><code>[relatedTo] =&gt; Array
      (
          [sourceElement] =&gt; 174
          [sourceLocale] =&gt; en_us
          [field] =&gt; 25
      )
  </code></pre>
  
  <p>This is the correct field ID, but I'm not sure how to get to the values of the category field. For example "Category A", "Category B".</p>
  
  <p>Thank you for any suggestions!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-26T03:39:21.553Z
Comments.Text: I am pretty sure `$lineItem->purchasable->myCategoryField` returns a element criteria model, so you should be able to call `->find()` on it to get the categories.
Comments.UserDisplayName: Luke Holder
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-26T09:59:16.743Z
Comments.Text: Thanks Luke, I am most likely misunderstanding you. When I try `$lineItem->purchasable->campCategory->find()` I am getting back an empty array. Maybe I'm not on the right element after all.
Comments.UserDisplayName: Damon
Tags: <plugin-development><plugin-craftcommerce>
Title: Commerce - How can I get the values of a category field from a plugin?
ThreadText: # Commerce - How can I get the values of a category field from a plugin?
  
  (Asked by: username_0 on 2016-05-26)
  
  I have a purchasable that has a categories field. When the user visits the cart, I inspect the cart via my plugin.
  
  I am able to see the field, but I am stumbling on how to actually check the values of the field (get the chosen categories).
  
  **MyPlugin**
  
  ```
  ...
  
  $lineItems = $cart->getLineItems();
  
  if ($lineItems) {
      foreach ($lineItems as $lineItem) {
          error_log(print_r($lineItem->purchasable, true));
      }
  } else {
      // The cart is empty.
  }
  
  ...
  
  ...
  [myCategoryField] => Array
      (
          [model] => 
          [column] => text
          [type] => mixed
          [label] => My Category
      )
  ...
  
  ```
  
  If I go a step further into the field:
  
  ```
  ...
  error_log(print_r($lineItem->purchasable->myCategoryField, true));
  
  ```
  
  I can get something like this:
  
  ```
  [relatedTo] => Array
      (
          [sourceElement] => 174
          [sourceLocale] => en_us
          [field] => 25
      )
  
  ```
  
  This is the correct field ID, but I'm not sure how to get to the values of the category field. For example "Category A", "Category B".
  
  Thank you for any suggestions!
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  > **Comments:**
  > **username_1**: I am pretty sure `$lineItem->purchasable->myCategoryField` returns a element criteria model, so you should be able to call `->find()` on it to get the categories.  
  > **username_0**: Thanks username_1, I am most likely misunderstanding you. When I try `$lineItem->purchasable->campCategory->find()` I am getting back an empty array. Maybe I'm not on the right element after all.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-05-27. Score: 2
  
  **MyPlugin.php**
  
  ```
  $record = MyPluginRecord::model()->findByPk($user->id);
  
  if ($record) {
      $field = $record->getAttributes(); // The plugin model.
  
      if ($lineItems) {
          foreach ($lineItems as $lineItem) {
              foreach ($lineItem->purchasable->product->myCategoryField as $category) {
                  error_log($category->title); // My Category
              }
          }
      } else {
          // The cart is empty.
      }
  } else {
      // Product not found in cart.
  }
  
  ```
  
  
  ---
  
  