Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RendererPool.php
Go to the documentation of this file.
1 <?php
8 
13 
19 {
23  const DEFAULT_PRICE_GROUP_TYPE = 'default';
24 
28  const PRICE_RENDERER_DEFAULT = \Magento\Framework\Pricing\Render\PriceBox::class;
29 
33  const AMOUNT_RENDERER_DEFAULT = \Magento\Framework\Pricing\Render\Amount::class;
34 
44  public function createPriceRender(
45  $priceCode,
46  SaleableInterface $saleableItem,
47  array $data = []
48  ) {
49  $type = $saleableItem->getTypeId();
50 
51  // implement class resolving fallback
52  $pattern = [
53  $type . '/prices/' . $priceCode . '/render_class',
54  $type . '/default_render_class',
55  'default/prices/' . $priceCode . '/render_class',
56  'default/default_render_class',
57  ];
58  $renderClassName = $this->findDataByPattern($pattern);
59  if (!$renderClassName) {
60  throw new \InvalidArgumentException(
61  'Class name for price code "' . $priceCode . '" not registered'
62  );
63  }
64 
65  $price = $saleableItem->getPriceInfo()->getPrice($priceCode);
66  if (!$price) {
67  throw new \InvalidArgumentException(
68  'Price model for price code "' . $priceCode . '" not registered'
69  );
70  }
71 
72  $arguments['data'] = $data;
73  $arguments['rendererPool'] = $this;
74  $arguments['price'] = $price;
75  $arguments['saleableItem'] = $saleableItem;
76 
78  $renderBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments);
79  if (!$renderBlock instanceof PriceBoxRenderInterface) {
80  throw new \InvalidArgumentException(
81  'Block "' . $renderClassName
82  . '" must implement \Magento\Framework\Pricing\Render\PriceBoxRenderInterface'
83  );
84  }
85  $renderBlock->setTemplate($this->getRenderBlockTemplate($type, $priceCode));
86  return $renderBlock;
87  }
88 
99  public function createAmountRender(
101  SaleableInterface $saleableItem = null,
102  PriceInterface $price = null,
103  array $data = []
104  ) {
106  if ($saleableItem) {
107  $type = $saleableItem->getTypeId();
108  }
109 
110  $priceCode = null;
111  $renderClassName = self::AMOUNT_RENDERER_DEFAULT;
112 
113  if ($price) {
114  $priceCode = $price->getPriceCode();
115  // implement class resolving fallback
116  $pattern = [
117  $type . '/prices/' . $priceCode . '/amount_render_class',
118  $type . '/default_amount_render_class',
119  'default/prices/' . $priceCode . '/amount_render_class',
120  'default/default_amount_render_class',
121  ];
122  $renderClassName = $this->findDataByPattern($pattern);
123  if (!$renderClassName) {
124  throw new \InvalidArgumentException(
125  'There is no amount render class for price code "' . $priceCode . '"'
126  );
127  }
128  }
129 
130  $arguments['data'] = $data;
131  $arguments['rendererPool'] = $this;
132  $arguments['amount'] = $amount;
133 
134  if ($saleableItem) {
135  $arguments['saleableItem'] = $saleableItem;
136  if ($price) {
137  $arguments['price'] = $price;
138  }
139  }
140 
142  $amountBlock = $this->getLayout()->createBlock($renderClassName, '', $arguments);
143  if (!$amountBlock instanceof AmountRenderInterface) {
144  throw new \InvalidArgumentException(
145  'Block "' . $renderClassName
146  . '" must implement \Magento\Framework\Pricing\Render\AmountRenderInterface'
147  );
148  }
149  $amountBlock->setTemplate($this->getAmountRenderBlockTemplate($type, $priceCode));
150  return $amountBlock;
151  }
152 
158  public function getAdjustmentRenders(SaleableInterface $saleableItem = null, PriceInterface $price = null)
159  {
160  $itemType = null === $saleableItem ? 'default' : $saleableItem->getTypeId();
161  $priceType = null === $price ? 'default' : $price->getPriceCode();
162 
163  $fallbackPattern = [
164  "{$itemType}/adjustments/{$priceType}",
165  "{$itemType}/adjustments/default",
166  "default/adjustments/{$priceType}",
167  "default/adjustments/default",
168  ];
169  $renders = $this->findDataByPattern($fallbackPattern);
170  if ($renders) {
171  foreach ($renders as $code => $configuration) {
173  $render = $this->getLayout()->createBlock($configuration['adjustment_render_class']);
174  $render->setTemplate($configuration['adjustment_render_template']);
175  $renders[$code] = $render;
176  }
177  }
178 
179  return $renders;
180  }
181 
188  protected function getAmountRenderBlockTemplate($type, $priceCode)
189  {
190  $pattern = [
191  $type . '/prices/' . $priceCode . '/amount_render_template',
192  $type . '/default_amount_render_template',
193  'default/prices/' . $priceCode . '/amount_render_template',
194  'default/default_amount_render_template',
195  ];
197  if (!$template) {
198  throw new \InvalidArgumentException(
199  'For type "' . $type . '" amount render block not configured'
200  );
201  }
202  return $template;
203  }
204 
211  protected function getRenderBlockTemplate($type, $priceCode)
212  {
213  $pattern = [
214  $type . '/prices/' . $priceCode . '/render_template',
215  $type . '/default_render_template',
216  'default/prices/' . $priceCode . '/render_template',
217  'default/default_render_template',
218  ];
220  if (!$template) {
221  throw new \InvalidArgumentException(
222  'Price code "' . $priceCode . '" render block not configured'
223  );
224  }
225  return $template;
226  }
227 
232  protected function findDataByPattern(array $pattern)
233  {
234  $data = null;
235  foreach ($pattern as $key) {
236  $data = $this->getData($key);
237  if ($data) {
238  break;
239  }
240  }
241  return $data;
242  }
243 }
getData($key='', $index=null)
Definition: DataObject.php:119
$configuration
Definition: index.php:33
$pattern
Definition: website.php:22
$price
$amount
Definition: order.php:14
$type
Definition: item.phtml:13
$priceType
Definition: msrp.phtml:18
$arguments
$template
Definition: export.php:12
$code
Definition: info.phtml:12