Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfiguredPrice.php
Go to the documentation of this file.
1 <?php
8 
17 
24 {
29 
33  protected $calculator;
34 
38  protected $item;
39 
45  private $serializer;
46 
50  private $configuredPriceSelection;
51 
61  public function __construct(
62  Product $saleableItem,
63  $quantity,
66  ItemInterface $item = null,
67  JsonSerializer $serializer = null,
68  ConfiguredPriceSelection $configuredPriceSelection = null
69  ) {
70  $this->item = $item;
71  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
72  ->get(JsonSerializer::class);
73  $this->configuredPriceSelection = $configuredPriceSelection
75  ->get(ConfiguredPriceSelection::class);
76  parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
77  }
78 
83  public function setItem(ItemInterface $item)
84  {
85  $this->item = $item;
86  return $this;
87  }
88 
94  public function getOptions()
95  {
97  $bundleOptions = [];
99  $typeInstance = $bundleProduct->getTypeInstance();
100  $bundleOptionsIds = [];
101  if ($this->item !== null) {
102  // get bundle options
103  $optionsQuoteItemOption = $this->item->getOptionByCode('bundle_option_ids');
104  if ($optionsQuoteItemOption && $optionsQuoteItemOption->getValue()) {
105  $bundleOptionsIds = $this->serializer->unserialize($optionsQuoteItemOption->getValue());
106  }
107  }
108  if ($bundleOptionsIds) {
110  $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $bundleProduct);
111  // get and add bundle selections collection
112  $selectionsQuoteItemOption = $this->item->getOptionByCode('bundle_selection_ids');
113  $bundleSelectionIds = $this->serializer->unserialize($selectionsQuoteItemOption->getValue());
114  if ($bundleSelectionIds) {
115  $selectionsCollection = $typeInstance->getSelectionsByIds($bundleSelectionIds, $bundleProduct);
116  $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true);
117  }
118  }
119 
120  return $bundleOptions;
121  }
122 
129  public function getConfiguredAmount($baseValue = 0.)
130  {
131  $selectionPriceList = $this->configuredPriceSelection->getSelectionPriceList($this);
132 
133  return $this->calculator->calculateBundleAmount(
134  $baseValue,
135  $this->product,
136  $selectionPriceList
137  );
138  }
139 
145  public function getValue()
146  {
147  if ($this->item) {
148  $configuredOptionsAmount = $this->getConfiguredAmount()->getBaseAmount();
149  return parent::getValue() +
150  $this->priceInfo
151  ->getPrice(BundleDiscountPrice::PRICE_CODE)
152  ->calculateDiscount($configuredOptionsAmount);
153  }
154  return parent::getValue();
155  }
156 
162  public function getAmount()
163  {
164  return $this->item ? $this->getConfiguredAmount($this->getBasePrice()->getValue()) : parent::getAmount();
165  }
166 }
$bundleProduct
__construct(Product $saleableItem, $quantity, BundleCalculatorInterface $calculator, PriceCurrencyInterface $priceCurrency, ItemInterface $item=null, JsonSerializer $serializer=null, ConfiguredPriceSelection $configuredPriceSelection=null)