Example #1
0
    /**
     * Retrieve Amount object based on given float amount, product and exclude option.
     * It is possible to pass "true" or adjustment code to exclude all or specific adjustment from an amount.
     *
     * @param float|string $amount
     * @param SaleableInterface $saleableItem
     * @param null|bool|string $exclude
     * @param null|array $context
     * @return \Magento\Framework\Pricing\Amount\AmountInterface
     */
    public function getAmount($amount, SaleableInterface $saleableItem, $exclude = null, $context = [])
    {
        $baseAmount = $fullAmount = $amount;
        $previousAdjustments = 0;
        $adjustments = [];
        foreach ($saleableItem->getPriceInfo()->getAdjustments() as $adjustment) {
            $code = $adjustment->getAdjustmentCode();
            $toExclude = false;
            if ($exclude === true || ($exclude !== null && $code === $exclude)) {
                $toExclude = true;
            }
            if ($adjustment->isIncludedInBasePrice()) {
                $adjust = $adjustment->extractAdjustment($baseAmount, $saleableItem, $context);
                $baseAmount -= $adjust;
                $fullAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context);
                $adjust = $fullAmount - $baseAmount - $previousAdjustments;
                if (!$toExclude) {
                    $adjustments[$code] = $adjust;
                }
            } elseif ($adjustment->isIncludedInDisplayPrice($saleableItem)) {
                if ($toExclude) {
                    continue;
                }
                $newAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem, $context);
                $adjust = $newAmount - $fullAmount;
                $adjustments[$code] = $adjust;
                $fullAmount = $newAmount;
                $previousAdjustments += $adjust;
            }
        }

        return $this->amountFactory->create($fullAmount, $adjustments);
    }
 /**
  * Create amount renderer
  *
  * @param string $priceCode
  * @param SaleableInterface $saleableItem
  * @param array $data
  * @throws \InvalidArgumentException
  * @return PriceBoxRenderInterface
  */
 public function createPriceRender($priceCode, SaleableInterface $saleableItem, array $data = [])
 {
     $type = $saleableItem->getTypeId();
     // implement class resolving fallback
     $pattern = [$type . '/prices/' . $priceCode . '/render_class', $type . '/default_render_class', 'default/prices/' . $priceCode . '/render_class', 'default/default_render_class'];
     $renderClassName = $this->findDataByPattern($pattern);
     if (!$renderClassName) {
         throw new \InvalidArgumentException('Class name for price code "' . $priceCode . '" not registered');
     }
     $price = $saleableItem->getPriceInfo()->getPrice($priceCode);
     if (!$price) {
         throw new \InvalidArgumentException('Price model for price code "' . $priceCode . '" not registered');
     }
     $arguments['data'] = $data;
     $arguments['rendererPool'] = $this;
     $arguments['price'] = $price;
     $arguments['saleableItem'] = $saleableItem;
     /** @var \Magento\Framework\View\Element\Template $renderBlock */
     $renderBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments);
     if (!$renderBlock instanceof PriceBoxRenderInterface) {
         throw new \InvalidArgumentException('Block "' . $renderClassName . '" must implement \\Magento\\Framework\\Pricing\\Render\\PriceBoxRenderInterface');
     }
     $renderBlock->setTemplate($this->getRenderBlockTemplate($type, $priceCode));
     return $renderBlock;
 }
 /**
  * @param SaleableInterface $saleableItem
  * @param float $quantity
  * @param CalculatorInterface $calculator
  * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  */
 public function __construct(SaleableInterface $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency)
 {
     $this->product = $saleableItem;
     $this->quantity = $quantity;
     $this->calculator = $calculator;
     $this->priceCurrency = $priceCurrency;
     $this->priceInfo = $saleableItem->getPriceInfo();
 }
 /**
  * @param \Magento\Framework\Pricing\SaleableInterface $product
  * @return float
  */
 public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)
 {
     return $product->getPriceInfo()->getPrice(\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE)->getAmount()->getValue();
 }
Example #5
0
 /**
  * Retrieve price object of given type and quantity
  *
  * @param string $priceCode
  * @return PriceInterface
  */
 public function getPriceType($priceCode)
 {
     return $this->saleableItem->getPriceInfo()->getPrice($priceCode);
 }