【问题标题】:Magento 2:How get price configurable in product listMagento 2:如何在产品列表中配置价格
【发布时间】:2016-08-19 16:46:54
【问题描述】:

如何获得价格产品

使用简单的产品: $oldPrice= $_product->getPrice(); $newPrice= $_product->getSpecialPrice(); 它返回正确的结果 使用可配置产品: $oldPrice= $_product->getPrice(); //return Null $newPrice= $_product->getSpecialPrice(); // return Null 我有 1 个可配置产品和 2 个简单产品 测试Con1 简单 1:价格 120 美元。特价:100$ simple2:价格 120 美元。特价:90 美元

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

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

【问题讨论】:

  • 您找到解决方案了吗?
  • 如何获取子产品的价格

标签: attributes magento2 configurable-product price


【解决方案1】:

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

【讨论】:

  • 是的!我知道!你能帮我得到价格简单的可配置产品吗(Magento 2 是新的)。谢谢!
【解决方案2】:

看看 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); ?>

这可以很好地显示预先格式化的价格和带有划掉旧价格的销售价格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2022-11-11
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多