Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-11T07:20:18.247Z
Answers.Text: 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?
Answers.UserDisplayName: Henrik
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-11T21:54:57.217Z
Answers.Text: 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);
Answers.UserDisplayName: Henrik
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-11T04:07:11.100Z
Answers.LastActivityDate: 2023-12-11T04:07:11.100Z
Answers.UserDisplayName: Luke Holder
Body: <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>
  
Tags: <plugin-craftcommerce><adjusters>
Title: How to get category id of lineItem to use in commerce adjuster
ThreadText: # 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:
  
  ```php
  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);  
  
  ---
  
  