Skip to content

Commit

Permalink
Fix tier price calculation when using discount
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin-Magmodules committed Dec 5, 2024
1 parent b7fc265 commit eca775f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
63 changes: 39 additions & 24 deletions Service/Product/PriceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,48 +290,63 @@ public function processPrice(Product $product, $price, array $config, ?bool $inc
}

/**
* @param $price
* @param $config
* Format a price based on the provided configuration.
*
* @return string
* @param float|string $price The price value to be formatted.
* @param array $config Configuration options for formatting.
* @return string The formatted price.
*/
public function formatPrice($price, $config): string
public function formatPrice($price, array $config): string
{
$decimal = isset($config['decimal_point']) ? $config['decimal_point'] : '.';
$price = number_format(floatval(str_replace(',', '.', (string)$price)), 2, $decimal, '');
if (!empty($config['use_currency']) && ($price >= 0)) {
$price .= ' ' . $config['currency'];
$decimalPoint = $config['decimal_point'] ?? '.';
$currency = $config['currency'] ?? '';
$useCurrency = !empty($config['use_currency']);

// Ensure the price is converted to a float.
$formattedPrice = number_format(
floatval(str_replace(',', '.', (string)$price)),
2,
$decimalPoint,
''
);

// Append currency if configured.
if ($useCurrency && $formattedPrice >= 0) {
$formattedPrice .= ' ' . $currency;
}
return $price;

return $formattedPrice;
}

/**
* @param Product $product
* @param array $config
* @return array|null
* Process tier pricing for a product.
*
* @param Product $product The product instance.
* @param array $config Configuration for price formatting.
* @return array|null An array of reformatted tier prices or null if none exist.
*/
public function processTierPrice(Product $product, array $config): ?array
{
if (!$product->getData('tier_price')) {
if (!$tierPrices = $product->getData('tier_price')) {
return null;
}

$reformattedTierPriced = [];
foreach ($product->getData('tier_price') as $priceTier) {
$price = $priceTier['percentage_value']
? $product->getPrice() * ($priceTier['percentage_value'] / 100)
$reformattedTierPrices = [];
foreach ($tierPrices as $priceTier) {
$price = !empty($priceTier['percentage_value'])
? $product->getPrice() * (1 - ($priceTier['percentage_value'] / 100)) // Subtract discount
: $priceTier['value'];

$reformattedTierPriced[] = [
'price_id' => $priceTier['value_id'],
'website_id' => $priceTier['website_id'],
'all_groups' => $priceTier['all_groups'],
'cust_group' => $priceTier['customer_group_id'],
'qty' => $priceTier['qty'],
$reformattedTierPrices[] = [
'price_id' => $priceTier['value_id'] ?? null,
'website_id' => $priceTier['website_id'] ?? null,
'all_groups' => $priceTier['all_groups'] ?? 0,
'cust_group' => $priceTier['customer_group_id'] ?? null,
'qty' => $priceTier['qty'] ?? 0,
'price' => $this->formatPrice($price, $config)
];
}

return $reformattedTierPriced;
return $reformattedTierPrices;
}
}
1 change: 1 addition & 0 deletions Service/Product/TierPriceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct(
/**
* @param ProductCollection $products
* @param ProductCollection $parents
* @param $websiteId
* @return void
*/
public function load(ProductCollection $products, ProductCollection $parents, $websiteId)
Expand Down

0 comments on commit eca775f

Please sign in to comment.