Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Block/AfterPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ public function getBasePrice()
{
return $this->_helper->getBasePriceText($this->getProduct());
}

/**
* Returns the base price for tier prices
* @return array
*/
public function getTierBasePrices(): array
{
return $this->_helper->getTierBasePricesText($this->getProduct(), true);
}
}
82 changes: 71 additions & 11 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ public function getConversion($product)
return 1;
}

private function getBasepriceLayoutTemplate(): string {
return $this->scopeConfig->getValue(
'baseprice/general/template',
ScopeInterface::SCOPE_STORE
);
}

/**
* Returns the base price text according to the configured template
*
Expand All @@ -99,30 +106,76 @@ public function getConversion($product)
*/
public function getBasePriceText(Product $product)
{
$template = $this->scopeConfig->getValue(
'baseprice/general/template',
ScopeInterface::SCOPE_STORE
);

$basePrice = $this->getBasePrice($product);

if (!$basePrice) return '';

return str_replace(
'{REF_UNIT}', $this->getReferenceUnit($product), str_replace(
'{REF_AMOUNT}', $this->getReferenceAmount($product), str_replace(
'{BASE_PRICE}', $this->priceHelper->currency($basePrice), $template)
'{BASE_PRICE}', $this->priceHelper->currency($basePrice), $this->getBasepriceLayoutTemplate())
));
}

/**
* Returns an array with the base prices for all tier prices. Uses the tier price id as index.
* @param Product $product
*
* @return array
*/
public function getTierBasePricesText(Product $product):array
{
$tierPricesTexts = [];
foreach ($product->getTierPrice() as $tier) {
$basePrice = $this->getBasePrice($product, $tier['price']);

if (!$basePrice) continue;

$tierPricesTexts[$tier['price_id']] = str_replace(
'{REF_UNIT}', $this->getReferenceUnit($product), str_replace(
'{REF_AMOUNT}', $this->getReferenceAmount($product), str_replace(
'{BASE_PRICE}', $this->priceHelper->currency($basePrice), $this->getBasepriceLayoutTemplate())
));
}

return $tierPricesTexts;
}

/**
* Returns the base price for a tier price by its ID.
* @param Product $product
* @param int $tierPriceID
*
* @return string
*/
public function getTierBasePriceText(Product $product, int $tierPriceID): string
{
foreach ($product->getTierPrice() as $tier) {
if( (int) $tier['price_id'] === $tierPriceID) {
$basePrice = $this->getBasePrice($product, $tier['price']);

if ( ! $basePrice) {
break;
}

return str_replace(
'{REF_UNIT}', $this->getReferenceUnit($product), str_replace(
'{REF_AMOUNT}', $this->getReferenceAmount($product), str_replace(
'{BASE_PRICE}', $this->priceHelper->currency($basePrice), $this->getBasepriceLayoutTemplate())
));
}
}
return '';
}

/**
* Returns the reference unit of current product
*
* @return string
*/
public function getReferenceUnit(Product $product)
{
return $product->getAttributeText('baseprice_reference_unit');
return __($product->getAttributeText('baseprice_reference_unit'));
}

/**
Expand All @@ -137,12 +190,19 @@ public function getReferenceAmount(Product $product)

/**
* Calculates the base price for given product
* @param Product $product
* @param float $amount Optional amount. Used to calculate the tier prices.
*
* @return float|string
* @return float
*/
public function getBasePrice(Product $product)
public function getBasePrice(Product $product, float $amount = 0)
{
$productPrice = round($product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue(), PriceCurrencyInterface::DEFAULT_PRECISION);
$price = $amount;
if($amount == 0) {
$price = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
}

$productPrice = round($price, PriceCurrencyInterface::DEFAULT_PRECISION);
$conversion = $this->getConversion($product);
$referenceAmount = $product->getData('baseprice_reference_amount');
$productAmount = $product->getData('baseprice_product_amount');
Expand All @@ -152,6 +212,6 @@ public function getBasePrice(Product $product)
$basePrice = $productPrice * $conversion * $referenceAmount / $productAmount;
}

return $basePrice;
return (float) $basePrice;
}
}