【问题标题】:Why doesn't the Price.php calculate final price correctly?为什么 Price.php 不能正确计算最终价格?
【发布时间】:2022-01-14 11:20:43
【问题描述】:

我尝试在插件中获取一个简单产品的最终价格。产品的正常价格为 12.99,但受目录价格规则 50% 的影响。 前端一切正常。它与 6.50 一起显示,但当我尝试在插件中使用它时,我总是得到正常价格。

我使用 vendor/magento/module-catalog/Model/Product/Type/Price::getFinalPrice($qty, $product) 方法:

private function getFinalPriceForMyPlugin(array $product): float 
{
   /** @var Product $productEntity */
   $productEntity = $this->productRepository->get((string)$product['sku']);
   return (float) $this->price->getFinalPrice(1, $productEntity);
}

我很确定 Price.php 负责计算最终价格。那么为什么它不起作用呢?

顺便说一句,缓存被刷新,索引器被重新索引

【问题讨论】:

    标签: php magento magento2


    【解决方案1】:

    您可以通过以下方式获取所有类型产品的正常价格和最终价格。

    1. 简单的产品

      $regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getValue(); $specialPrice = $product->getPriceInfo()->getPrice('special_price')->getValue();
      
    2. 可配置产品

      if ($product->getTypeId() == 'configurable') {
          $basePrice = $product->getPriceInfo()->getPrice('regular_price');
      
          $regularPrice = $basePrice->getMinRegularAmount()->getValue();
          $specialPrice = $product->getFinalPrice();
      }
      

    【讨论】:

    • 我试过了,但没有用。结果我总是得到0。原因是 Pricing/Price/SpecialPrice.php 中的 getValue() 方法。条件 $this->isScopeDateInInterval() 返回 false 因为(我认为)特价的有效时间范围超出了限制。
    • 但我觉得特价甚至不是我需要的。我正在寻找最终价格。最终价格为regular_price、catalog_rule_price、special_price和tier_price中最低的价格。
    • 例如,我的产品受目录价格规则影响。正常价格和特价都没有给我正确的价值。您可以在这里找到不同之处:Final Price 但我仍然不知道如何获得最终价格
    猜你喜欢
    • 2021-05-10
    • 2020-07-09
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多