Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicBundleWithTierPriceCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory;
10 
15 {
17  private $tierPriceFactory;
18 
19  protected function setUp()
20  {
21  parent::setUp();
22  $this->tierPriceFactory = $this->objectManager->create(ProductTierPriceInterfaceFactory::class);
23  }
24 
33  public function testPriceForDynamicBundle(array $strategyModifiers, array $expectedResults)
34  {
35  $this->prepareFixture($strategyModifiers, 'bundle_product');
36  $bundleProduct = $this->productRepository->get('bundle_product', false, null, true);
37 
39  $priceInfo = $bundleProduct->getPriceInfo();
41 
42  $this->assertEquals(
43  $expectedResults['minimalPrice'],
44  $priceInfo->getPrice($priceCode)->getMinimalPrice()->getValue(),
45  'Failed to check minimal price on product'
46  );
47  $this->assertEquals(
48  $expectedResults['maximalPrice'],
49  $priceInfo->getPrice($priceCode)->getMaximalPrice()->getValue(),
50  'Failed to check maximal price on product'
51  );
52 
53  $priceInfoFromIndexer = $this->productCollectionFactory->create()
54  ->addFieldToFilter('sku', 'bundle_product')
55  ->addPriceData()
56  ->load()
57  ->getFirstItem();
58 
59  $this->assertEquals($expectedResults['minimalPrice'], $priceInfoFromIndexer->getMinimalPrice());
60  $this->assertEquals($expectedResults['maximalPrice'], $priceInfoFromIndexer->getMaxPrice());
61  }
62 
68  public function getTestCases()
69  {
70  return [
71  '
72  #1 Testing product price for dynamic bundle
73  with one required option and tier price
74  ' => [
75  'strategy' => $this->getBundleConfiguration1(),
76  'expectedResults' => [
77  // 0.5 * 10
78  'minimalPrice' => 5,
79  // 0.5 * 10
80  'maximalPrice' => 5,
81  ]
82  ],
83 
84  '
85  #2 Testing product price for dynamic bundle
86  with one non required option and tier price
87  ' => [
88  'strategy' => $this->getBundleConfiguration2(),
89  'expectedResults' => [
90  // 0.5 * 2 * 10
91  'minimalPrice' => 10,
92  // 0.5 * 2 * 10
93  'maximalPrice' => 10,
94  ]
95  ],
96 
97  '
98  #3 Testing product price for dynamic bundle
99  with one required checkbox type option and tier price
100  ' => [
101  'strategy' => $this->getBundleConfiguration3(),
102  'expectedResults' => [
103  // 0.5 * 1 * 10
104  'minimalPrice' => 5,
105  // 0.5 * (1 * 10 + 3 * 20)
106  'maximalPrice' => 35,
107  ]
108  ],
109 
110  '
111  #4 Testing product price for dynamic bundle
112  with one required multi type option and tier price
113  ' => [
114  'strategy' => $this->getBundleConfiguration4(),
115  'expectedResults' => [
116  // 0.5 * 1 * 10
117  'minimalPrice' => 5,
118  // 0.5 * (1 * 10 + 3 * 20)
119  'maximalPrice' => 35,
120  ]
121  ],
122 
123  '
124  #5 Testing product price for dynamic bundle
125  with one required radio type option and tier price
126  ' => [
127  'strategy' => $this->getBundleConfiguration5(),
128  'expectedResults' => [
129  // 0.5 * 1 * 10
130  'minimalPrice' => 5,
131  // 0.5 * 3 * 20
132  'maximalPrice' => 30,
133 
134  ]
135  ],
136 
137  '
138  #6 Testing product price for dynamic bundle
139  with two required options and tier price
140  ' => [
141  'strategy' => $this->getBundleConfiguration6(),
142  'expectedResults' => [
143  // 0.5 * (1 * 10 + 1 * 10)
144  'minimalPrice' => 10,
145  // 0.5 * (3 * 20 + 1 * 10 + 3 * 20)
146  'maximalPrice' => 65,
147  ]
148  ],
149 
150  '
151  #7 Testing product price for dynamic bundle
152  with one required option, one non required option and tier price
153  ' => [
154  'strategy' => $this->getBundleConfiguration7(),
155  'expectedResults' => [
156  // 0.5 * (1 * 10)
157  'minimalPrice' => 5,
158  // 0.5 * (3 * 20 + 1 * 10 + 3 * 20)
159  'maximalPrice' => 65,
160  ]
161  ],
162 
163  '
164  #8 Testing product price for dynamic bundle
165  with two non required options and tier price
166  ' => [
167  'strategy' => $this->getBundleConfiguration8(),
168  'expectedResults' => [
169  // 0.5 * (1 * 10)
170  'minimalPrice' => 5,
171  // 0.5 * (3 * 20 + 1 * 10 + 3 * 20)
172  'maximalPrice' => 65,
173  ]
174  ],
175 
176  '
177  #9 Testing product price for dynamic bundle
178  with tier price and with simple with tier price
179  ' => [
180  'strategy' => $this->getBundleConfiguration9(),
181  'expectedResults' => [
182  // 0.5 * 1 * 2.5
183  'minimalPrice' => 1.25,
184  // 0.5 * 3 * 20
185  'maximalPrice' => 30,
186  ]
187  ],
188  ];
189  }
190 
195  private function getBundleConfiguration1()
196  {
197  $optionsData = [
198  [
199  'title' => 'Op1',
200  'required' => true,
201  'type' => 'checkbox',
202  'links' => [
203  [
204  'sku' => 'simple1',
205  'qty' => 1,
206  ],
207  ]
208  ]
209  ];
210 
211  $tierPriceData = [
213  'qty' => 1,
214  'value' => 50,
215  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
216  ];
217 
218  return [
219  [
220  'modifierName' => 'addTierPrice',
221  'data' => [$tierPriceData]
222  ],
223  [
224  'modifierName' => 'addSimpleProduct',
225  'data' => [$optionsData]
226  ],
227  ];
228  }
229 
234  private function getBundleConfiguration2()
235  {
236  $optionsData = [
237  [
238  'title' => 'Op1',
239  'type' => 'checkbox',
240  'required' => false,
241  'links' => [
242  [
243  'sku' => 'simple1',
244  'qty' => 2,
245  ],
246  ]
247  ]
248  ];
249 
250  $tierPriceData = [
252  'qty' => 1,
253  'value' => 50,
254  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
255  ];
256 
257  return [
258  [
259  'modifierName' => 'addTierPrice',
260  'data' => [$tierPriceData]
261  ],
262  [
263  'modifierName' => 'addSimpleProduct',
264  'data' => [$optionsData]
265  ],
266  ];
267  }
268 
273  private function getBundleConfiguration3()
274  {
275  $optionsData = [
276  [
277  'title' => 'Op1',
278  'required' => true,
279  'type' => 'checkbox',
280  'links' => [
281  [
282  'sku' => 'simple1',
283  'qty' => 1,
284  ],
285  [
286  'sku' => 'simple2',
287  'qty' => 3,
288  ],
289  ]
290  ]
291  ];
292 
293  $tierPriceData = [
295  'qty' => 1,
296  'value' => 50,
297  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
298  ];
299 
300  return [
301  [
302  'modifierName' => 'addTierPrice',
303  'data' => [$tierPriceData]
304  ],
305  [
306  'modifierName' => 'addSimpleProduct',
307  'data' => [$optionsData]
308  ],
309  ];
310  }
311 
316  private function getBundleConfiguration4()
317  {
318  $optionsData = [
319  [
320  'title' => 'Op1',
321  'required' => true,
322  'type' => 'multi',
323  'links' => [
324  [
325  'sku' => 'simple1',
326  'qty' => 1,
327  ],
328  [
329  'sku' => 'simple2',
330  'qty' => 3,
331  ],
332  ]
333  ]
334  ];
335 
336  $tierPriceData = [
338  'qty' => 1,
339  'value' => 50,
340  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
341  ];
342 
343  return [
344  [
345  'modifierName' => 'addTierPrice',
346  'data' => [$tierPriceData]
347  ],
348  [
349  'modifierName' => 'addSimpleProduct',
350  'data' => [$optionsData]
351  ],
352  ];
353  }
354 
359  private function getBundleConfiguration5()
360  {
361  $optionsData = [
362  [
363  'title' => 'Op1',
364  'required' => true,
365  'type' => 'radio',
366  'links' => [
367  [
368  'sku' => 'simple1',
369  'qty' => 1,
370  ],
371  [
372  'sku' => 'simple2',
373  'qty' => 3,
374  ],
375  ]
376  ]
377  ];
378 
379  $tierPriceData = [
381  'qty' => 1,
382  'value' => 50,
383  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
384  ];
385 
386  return [
387  [
388  'modifierName' => 'addTierPrice',
389  'data' => [$tierPriceData]
390  ],
391  [
392  'modifierName' => 'addSimpleProduct',
393  'data' => [$optionsData]
394  ],
395  ];
396  }
397 
402  private function getBundleConfiguration6()
403  {
404  $optionsData = [
405  [
406  'title' => 'Op1',
407  'required' => true,
408  'type' => 'radio',
409  'links' => [
410  [
411  'sku' => 'simple1',
412  'qty' => 1,
413  ],
414  [
415  'sku' => 'simple2',
416  'qty' => 3,
417  ],
418  ]
419  ],
420  [
421  'title' => 'Op2',
422  'required' => true,
423  'type' => 'checkbox',
424  'links' => [
425  [
426  'sku' => 'simple1',
427  'qty' => 1,
428  ],
429  [
430  'sku' => 'simple2',
431  'qty' => 3,
432  ],
433  ]
434  ]
435  ];
436 
437  $tierPriceData = [
439  'qty' => 1,
440  'value' => 50,
441  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
442  ];
443 
444  return [
445  [
446  'modifierName' => 'addTierPrice',
447  'data' => [$tierPriceData]
448  ],
449  [
450  'modifierName' => 'addSimpleProduct',
451  'data' => [$optionsData]
452  ],
453  ];
454  }
455 
460  private function getBundleConfiguration7()
461  {
462  $optionsData = [
463  [
464  'title' => 'Op1',
465  'required' => false,
466  'type' => 'radio',
467  'links' => [
468  [
469  'sku' => 'simple1',
470  'qty' => 1,
471  ],
472  [
473  'sku' => 'simple2',
474  'qty' => 3,
475  ],
476  ]
477  ],
478  [
479  'title' => 'Op2',
480  'required' => true,
481  'type' => 'checkbox',
482  'links' => [
483  [
484  'sku' => 'simple1',
485  'qty' => 1,
486  ],
487  [
488  'sku' => 'simple2',
489  'qty' => 3,
490  ],
491  ]
492  ]
493  ];
494 
495  $tierPriceData = [
497  'qty' => 1,
498  'value' => 50,
499  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
500  ];
501 
502  return [
503  [
504  'modifierName' => 'addTierPrice',
505  'data' => [$tierPriceData]
506  ],
507  [
508  'modifierName' => 'addSimpleProduct',
509  'data' => [$optionsData]
510  ],
511  ];
512  }
513 
518  private function getBundleConfiguration8()
519  {
520  $optionsData = [
521  [
522  'title' => 'Op1',
523  'required' => false,
524  'type' => 'radio',
525  'links' => [
526  [
527  'sku' => 'simple1',
528  'qty' => 1,
529  ],
530  [
531  'sku' => 'simple2',
532  'qty' => 3,
533  ],
534  ]
535  ],
536  [
537  'title' => 'Op2',
538  'required' => false,
539  'type' => 'checkbox',
540  'links' => [
541  [
542  'sku' => 'simple1',
543  'qty' => 1,
544  ],
545  [
546  'sku' => 'simple2',
547  'qty' => 3,
548  ],
549  ]
550  ]
551  ];
552 
553  $tierPriceData = [
555  'qty' => 1,
556  'value' => 50,
557  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
558  ];
559 
560  return [
561  [
562  'modifierName' => 'addTierPrice',
563  'data' => [$tierPriceData]
564  ],
565  [
566  'modifierName' => 'addSimpleProduct',
567  'data' => [$optionsData]
568  ],
569  ];
570  }
571 
576  private function getBundleConfiguration9()
577  {
578  $optionsData = [
579  [
580  'title' => 'Op1',
581  'required' => true,
582  'type' => 'radio',
583  'links' => [
584  [
585  'sku' => 'simple1',
586  'qty' => 1,
587  ],
588  [
589  'sku' => 'simple2',
590  'qty' => 3,
591  ],
592  ]
593  ]
594  ];
595 
596  $tierPriceData = [
598  'qty' => 1,
599  'value' => 50,
600  'extension_attributes' => new \Magento\Framework\DataObject(['percentage_value' => 50])
601  ];
602 
603  $tierPriceSimpleProductData = [
605  'qty' => 1,
606  'value' => 2.5
607  ];
608 
609  return [
610  [
611  'modifierName' => 'addTierPrice',
612  'data' => [$tierPriceData]
613  ],
614  [
615  'modifierName' => 'addTierPriceForSimple',
616  'data' => ['simple1', $tierPriceSimpleProductData]
617  ],
618  [
619  'modifierName' => 'addSimpleProduct',
620  'data' => [$optionsData]
621  ],
622  ];
623  }
624 
630  protected function addTierPrice(\Magento\Catalog\Model\Product $product, $tirePriceData)
631  {
632  $tierPrice = $this->tierPriceFactory->create([
633  'data' => $tirePriceData
634  ]);
635  $product->setTierPrices([$tierPrice]);
636 
637  return $product;
638  }
639 
646  protected function addTierPriceForSimple(\Magento\Catalog\Model\Product $bundleProduct, $sku, $tirePriceData)
647  {
648  $simple = $this->productRepository->get($sku, false, null, true);
649  $simple = $this->addTierPrice($simple, $tirePriceData);
650  $this->productRepository->save($simple);
651 
652  return $bundleProduct;
653  }
654 }
if($msrpShowOnGesture && $price['price']->getValue()< $product->getMsrp()) if($isSaleable) $tierPriceData
addTierPriceForSimple(\Magento\Catalog\Model\Product $bundleProduct, $sku, $tirePriceData)
$bundleProduct