Magento2 get price of product

Posted by

Magento2 获取产品的各种价格方法如下:

<?php
namespace Yshuq\Test\Controller\Index;

class Price extends \Magento\Framework\App\Action\Action
{
    protected $_pageFactory;
    protected $_productFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Catalog\Model\ProductFactory $productFactory)
    {
        $this->_pageFactory = $pageFactory;
        $this->_productFactory = $productFactory;
        return parent::__construct($context);
    }

    public function execute()
    {
        $sku = 'HG61U0937';
        $product = $this->_productFactory->create();
        $productPriceBySku = $product->loadByAttribute('sku', $sku)->getPrice();
      // echo   $regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getValue();
      //echo   $specialPrice = $product->getPriceInfo()->getPrice('special_price')->getValue();
      //获取产品原价
        echo "get product<br> ";
        $originalPrice =  $product->loadByAttribute('sku', $sku)->getPrice();
        echo "original prices:"; print_r($originalPrice);
       
        //获取产品特价
        $specialPrice =  $product->loadByAttribute('sku', $sku)->getSpecialPrice();
        echo "<br>special prices:"; print_r($specialPrice);
        //获取产品最终显示并且使用的价格
        $finalPrice =  $product->loadByAttribute('sku', $sku)->getFinalPrice();
        echo "<br>final price:";
        print_r($finalPrice);
        exit;
    }
}
产品价格信息

注意:Catalog Price Rule 价格规则里面针对商品价格的设置。