如何获得价格产品
With simple product:$oldPrice= $_product->getPrice();
$newPrice= $_product->getSpecialPrice();
it returns the right results
With configuable product:
$oldPrice= $_product->getPrice(); //return Null
$newPrice= $_product->getSpecialPrice(); // return Null
I have 1 configuable product and 2 simple products
TestCon1
Simple1: Price 120$. Special price: 100$
simple2: Price 120$. Special price: 90$
我需要得到可配置回报的价格:测试1价格120美元。特价:90美元。
我用$oldpriceConf= $_product->getFinalPrice(); // retunr 100$
可配置产品没有任何正常价格或特殊价格。在前端,他们的儿童产品的价格被视为他们的正常价格。
在Magento 2中查看此文件:
vendor\magento\module-catalog\Block\Product\ListProduct.php
它包含两种可以粘贴到块中的方法:
/**
* @param \Magento\Catalog\Model\Product $product
* @return string
*/
public function getProductPrice(\Magento\Catalog\Model\Product $product)
{
$priceRender = $this->getPriceRender();
$price = '';
if ($priceRender) {
$price = $priceRender->render(
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
$product,
[
'include_container' => true,
'display_minimal_price' => true,
'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
'list_category_page' => true
]
);
}
return $price;
}
/**
* Specifies that price rendering should be done for the list of products
* i.e. rendering happens in the scope of product list, but not single product
*
* @return \Magento\Framework\Pricing\Render
*/
protected function getPriceRender()
{
return $this->getLayout()->getBlock('product.price.render.default')
->setData('is_product_list', true);
}
在.phtml模板文件中使用如下:
<?php echo $this->getProductPrice($product); ?>
这很好地显示预先格式化的价格和销售价格与旧的价格划掉。