Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Created October 17, 2019 14:40
Show Gist options
  • Save cgi-caesar/c150509c69b7509d1c5c6726941c5244 to your computer and use it in GitHub Desktop.
Save cgi-caesar/c150509c69b7509d1c5c6726941c5244 to your computer and use it in GitHub Desktop.
aMember (site.php): Discount based on Qty
<?php
Am_Di::getInstance()->hook->add(Am_Event::INVOICE_GET_CALCULATORS, function(Am_Event_InvoiceGetCalculators $e){
$e->insertBeforeTax(new class() extends Am_Invoice_Calc
{
public function calculate(Invoice $invoiceBill)
{
$discount_rules = [
108 => [ //Product ID
1000 => 40, //Discount Levels
500 => 25,
200 => 20,
]
];
foreach ($invoiceBill->getItems() as $item)
{
if ($item->item_type == 'product'
&& isset($discount_rules[$item->item_id])
) {
$qty = $item->qty;
$discount = 0;
foreach ($discount_rules[$item->item_id] as $threshold => $percent) {
if ($qty > $threshold) {
$discount += ($qty - $threshold) * $item->first_price * $percent / 100;
$qty -= ($qty - $threshold);
}
}
if ($discount > 0) {
$item->first_discount = $discount;
}
}
}
foreach ($invoiceBill->getItems() as $item)
{
$item->_calculateTotal();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment