Skip to main content
added 606 characters in body
Source Link
Rohan Hapani
  • 17.5k
  • 9
  • 55
  • 96

Use \Magento\Catalog\Model\ProductFactory class to get price of product

=> Factory Method :

protected $_product;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    $this->productFactory = $productFactory;
}

public function getPriceById($id)
{
    //$id = '21'; //Product ID
    $product = $this->productFactory->create();
    $productPriceById = $product->load($id)->getPrice();
    return $productPriceById;
}

public function getPriceBySku($sku)
{   
    //$sku = 'testing'; //Product sku
    $product = $this->productFactory->create();
    $productPriceBySku = $product->loadByAttribute('sku', $sku)->getPrice();
    return $productPriceBySku;
}

=> Object Manager Method :

BY Id :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->load(your-product-id);
$productPriceById = $productCollection->getPrice();

By Sku :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', your-sku);
$productPriceBySku = $productCollection->getPrice();

Use \Magento\Catalog\Model\ProductFactory class to get price of product

protected $_product;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    $this->productFactory = $productFactory;
}

public function getPriceById($id)
{
    //$id = '21'; //Product ID
    $product = $this->productFactory->create();
    $productPriceById = $product->load($id);
    return $productPriceById;
}

public function getPriceBySku($sku)
{   
    //$sku = 'testing'; //Product sku
    $product = $this->productFactory->create();
    $productPriceBySku = $product->loadByAttribute('sku', $sku);
    return $productPriceBySku;
}

Use \Magento\Catalog\Model\ProductFactory class to get price of product

=> Factory Method :

protected $_product;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    $this->productFactory = $productFactory;
}

public function getPriceById($id)
{
    //$id = '21'; //Product ID
    $product = $this->productFactory->create();
    $productPriceById = $product->load($id)->getPrice();
    return $productPriceById;
}

public function getPriceBySku($sku)
{   
    //$sku = 'testing'; //Product sku
    $product = $this->productFactory->create();
    $productPriceBySku = $product->loadByAttribute('sku', $sku)->getPrice();
    return $productPriceBySku;
}

=> Object Manager Method :

BY Id :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->load(your-product-id);
$productPriceById = $productCollection->getPrice();

By Sku :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', your-sku);
$productPriceBySku = $productCollection->getPrice();
Source Link
Rohan Hapani
  • 17.5k
  • 9
  • 55
  • 96

Use \Magento\Catalog\Model\ProductFactory class to get price of product

protected $_product;

public function __construct(
    ...      
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    $this->productFactory = $productFactory;
}

public function getPriceById($id)
{
    //$id = '21'; //Product ID
    $product = $this->productFactory->create();
    $productPriceById = $product->load($id);
    return $productPriceById;
}

public function getPriceBySku($sku)
{   
    //$sku = 'testing'; //Product sku
    $product = $this->productFactory->create();
    $productPriceBySku = $product->loadByAttribute('sku', $sku);
    return $productPriceBySku;
}