/**
  * Get the price value for one of selection product
  *
  * @return bool|float
  */
 public function getValue()
 {
     if (null !== $this->value) {
         return $this->value;
     }
     $priceCode = $this->useRegularPrice ? BundleRegularPrice::PRICE_CODE : FinalPrice::PRICE_CODE;
     if ($this->bundleProduct->getPriceType() == Price::PRICE_TYPE_DYNAMIC) {
         // just return whatever the product's value is
         $value = $this->priceInfo->getPrice($priceCode)->getValue();
     } else {
         // don't multiply by quantity.  Instead just keep as quantity = 1
         $selectionPriceValue = $this->selection->getSelectionPriceValue();
         if ($this->product->getSelectionPriceType()) {
             // calculate price for selection type percent
             $price = $this->bundleProduct->getPriceInfo()->getPrice(CatalogPrice\RegularPrice::PRICE_CODE)->getValue();
             $product = clone $this->bundleProduct;
             $product->setFinalPrice($price);
             $this->eventManager->dispatch('catalog_product_get_final_price', ['product' => $product, 'qty' => $this->bundleProduct->getQty()]);
             $value = $product->getData('final_price') * ($selectionPriceValue / 100);
         } else {
             // calculate price for selection type fixed
             $value = $this->priceCurrency->convert($selectionPriceValue) * $this->quantity;
         }
     }
     if (!$this->useRegularPrice) {
         $value = $this->discountCalculator->calculateDiscount($this->bundleProduct, $value);
     }
     $this->value = $this->priceCurrency->round($value);
     return $this->value;
 }
Example #2
0
 /**
  * @param Product $product
  * @param Product $bundle
  * @return Metadata
  */
 public function createDataFromModel(Product $product, Product $bundle)
 {
     $selectionPriceType = $selectionPrice = null;
     /** @var \Magento\Bundle\Model\Selection $product */
     if ($bundle->getPriceType()) {
         $selectionPriceType = $product->getSelectionPriceType();
         $selectionPrice = $product->getSelectionPriceValue();
     }
     $this->builder->populateWithArray($product->getData())->setDefault($product->getIsDefault())->setQty($product->getSelectionQty())->setDefined($product->getSelectionCanChangeQty())->setPrice($selectionPrice)->setPriceType($selectionPriceType);
     return $this->builder->create();
 }
 /**
  * @param \Magento\Catalog\Model\Product $selection
  * @param \Magento\Catalog\Model\Product $product
  * @return \Magento\Bundle\Api\Data\LinkInterface
  */
 private function buildLink(\Magento\Catalog\Model\Product $selection, \Magento\Catalog\Model\Product $product)
 {
     $selectionPriceType = $selectionPrice = null;
     /** @var \Magento\Bundle\Model\Selection $product */
     if ($product->getPriceType()) {
         $selectionPriceType = $selection->getSelectionPriceType();
         $selectionPrice = $selection->getSelectionPriceValue();
     }
     /** @var \Magento\Bundle\Api\Data\LinkInterface $link */
     $link = $this->linkFactory->create();
     $this->dataObjectHelper->populateWithArray($link, $selection->getData(), '\\Magento\\Bundle\\Api\\Data\\LinkInterface');
     $link->setIsDefault($selection->getIsDefault())->setQty($selection->getSelectionQty())->setIsDefined($selection->getSelectionCanChangeQty())->setPrice($selectionPrice)->setPriceType($selectionPriceType);
     return $link;
 }
Example #4
0
 /**
  * Calculate final price of selection
  * with take into account tier price
  *
  * @param  \Magento\Catalog\Model\Product $bundleProduct
  * @param  \Magento\Catalog\Model\Product $selectionProduct
  * @param  float                    $bundleQty
  * @param  float                    $selectionQty
  * @param  bool                       $multiplyQty
  * @param  bool                       $takeTierPrice
  * @return float
  */
 public function getSelectionFinalTotalPrice($bundleProduct, $selectionProduct, $bundleQty, $selectionQty, $multiplyQty = true, $takeTierPrice = true)
 {
     if (null === $bundleQty) {
         $bundleQty = 1.0;
     }
     if ($selectionQty === null) {
         $selectionQty = $selectionProduct->getSelectionQty();
     }
     if ($bundleProduct->getPriceType() == self::PRICE_TYPE_DYNAMIC) {
         $price = $selectionProduct->getFinalPrice($takeTierPrice ? $selectionQty : 1);
     } else {
         if ($selectionProduct->getSelectionPriceType()) {
             // percent
             $product = clone $bundleProduct;
             $product->setFinalPrice($this->getPrice($product));
             $this->_eventManager->dispatch('catalog_product_get_final_price', ['product' => $product, 'qty' => $bundleQty]);
             $price = $product->getData('final_price') * ($selectionProduct->getSelectionPriceValue() / 100);
         } else {
             // fixed
             $price = $selectionProduct->getSelectionPriceValue();
         }
     }
     if ($multiplyQty) {
         $price *= $selectionQty;
     }
     return min($price, $this->_applyGroupPrice($bundleProduct, $price), $this->_applyTierPrice($bundleProduct, $bundleQty, $price), $this->_applySpecialPrice($bundleProduct, $price));
 }