示例#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
  * @return \Magento\Framework\Pricing\Amount\AmountInterface
  */
 public function getAmount($amount, SaleableInterface $saleableItem, $exclude = null)
 {
     $baseAmount = $fullAmount = $amount;
     $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);
             $baseAmount -= $adjust;
             $fullAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem);
             $adjust = $fullAmount - $baseAmount;
             if (!$toExclude) {
                 $adjustments[$code] = $adjust;
             }
         } elseif ($adjustment->isIncludedInDisplayPrice($saleableItem)) {
             if ($toExclude) {
                 continue;
             }
             $newAmount = $adjustment->applyAdjustment($fullAmount, $saleableItem);
             $adjust = $newAmount - $fullAmount;
             $adjustments[$code] = $adjust;
             $fullAmount = $newAmount;
         }
     }
     return $this->amountFactory->create($fullAmount, $adjustments);
 }
示例#2
0
 /**
  * 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;
 }
示例#3
0
 /**
  * @param SaleableInterface $saleableItem
  * @param float $quantity
  * @param CalculatorInterface $calculator
  */
 public function __construct(SaleableInterface $saleableItem, $quantity, CalculatorInterface $calculator)
 {
     $this->product = $saleableItem;
     $this->quantity = $quantity;
     $this->calculator = $calculator;
     $this->priceInfo = $saleableItem->getPriceInfo();
 }
 /**
  * @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();
 }
示例#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);
 }