Magento 2:如何在产品列表中配置价格

时间:2016-04-26 04:04:08

标签: attributes magento2 configurable-product price

如何获得价格产品

简单的产品:

$oldPrice= $_product->getPrice(); $newPrice= $_product->getSpecialPrice();

它会返回正确的结果

使用可配置的产品:

$oldPrice= $_product->getPrice(); //return Null $newPrice= $_product->getSpecialPrice(); // return Null

我有1个可配置的产品和2个简单的产品

TestCon1  Simple1:价格120 $。特价:100美元

 simple2:价格120 $。特价:90美元

我需要得到可配置回报的价格:TestCon1价格120 $。特价:90美元。

我使用$oldpriceConf= $_product->getFinalPrice(); // retunr 100$

2 个答案:

答案 0 :(得分:2)

可配置产品没有任何正常价格或特价。 在前端,他们的儿童产品的价格被视为他们的正常价格。

答案 1 :(得分:0)

在Magento 2中查看此文件:

vendor\magento\module-catalog\Block\Product\ListProduct.php

它包含2种可以粘贴到块中的方法:

/**
 * @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); ?>

这很好地显示了预先格式化的价格和销售价格,而旧价格却被划掉了。

enter image description here

相关问题