Answers.Body: <p>100% untested, but I'd probably go through the purchasable to get the product type.  Something like this should work:</p>
  
  <pre><code>$lineItem = $event-&gt;params['lineItem'];
  $purchasable = $lineItem-&gt;getPurchasable();
  $variant = $purchasable-&gt;getProduct();
  $productType = $variant-&gt;getType();
  </code></pre>
  
  <p><code>$productType</code> will be of type <code>commerce\models\Commerce_ProductTypeModel</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-24T18:36:23.117Z
Answers.Text: Excellent, I'll give that a go. I tried going through the Purchasable array but not as above. Definitely looks more like what I should be doing. Thanks!
Answers.UserDisplayName: JonnyT
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-24T18:28:17.803Z
Answers.LastActivityDate: 2017-08-24T18:28:17.803Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Very minor change to Brad's approach, and with a bit more checking to avoid issues like the product being deleted while the code is looking in that direction, etc. (based on Luke's advice).</p>
  
  <pre><code>foreach ($cart-&gt;lineItems as $item)
  {
      // check the purchasable has not been deleted in the meantime
      if ($item-&gt;purchasableId)
      {
          // get the variant and the actual product so we can get to custom fields
          $variant = $item-&gt;purchasable;
  
          // If there is no product associated with this purchasable, it's a digital virtual product so just skip it
          if (!isset($variant-&gt;product))
          {
              continue;
          }
  
          // get the actual product element from the purchasable
          $product = $variant-&gt;product;
  
          // How to get a regular field on the product
          $title = $product-&gt;title;
  
          // How to get the product type - use handle rather than name, as this is less likely to change
          $productType = $product-&gt;type-&gt;handle;
  
          ...etc
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-24T23:33:04.717Z
Answers.LastActivityDate: 2017-08-25T04:39:31.827Z
Answers.LastEditDate: 2017-08-25T04:39:31.827Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I'm working on a plugin to calculate price based on width and height but struggling to get the product type of each product into the 'onPopulateLineItem' event. </p>
  
  <p>So far, I've looked into the purchasables and lineItems arrays to get the information, and although I can see the typeId under the $event array, I just can't get this value. </p>
  
  <p>Can anyone point me in the right direction for this please?</p>
  
  <p>** UPDATE **</p>
  
  <p>I've managed to get something with the below, but this feels really hacky. Is there a better way I should be doing this?</p>
  
  <pre><code>$lineItem = $event-&gt;params['lineItem'];
  echo $lineItem['snapshot']['product']['typeId'];
  </code></pre>
  
Tags: <plugin-development><plugin-craftcommerce>
Title: Get product type on event handler
ThreadText: # Get product type on event handler
  
  (Asked by: username_0 on 2017-08-24)
  
  I'm working on a plugin to calculate price based on width and height but struggling to get the product type of each product into the 'onPopulateLineItem' event.
  
  So far, I've looked into the purchasables and lineItems arrays to get the information, and although I can see the typeId under the $event array, I just can't get this value.
  
  Can anyone point me in the right direction for this please?
  
  \*\* UPDATE \*\*
  
  I've managed to get something with the below, but this feels really hacky. Is there a better way I should be doing this?
  
  ```
  $lineItem = $event->params['lineItem'];
  echo $lineItem['snapshot']['product']['typeId'];
  
  ```
  
  **Tags:** plugin-development, plugin-craftcommerce
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-08-24. Score: 0 | Accepted answer
  
  100% untested, but I'd probably go through the purchasable to get the product type. Something like this should work:
  
  ```
  $lineItem = $event->params['lineItem'];
  $purchasable = $lineItem->getPurchasable();
  $variant = $purchasable->getProduct();
  $productType = $variant->getType();
  
  ```
  
  `$productType` will be of type `commerce\models\Commerce_ProductTypeModel`.
  
  > **Comments:**
  > **username_0**: Excellent, I'll give that a go. I tried going through the Purchasable array but not as above. Definitely looks more like what I should be doing. Thanks!  
  
  ---
  
  ### Answer by username_2 on 2017-08-24. Score: 2
  
  Very minor change to username_1's approach, and with a bit more checking to avoid issues like the product being deleted while the code is looking in that direction, etc. (based on Luke's advice).
  
  ```
  foreach ($cart->lineItems as $item)
  {
      // check the purchasable has not been deleted in the meantime
      if ($item->purchasableId)
      {
          // get the variant and the actual product so we can get to custom fields
          $variant = $item->purchasable;
  
          // If there is no product associated with this purchasable, it's a digital virtual product so just skip it
          if (!isset($variant->product))
          {
              continue;
          }
  
          // get the actual product element from the purchasable
          $product = $variant->product;
  
          // How to get a regular field on the product
          $title = $product->title;
  
          // How to get the product type - use handle rather than name, as this is less likely to change
          $productType = $product->type->handle;
  
          ...etc
      }
  }
  
  ```
  
  
  ---
  
  