Skip to content

Instantly share code, notes, and snippets.

@DominicWatts
Last active July 22, 2024 14:31
Show Gist options
  • Save DominicWatts/81cc54d74b35d29fd092a32d6fab19b9 to your computer and use it in GitHub Desktop.
Save DominicWatts/81cc54d74b35d29fd092a32d6fab19b9 to your computer and use it in GitHub Desktop.
Magento 2 Prices

Tier price in phtml

<?php
$priceInfo = $_product->getPriceInfo();
$tierPrices = $priceInfo->getPrice('tier_price')->getTierPriceList();
$productPriceAmount = $priceInfo->getPrice('final_price')->getAmount();
?>
<?php if (count($tierPrices)) : ?>
    <ul class="prices-tier items">
        <?php foreach ($tierPrices as $index => $price) : ?>
            <li class="item">
                <?= /* @noEscape */ __(
                    'Buy %1 for %2 each and <strong class="benefit">save<span class="percent tier-%3">&nbsp;%4</span>%</strong>',
                    $price['price_qty'],
                    $this->helper('Magento\Framework\Pricing\Helper\Data')->currency($price['website_price'], true, false),
                    $index,
                    '<strong class="benefit">' . round(100 - ((100 / $productPriceAmount->getValue()) * $price['website_price'])) . '</strong>'
                ); ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Quick price in phtml

/** @var \Magento\Catalog\Model\Product $product */
$helper = $this->helper(\Magento\Catalog\Helper\Data::class);    
$priceIncTax = $helper->getTaxPrice(
    $product,
    $product->getFinalPrice(),
    true
); 

Format

use Magento\Directory\Model\CurrencyFactory $currencyFactory 

protected $currencyFactory; 

public function __construct( 
    CurrencyFactory $currencyFactory 
) { 
    $this->currencyFactory = $currencyFactory; 
} 

/** 
 * Format price without symbol ['display'=>\Zend_Currency::NO_SYMBOL]
 * @param float $price 
 * @return float 
 */ 
public function formatPrice($price) 
{ 
    return $this->currencyFactory 
        ->create() 
        ->format( 
            $price, 
            ['symbol' => $this->getCurrencySymbol()], 
            false 
        ); 
} 

/** 
 * Get currency from loaded order 
 * @return void|string 
 */ 
public function getCurrencySymbol() 
{ 
    $order = $this->getOrder(); 

    if (!$order || ! $order->getId()) { 
        return null; 
    }       

    $currency = $this->currencyFactory 
        ->create() 
        ->load($order->getOrderCurrency());       

    if (!$currency) { 
        return null; 
    }        

    return $currency->getCurrencySymbol();      
} 

Format

use Magento\Framework\Pricing\PriceCurrencyInterface as CurrencyInterface; 

protected $currencyInterface; 

public function __construct( 
    CurrencyInterface $currencyInterface 
) { 
    $this->currencyInterface = $currencyInterface; 
} 

public function formatPrice($price)
{
    return $this->currencyInterface->format( 
        $price, 
        false, 
        4  
    ); 
}

public function convertAndFormat($price)
{
    return $this->currencyInterface->convertAndFormat( 
        $price, 
        false, 
        4  
    );
}

public function convert($price)
{
    return $this->currencyInterface->convert($price);
}

Price block rendering

<?php

namespace Xigen\Category\Block;

use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\Framework\Pricing\Render;
use Magento\Catalog\Model\Product;

class Main extends \Magento\Framework\View\Element\Template
{
    /**
     * Specifies that price rendering should be done for the list of products.
     * (rendering happens in the scope of product list, but not single product)
     * @return Render
     */
    protected function getPriceRender()
    {
        $layout = $this->getLayout();
        $priceRender = $layout->getBlock('product.price.render.default');
        if (!$priceRender) {
            $priceRender =  $layout->createBlock(
                Render::class,
                'product.price.render.default',
                ['data' => ['price_render_handle' => 'catalog_product_prices']]
            );
        }
        $priceRender->setData('is_product_list', true);
        return $priceRender;
    }

    /**
     * Get product price.
     *
     * @param Product $product
     * @return string
     */
    public function getProductPrice(Product $product)
    {
        $priceRender = $this->getPriceRender();
        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                FinalPrice::PRICE_CODE,
                $product,
                [
                    'include_container' => true,
                    'display_minimal_price' => true,
                    'zone' => Render::ZONE_ITEM_LIST,
                    'list_category_page' => true
                ]
            );
        }
        return $price;
    }
}
[...]
$block->getProductPrice($product);
[...]

default rate

use Magento\Tax\Api\TaxCalculationInterface;
use Magento\Tax\Helper\Data as TaxHelper;

class TaxClass
{
    protected $taxCalculation;
    protected $taxHelper;

    public function __construct(
        TaxCalculationInterface $taxCalculation,
        TaxHelper $taxHelper
    ) {
        $this->taxCalculation = $taxCalculation;
        $this->taxHelper = $taxHelper;
    }
        $taxClassId = $this->taxHelper->getDefaultProductTaxClass($storeId);
        $rate = $this->taxCalculation->getDefaultCalculatedRate($taxClassId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment