Skip to content

Commit

Permalink
Merge pull request #9 from justbetter/feature/accept-decimal-qty-sort…
Browse files Browse the repository at this point in the history
…-customerprices-by-quantity

accept decimal qty and sort customerprices by quantity
  • Loading branch information
VincentBean authored Aug 2, 2024
2 parents 6984f87 + 4e38fd7 commit ae217d9
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/Actions/CustomerPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,24 @@ public function __construct(
) {
}

public function getCustomerPrice(int $productId, int $customerId, int $quantity = 1): ?float
public function getCustomerPrice(int $productId, int $customerId, float $quantity = 1): ?float
{
$this->customerPricingCollection
->clear()
->getSelect()
->reset(Zend_Db_Select::WHERE);
->reset(Zend_Db_Select::WHERE)
->reset(Zend_Db_Select::ORDER);

$customerPrices = $this->customerPricingCollection
->addFieldToFilter('product_id', ['eq' => $productId])
->addFieldToFilter('customer_id', ['eq' => $customerId])
->load()
->addFieldToFilter('quantity', ['lteq' => $quantity])
->setOrder('quantity')
->getItems();

usort(
$customerPrices,
fn (CustomerPricing $a, CustomerPricing $b): int => $a->getData('quantity') <=> $b->getData('quantity')
);

foreach ($customerPrices as $price) {
if ($quantity >= $price->getData('quantity')) {
return $price->getData('price');
if ($quantity >= (float) $price->getData('quantity')) {
return (float) $price->getData('price');
}
}

Expand Down

0 comments on commit ae217d9

Please sign in to comment.