Answers.Body: <p>The method to check the previous orders would look something like this:</p>
  
  <pre><code>public function checkIfDonated($order)
  {
  
      $email = $order-&gt;getEmail();
  
      if (!$email) {
          return false;
      }
  
      $orders = Plugin::getInstance()-&gt;getOrders()-&gt;getOrdersByEmail($email);
  
      $didDonate = false;
      foreach ($orders as $previousOrder) {
          foreach ($previousOrder-&gt;getLineItems() as $lineItem) {
              $purchable = $lineItem-&gt;getPurchasable();
              if ($purchable &amp;&amp; $purchable instanceof \craft\commerce\elements\Variant &amp;&amp; $purchasable-&gt;getProduct()-&gt;getType()-&gt;handle == 'typeA') {
                  $didDonate = true;
              }
          }
      }
  
      return $didDonate;
  }
  </code></pre>
  
  <p>That above assumes you want to look at all orders made by anyone with that email address. You could add additional guards after the email check to see if they are logged in as a user etc. Hope that helps.</p>
  
  <p>Also, don't forget to pass the order to the method: <code>if ($this-&gt;checkIfDonated($order) == true)</code></p>
  
  <p>And to add the Plugin import: <code>use craft\commerce\Plugin;</code> to the top of the adjuster class.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-27T17:36:19.423Z
Answers.Text: There is a missing equal in product type comparison, wasn't allowed to edit it :-)
Answers.UserDisplayName: HendrikEng
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-26T01:22:32.133Z
Answers.LastActivityDate: 2018-06-28T15:48:11.940Z
Answers.LastEditDate: 2018-06-28T15:48:11.940Z
Answers.UserDisplayName: Luke Holder
Body: <p>I am trying to write a Commerce 2 adjuster that will apply a discount on all items from a specific type if a customer bought a specific item with another product type in the last year as well as a minimum price.</p>
  
  <p>It's my first attempt and I'm already stuck trying to retrieve the order history from a specific user.  I can't find anything in the Commerce 2 docs. That's what I have so far. Please be gentle :-)</p>
  
  <pre><code>&lt;?php
  namespace craft\commerce\adjusters;
  
  use Craft;
  use craft\commerce\base\AdjusterInterface;
  use craft\commerce\elements\Order;
  use craft\commerce\models\OrderAdjustment;
  
  class FriendDiscount implements AdjusterInterface
  {
  
      // get the current user and check if the discount condition is true
      public function checkIfDonated()
      {
          $user = Craft::$app-&gt;getUser();
          if ($user) {
              // check if user bought line item with product type 'typeA' in the last 365 days and spent at least 100$ on it, then return true
              return true;
          }
          return [];
      }
  
      public function adjust(Order $order): array
      {
  
          $friendAdjuster = new OrderAdjustment();
  
          // apply discount if checkIfDonated() returned true
          if ($this-&gt;checkIfDonated() == true) {
              // apply discount only on items with the product type 'typeB'
              $friendAdjuster-&gt;type = "Discount";
              $friendAdjuster-&gt;name = "-14$ Off";
              $friendAdjuster-&gt;description = "-14$";
              $friendAdjuster-&gt;amount = -14;
              $friendAdjuster-&gt;orderId = $order-&gt;id;
              return [$friendAdjuster];
          }
          return [];
  
      }
  
  }
  </code></pre>
  
Tags: <plugin-development><craft3><plugins><plugin-craftcommerce2>
Title: Getting previous order history in a Commerce 2 adjuster
ThreadText: # Getting previous order history in a Commerce 2 adjuster
  
  (Asked by: username_0 on 2018-06-25)
  
  I am trying to write a Commerce 2 adjuster that will apply a discount on all items from a specific type if a customer bought a specific item with another product type in the last year as well as a minimum price.
  
  It's my first attempt and I'm already stuck trying to retrieve the order history from a specific user. I can't find anything in the Commerce 2 docs. That's what I have so far. Please be gentle :-)
  
  ```
  <?php
  namespace craft\commerce\adjusters;
  
  use Craft;
  use craft\commerce\base\AdjusterInterface;
  use craft\commerce\elements\Order;
  use craft\commerce\models\OrderAdjustment;
  
  class FriendDiscount implements AdjusterInterface
  {
  
      // get the current user and check if the discount condition is true
      public function checkIfDonated()
      {
          $user = Craft::$app->getUser();
          if ($user) {
              // check if user bought line item with product type 'typeA' in the last 365 days and spent at least 100$ on it, then return true
              return true;
          }
          return [];
      }
  
      public function adjust(Order $order): array
      {
  
          $friendAdjuster = new OrderAdjustment();
  
          // apply discount if checkIfDonated() returned true
          if ($this->checkIfDonated() == true) {
              // apply discount only on items with the product type 'typeB'
              $friendAdjuster->type = "Discount";
              $friendAdjuster->name = "-14$ Off";
              $friendAdjuster->description = "-14$";
              $friendAdjuster->amount = -14;
              $friendAdjuster->orderId = $order->id;
              return [$friendAdjuster];
          }
          return [];
  
      }
  
  }
  
  ```
  
  **Tags:** plugin-development, craft3, plugins, plugin-craftcommerce2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-26. Score: 2 | Accepted answer
  
  The method to check the previous orders would look something like this:
  
  ```
  public function checkIfDonated($order)
  {
  
      $email = $order->getEmail();
  
      if (!$email) {
          return false;
      }
  
      $orders = Plugin::getInstance()->getOrders()->getOrdersByEmail($email);
  
      $didDonate = false;
      foreach ($orders as $previousOrder) {
          foreach ($previousOrder->getLineItems() as $lineItem) {
              $purchable = $lineItem->getPurchasable();
              if ($purchable && $purchable instanceof \craft\commerce\elements\Variant && $purchasable->getProduct()->getType()->handle == 'typeA') {
                  $didDonate = true;
              }
          }
      }
  
      return $didDonate;
  }
  
  ```
  
  That above assumes you want to look at all orders made by anyone with that email address. You could add additional guards after the email check to see if they are logged in as a user etc. Hope that helps.
  
  Also, don't forget to pass the order to the method: `if ($this->checkIfDonated($order) == true)`
  
  And to add the Plugin import: `use craft\commerce\Plugin;` to the top of the adjuster class.
  
  > **Comments:**
  > **username_0**: There is a missing equal in product type comparison, wasn't allowed to edit it :-)  
  
  ---
  
  