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