<p>You can't get the related categories from the product type. I think you want to get the category IDs from the relationship field on the variant or purchasable:</p>
<pre class="lang-php prettyprint-override"><code>if($purchasable = $lineItem-&gt;getPurchasable())
{
    if($purchasable instanceof Variant){
        $categoryIdsRelatedToVariant = $purchasable-&gt;categoryRelationFieldNameHere-&gt;ids();
        $categoryIdsRelatedToProduct = $purchasable-&gt;getProduct()-&gt;categoryRelationFieldNameHere-&gt;ids();
    }
}
</code></pre>

CC BY-SA 4.0
2023-12-11T07:20:18.247Z
Thanks, will try that out later. I have different category relation fields (some product have these, some these). I do have to do that then for every category relation field and then combine these together, right?
Henrik
CC BY-SA 4.0
2023-12-11T21:54:57.217Z
Cannot get it working. If i Craft::dd($purchasable); or lineItem, the Category Field is always null. categoryRelationFieldNameHere should be the fieldhandle of the category field?
I tried also to get the field like this, but no luck:

$catDeliField = Craft::$app->getFields()>getFieldByHandle('categoryFieldHandle');

$catFieldFieldColumn = ElementHelper::fieldColumnFromField($catDeliField);
Henrik
CC BY-SA 4.0
2023-12-11T04:07:11.100Z
2023-12-11T04:07:11.100Z
Luke Holder
<p>I want to add an adjustment to a product in the basket via an adjuster,
only when the product has certain categories.
(10% off if there are more than 6 same items in the basket. It's not possible to do this via the backend of commerce right now, unless I do it for every single product. But there are so many and want to do it once and for all).</p>
<p>I put a light switch on the categories. If the light switch is on, I want to compare the categories of the lineItem in the basket to the categories where the discount can be applied for.</p>
<p>So far I have the categories with which there is a discount possible in an array ($allowedCategoryIds), but I have trouble of getting the categories ID of a lineItem.
(-&gt; $categoryIds).</p>
<pre><code>foreach ($order-&gt;lineItems as $lineItem) {

        // Get the categories associated with the line Item
        $categories = $lineItem-&gt;getProductType()-&gt;getCategories();

        // Loop through the categories and get their IDs
        $categoryIds = [];
        foreach ($categories as $category) {
          $categoryIds[] = $category-&gt;id;
        }
        
        if (in_array($categoryIds, $allowedCategoryIds)) {

// apply the adjustment code //
}

</code></pre>
<p><strong>EDIT</strong></p>
<p>i changed my plan and put a lightswitch on a product, deciding if the product can be get the discount adjusment or not.
in my adjuster i checked if the lightswitch is set or not.
For me its more futureproof, as there could be more categories on a product, which might interfere each other (as one can have a discount and the other one not)</p>
<pre><code>if ($lineItem-&gt;lightSwitchHandle) {
...
}
</code></pre>

<plugin-craftcommerce><adjusters>
How to get category id of lineItem to use in commerce adjuster
# How to get category id of lineItem to use in commerce adjuster

(Asked by: username_0 on 2023-12-10)

I want to add an adjustment to a product in the basket via an adjuster,
only when the product has certain categories.
(10% off if there are more than 6 same items in the basket. It's not possible to do this via the backend of commerce right now, unless I do it for every single product. But there are so many and want to do it once and for all).
I put a light switch on the categories. If the light switch is on, I want to compare the categories of the lineItem in the basket to the categories where the discount can be applied for.
So far I have the categories with which there is a discount possible in an array ($allowedCategoryIds), but I have trouble of getting the categories ID of a lineItem.
(-> $categoryIds).
foreach ($order->lineItems as $lineItem) {

        // Get the categories associated with the line Item
        $categories = $lineItem->getProductType()->getCategories();

        // Loop through the categories and get their IDs
        $categoryIds = [];
        foreach ($categories as $category) {
          $categoryIds[] = $category->id;
        }
        
        if (in_array($categoryIds, $allowedCategoryIds)) {

// apply the adjustment code //
}


EDIT
i changed my plan and put a lightswitch on a product, deciding if the product can be get the discount adjusment or not.
in my adjuster i checked if the lightswitch is set or not.
For me its more futureproof, as there could be more categories on a product, which might interfere each other (as one can have a discount and the other one not)
if ($lineItem->lightSwitchHandle) {
...
}

**Tags:** plugin-craftcommerce, adjusters


---

## 1 answers

### Answer by username_1 on 2023-12-11. Score: 1

You can't get the related categories from the product type. I think you want to get the category IDs from the relationship field on the variant or purchasable:
if($purchasable = $lineItem->getPurchasable())
{
    if($purchasable instanceof Variant){
        $categoryIdsRelatedToVariant = $purchasable->categoryRelationFieldNameHere->ids();
        $categoryIdsRelatedToProduct = $purchasable->getProduct()->categoryRelationFieldNameHere->ids();
    }
}

> **Comments:**
> **username_0**: Thanks, will try that out later. I have different category relation fields (some product have these, some these). I do have to do that then for every category relation field and then combine these together, right?  
> **username_0**: Cannot get it working. If i Craft::dd($purchasable); or lineItem, the Category Field is always null. categoryRelationFieldNameHere should be the fieldhandle of the category field? I tried also to get the field like this, but no luck:  $catDeliField = Craft::$app->getFields()>getFieldByHandle('categoryFieldHandle');  $catFieldFieldColumn = ElementHelper::fieldColumnFromField($catDeliField);  

---

