Commerce: How do I use an adjustor for line items? 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-&gt;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-&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;
}


I have an adjustor set up...it's basically from this thread.

What I don't understand is where to actually put the adjustor.

Adjustor.php

$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];


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

    &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];

   }

}


...the main thing to note in there is you need to record the ids of any lineitems you're adjusting by changing this - ($myAdjuster-&gt;optionsJson = ['lineItemsAffected'=&gt;null];