Answers.Body: <p>I'm finding the question hard to follow, but if I am understanding you, then yes I would do it all in the adjuster - rather than using a discount here... I don't see what using a Commerce discount here offers, as you can't really express your credits system in any way meaningful that relates to a Commerce discount I think.  </p>
  
  <p>So this indeed looks like a job for an adjuster, which can act at either the lineItem or order level as per the linked example.</p>
  
  <p>In terms of where you <em>put</em> that adjuster, there are basically three steps.  The first two are in your main plugin file:</p>
  
  <pre><code>public function init()
  {
    Craft::import('plugins.yourPluginHandle.adjusters.YourAdjusterClassName');
  }
  </code></pre>
  
  <p>and then later in that file...</p>
  
  <pre><code> public function commerce_registerOrderAdjusters(){
     return [
       new BusinessLogic_TaxRemover
     ];
   }
  </code></pre>
  
  <p>Then, you have the actual code in your adjuster to do whatever you want to do, which (repeating that example), looks like this:</p>
  
  <pre><code>    &lt;?php
  
      namespace Craft;
      use Craft\Commerce_LineItemModel;
      use Craft\Commerce_OrderAdjustmentModel;
      use Craft\Commerce_OrderModel;
  
      class BusinessLogic_TaxRemover implements Commerce_AdjusterInterface {
  
      public function adjust(Commerce_OrderModel &amp;$order, array $lineItems = []){
  
      $myAdjuster = new Commerce_OrderAdjustmentModel();
  
      $order-&gt;baseDiscount = $order-&gt;baseDiscount - 5;
  
      $myAdjuster-&gt;type = "Test";
      $myAdjuster-&gt;name = "Test Adjuster";
      $myAdjuster-&gt;description = "Takes $5 off the order";
      $myAdjuster-&gt;amount = -5.0;
      $myAdjuster-&gt;orderId = $order-&gt;id;
      //if your Adjuster affects lineItems rather than the total, you record the ids here
      $myAdjuster-&gt;optionsJson = ['lineItemsAffected'=&gt;null];
      $myAdjuster-&gt;included = false;
  
      return [$myAdjuster];
  
     }
  
  }
  </code></pre>
  
  <p>...the main thing to note in there is you need to record the ids of any lineitems you're adjusting by changing this - (<code>$myAdjuster-&gt;optionsJson = ['lineItemsAffected'=&gt;null];</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-15T06:10:48.033Z
Answers.LastActivityDate: 2017-04-05T11:19:48.260Z
Answers.LastEditDate: 2017-04-05T11:19:48.260Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>I have built a plugin that, <strong>TL;DR</strong> subtracts credits. Everything is working great - until I actually need to change the price. </p>
  
  <p>I just flat-out don't understand it.</p>
  
  <p>This is how I am looping through to check a user's credits using the <code>commerce_discounts.onBeforeMatchLineItem</code> event. You can see where I was using the discount <code>$event-&gt;performAction = false</code> method to stop the discount from being applied. But, I need the ability to split the line items so-to-speak. </p>
  
  <p>Example: qty of 5 of the same product; 3 are free, 2 are full price.</p>
  
  <pre><code>// $entry is the entry that keeps track how many credits are left.
  if ((int)$entry-&gt;events &gt; 0) {
      $i = (int)$entry-&gt;events;
  
      while ($i &gt; 0) {
          error_log('doing stuff...');
          $entry-&gt;setContentFromPost([
              'events' =&gt; ((int)$entry-&gt;events &gt; 0) ? ((int)$entry-&gt;events - (int)$i) : 0
              ]);
  
          craft()-&gt;entries-&gt;saveEntry($entry);
  
          $i--;
  
          if ($i == 0) {
              error_log('OUT OF CREDITS');
              // $event-&gt;performAction = false;
          }
      error_log((craft()-&gt;config-&gt;get('devMode')) ? 'CREDITS LEFT: ' . (int)$i : '');
      }
  } else {
      error_log((craft()-&gt;config-&gt;get('devMode')) ? 'Uh-oh user is out of event credits..' : '');
     $event-&gt;performAction = false;
  }
  </code></pre>
  
  <p>I have an adjustor set up...it's basically <a href="https://craftcms.stackexchange.com/questions/14106/3-for-2-etc-with-craft-commerce">from this thread</a>.</p>
  
  <p>What I don't understand is where to actually put the adjustor.</p>
  
  <p><strong>Adjustor.php</strong></p>
  
  <pre><code>$myAdjuster-&gt;amount = -5.0;  // How do I make the amount be 100%?
  $myAdjuster-&gt;orderId = $order-&gt;id;
      // Is this is where i need to loop through the line items and do the credit deductions?
  
  $myAdjuster-&gt;optionsJson = ['lineItemsAffected' =&gt; null];
  </code></pre>
  
  <p>Do I do all of the looping and such within the adjustor? Instead of a dollar amount, I would like 100% off until my credits are zero. Then the user pays full price.</p>
  
  <p>My apologies for such a large question. Any guidance is greatly appreciated!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-12-15T10:53:36.090Z
Comments.Text: I added a comment to the other thread, but I built this plugin which might do what you're looking for: https://craftpl.us/plugins/product-deals
Comments.UserDisplayName: Stephen Hamilton
Tags: <plugin-development><plugin-craftcommerce><adjusters>
Title: Commerce: How do I use an adjustor for line items?
ThreadText: # Commerce: How do I use an adjustor for line items?
  
  (Asked by: username_0 on 2016-12-14)
  
  I have built a plugin that, **TL;DR** subtracts credits. Everything is working great - until I actually need to change the price.
  
  I just flat-out don't understand it.
  
  This is how I am looping through to check a user's credits using the `commerce_discounts.onBeforeMatchLineItem` event. You can see where I was using the discount `$event->performAction = false` method to stop the discount from being applied. But, I need the ability to split the line items so-to-speak.
  
  Example: qty of 5 of the same product; 3 are free, 2 are full price.
  
  ```
  // $entry is the entry that keeps track how many credits are left.
  if ((int)$entry->events > 0) {
      $i = (int)$entry->events;
  
      while ($i > 0) {
          error_log('doing stuff...');
          $entry->setContentFromPost([
              'events' => ((int)$entry->events > 0) ? ((int)$entry->events - (int)$i) : 0
              ]);
  
          craft()->entries->saveEntry($entry);
  
          $i--;
  
          if ($i == 0) {
              error_log('OUT OF CREDITS');
              // $event->performAction = false;
          }
      error_log((craft()->config->get('devMode')) ? 'CREDITS LEFT: ' . (int)$i : '');
      }
  } else {
      error_log((craft()->config->get('devMode')) ? 'Uh-oh user is out of event credits..' : '');
     $event->performAction = false;
  }
  
  ```
  
  I have an adjustor set up...it's basically [from this thread](https://craftcms.stackexchange.com/questions/14106/3-for-2-etc-with-craft-commerce).
  
  What I don't understand is where to actually put the adjustor.
  
  **Adjustor.php**
  
  ```
  $myAdjuster->amount = -5.0;  // How do I make the amount be 100%?
  $myAdjuster->orderId = $order->id;
      // Is this is where i need to loop through the line items and do the credit deductions?
  
  $myAdjuster->optionsJson = ['lineItemsAffected' => null];
  
  ```
  
  Do I do all of the looping and such within the adjustor? Instead of a dollar amount, I would like 100% off until my credits are zero. Then the user pays full price.
  
  My apologies for such a large question. Any guidance is greatly appreciated!
  
  **Tags:** plugin-development, plugin-craftcommerce, adjusters
  
  > **Comments:**
  > **username_1**: I added a comment to the other thread, but I built this plugin which might do what you're looking for: https://craftpl.us/plugins/product-deals  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2016-12-15. Score: 1
  
  I'm finding the question hard to follow, but if I am understanding you, then yes I would do it all in the adjuster - rather than using a discount here... I don't see what using a Commerce discount here offers, as you can't really express your credits system in any way meaningful that relates to a Commerce discount I think.
  
  So this indeed looks like a job for an adjuster, which can act at either the lineItem or order level as per the linked example.
  
  In terms of where you *put* that adjuster, there are basically three steps. The first two are in your main plugin file:
  
  ```
  public function init()
  {
    Craft::import('plugins.yourPluginHandle.adjusters.YourAdjusterClassName');
  }
  
  ```
  
  and then later in that file...
  
  ```
   public function commerce_registerOrderAdjusters(){
     return [
       new BusinessLogic_TaxRemover
     ];
   }
  
  ```
  
  Then, you have the actual code in your adjuster to do whatever you want to do, which (repeating that example), looks like this:
  
  ```
      <?php
  
      namespace Craft;
      use Craft\Commerce_LineItemModel;
      use Craft\Commerce_OrderAdjustmentModel;
      use Craft\Commerce_OrderModel;
  
      class BusinessLogic_TaxRemover implements Commerce_AdjusterInterface {
  
      public function adjust(Commerce_OrderModel &$order, array $lineItems = []){
  
      $myAdjuster = new Commerce_OrderAdjustmentModel();
  
      $order->baseDiscount = $order->baseDiscount - 5;
  
      $myAdjuster->type = "Test";
      $myAdjuster->name = "Test Adjuster";
      $myAdjuster->description = "Takes $5 off the order";
      $myAdjuster->amount = -5.0;
      $myAdjuster->orderId = $order->id;
      //if your Adjuster affects lineItems rather than the total, you record the ids here
      $myAdjuster->optionsJson = ['lineItemsAffected'=>null];
      $myAdjuster->included = false;
  
      return [$myAdjuster];
  
     }
  
  }
  
  ```
  
  ...the main thing to note in there is you need to record the ids of any lineitems you're adjusting by changing this - (`$myAdjuster->optionsJson = ['lineItemsAffected'=>null];`
  
  
  ---
  
  