<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>

CC BY-SA 3.0
2017-08-24T18:36:23.117Z
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!
JonnyT
CC BY-SA 3.0
2017-08-24T18:28:17.803Z
2017-08-24T18:28:17.803Z
Brad Bell
<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>

CC BY-SA 3.0
2017-08-24T23:33:04.717Z
2017-08-25T04:39:31.827Z
2017-08-25T04:39:31.827Z
Jeremy Daalder
<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>

<plugin-development><plugin-craftcommerce>
Get product type on event handler
# 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 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).
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
    }
}


---

