Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundlePanel.php
Go to the documentation of this file.
1 <?php
7 
18 
24 {
25  const GROUP_CONTENT = 'content';
26  const CODE_SHIPMENT_TYPE = 'shipment_type';
27  const CODE_BUNDLE_DATA = 'bundle-items';
28  const CODE_AFFECT_BUNDLE_PRODUCT_SELECTIONS = 'affect_bundle_product_selections';
29  const CODE_BUNDLE_HEADER = 'bundle_header';
30  const CODE_BUNDLE_OPTIONS = 'bundle_options';
31  const SORT_ORDER = 20;
32 
36  protected $urlBuilder;
37 
41  protected $shipmentType;
42 
46  protected $arrayManager;
47 
51  protected $locator;
52 
59  public function __construct(
62  ShipmentType $shipmentType,
64  ) {
65  $this->locator = $locator;
66  $this->urlBuilder = $urlBuilder;
67  $this->shipmentType = $shipmentType;
68  $this->arrayManager = $arrayManager;
69  }
70 
75  public function modifyMeta(array $meta)
76  {
77  $meta = $this->removeFixedTierPrice($meta);
78  $path = $this->arrayManager->findPath(static::CODE_BUNDLE_DATA, $meta, null, 'children');
79 
80  $meta = $this->arrayManager->merge(
81  $path,
82  $meta,
83  [
84  'arguments' => [
85  'data' => [
86  'config' => [
87  'dataScope' => '',
88  'opened' => true,
89  'sortOrder' => $this->getNextGroupSortOrder(
90  $meta,
91  static::GROUP_CONTENT,
92  static::SORT_ORDER
93  )
94  ],
95  ],
96  ],
97  'children' => [
98  'modal' => [
99  'arguments' => [
100  'data' => [
101  'config' => [
102  'isTemplate' => false,
103  'componentType' => Modal::NAME,
104  'dataScope' => '',
105  'provider' => 'product_form.product_form_data_source',
106  'options' => [
107  'title' => __('Add Products to Option'),
108  'buttons' => [
109  [
110  'text' => __('Cancel'),
111  'actions' => ['closeModal'],
112  ],
113  [
114  'text' => __('Add Selected Products'),
115  'class' => 'action-primary',
116  'actions' => [
117  [
118  'targetName' => 'index = bundle_product_listing',
119  'actionName' => 'save'
120  ],
121  'closeModal'
122  ],
123  ],
124  ],
125  ],
126  ],
127  ],
128  ],
129  'children' => [
130  'bundle_product_listing' => [
131  'arguments' => [
132  'data' => [
133  'config' => [
134  'autoRender' => false,
135  'componentType' => 'insertListing',
136  'dataScope' => 'bundle_product_listing',
137  'externalProvider' =>
138  'bundle_product_listing.bundle_product_listing_data_source',
139  'selectionsProvider' =>
140  'bundle_product_listing.bundle_product_listing.product_columns.ids',
141  'ns' => 'bundle_product_listing',
142  'render_url' => $this->urlBuilder->getUrl('mui/index/render'),
143  'realTimeLink' => false,
144  'dataLinks' => ['imports' => false, 'exports' => true],
145  'behaviourType' => 'simple',
146  'externalFilterMode' => true,
147  ],
148  ],
149  ],
150  ],
151  ],
152  ],
153  self::CODE_AFFECT_BUNDLE_PRODUCT_SELECTIONS => [
154  'arguments' => [
155  'data' => [
156  'config' => [
157  'componentType' => Form\Field::NAME,
158  'dataType' => Form\Element\DataType\Text::NAME,
159  'formElement' => Form\Element\Input::NAME,
160  'dataScope' => 'data.affect_bundle_product_selections',
161  'visible' => false,
162  'value' => '1'
163  ],
164  ],
165  ],
166  ],
167  self::CODE_BUNDLE_HEADER => $this->getBundleHeader(),
168  self::CODE_BUNDLE_OPTIONS => $this->getBundleOptions()
169  ]
170  ]
171  );
172 
173  //TODO: Remove this workaround after MAGETWO-49902 is fixed
174  $bundleItemsGroup = $this->arrayManager->get($path, $meta);
175  $meta = $this->arrayManager->remove($path, $meta);
176  $meta = $this->arrayManager->set($path, $meta, $bundleItemsGroup);
177 
178  $meta = $this->modifyShipmentType($meta);
179 
180  return $meta;
181  }
182 
189  private function removeFixedTierPrice(array $meta)
190  {
191  $tierPricePath = $this->arrayManager->findPath(
193  $meta,
194  null,
195  'children'
196  );
197  $pricePath = $this->arrayManager->findPath(
199  $meta,
200  $tierPricePath
201  );
202  $pricePath = $this->arrayManager->slicePath($pricePath, 0, -1) . '/value_type/arguments/data/options';
203 
204  $price = $this->arrayManager->get($pricePath, $meta);
205  if ($price) {
206  $meta = $this->arrayManager->remove($pricePath, $meta);
207  foreach ($price as $key => $item) {
209  unset($price[$key]);
210  }
211  }
212  $meta = $this->arrayManager->merge(
213  $this->arrayManager->slicePath($pricePath, 0, -1),
214  $meta,
215  ['options' => $price]
216  );
217  }
218 
219  return $meta;
220  }
221 
225  public function modifyData(array $data)
226  {
227  return $data;
228  }
229 
236  private function modifyShipmentType(array $meta)
237  {
238  $meta = $this->arrayManager->merge(
239  $this->arrayManager->findPath(
240  static::CODE_SHIPMENT_TYPE,
241  $meta,
242  null,
243  'children'
244  ) . static::META_CONFIG_PATH,
245  $meta,
246  [
247  'dataScope' => 'data.product.shipment_type',
248  'validation' => [
249  'required-entry' => false
250  ]
251  ]
252  );
253 
254  return $meta;
255  }
256 
262  protected function getBundleHeader()
263  {
264  return [
265  'arguments' => [
266  'data' => [
267  'config' => [
268  'label' => null,
269  'formElement' => Container::NAME,
270  'componentType' => Container::NAME,
271  'template' => 'ui/form/components/complex',
272  'sortOrder' => 10,
273  ],
274  ],
275  ],
276  'children' => [
277  'add_button' => [
278  'arguments' => [
279  'data' => [
280  'config' => [
281  'title' => __('Add Option'),
282  'formElement' => Container::NAME,
283  'componentType' => Container::NAME,
284  'component' => 'Magento_Ui/js/form/components/button',
285  'sortOrder' => 20,
286  'actions' => [
287  [
288  'targetName' => 'product_form.product_form.'
289  . self::CODE_BUNDLE_DATA . '.' . self::CODE_BUNDLE_OPTIONS,
290  'actionName' => 'processingAddChild',
291  ]
292  ],
293  ],
294  ],
295  ],
296  ],
297  ],
298  ];
299  }
300 
306  protected function getBundleOptions()
307  {
308  return [
309  'arguments' => [
310  'data' => [
311  'config' => [
312  'componentType' => Container::NAME,
313  'component' => 'Magento_Bundle/js/components/bundle-dynamic-rows',
314  'template' => 'ui/dynamic-rows/templates/collapsible',
315  'additionalClasses' => 'admin__field-wide',
316  'dataScope' => 'data.bundle_options',
317  'isDefaultFieldScope' => 'is_default',
318  'bundleSelectionsName' => 'product_bundle_container.bundle_selections',
319  ],
320  ],
321  ],
322  'children' => [
323  'record' => [
324  'arguments' => [
325  'data' => [
326  'config' => [
327  'componentType' => Container::NAME,
328  'isTemplate' => true,
329  'is_collection' => true,
330  'headerLabel' => __('New Option'),
331  'component' => 'Magento_Ui/js/dynamic-rows/record',
332  'positionProvider' => 'product_bundle_container.position',
333  'imports' => [
334  'label' => '${ $.name }' . '.product_bundle_container.option_info.title:value'
335  ],
336  ],
337  ],
338  ],
339  'children' => [
340  'product_bundle_container' => [
341  'arguments' => [
342  'data' => [
343  'config' => [
344  'componentType' => 'fieldset',
345  'collapsible' => true,
346  'label' => '',
347  'opened' => true,
348  ],
349  ],
350  ],
351  'children' => [
352  'option_info' => $this->getOptionInfo(),
353  'position' => $this->getHiddenColumn('position', 20),
354  'option_id' => $this->getHiddenColumn('option_id', 30),
355  'delete' => $this->getHiddenColumn('delete', 40),
356  'bundle_selections' => [
357  'arguments' => [
358  'data' => [
359  'config' => [
360  'componentType' => Container::NAME,
361  'component' => 'Magento_Bundle/js/components/bundle-dynamic-rows-grid',
362  'sortOrder' => 50,
363  'additionalClasses' => 'admin__field-wide',
364  'template' => 'ui/dynamic-rows/templates/default',
365  'provider' => 'product_form.product_form_data_source',
366  'dataProvider' => '${ $.dataScope }' . '.bundle_button_proxy',
367  'identificationDRProperty' => 'product_id',
368  'identificationProperty' => 'product_id',
369  'map' => [
370  'product_id' => 'entity_id',
371  'name' => 'name',
372  'sku' => 'sku',
373  'price' => 'price',
374  'delete' => '',
375  'selection_can_change_qty' => '',
376  'selection_id' => '',
377  'selection_price_type' => '',
378  'selection_price_value' => '',
379  'selection_qty' => '',
380  ],
381  'links' => ['insertData' => '${ $.provider }:${ $.dataProvider }'],
382  'imports' => [
383  'inputType' => '${$.provider}:${$.dataScope}.type',
384  ],
385  'source' => 'product',
386  ],
387  ],
388  ],
389  'children' => [
390  'record' => $this->getBundleSelections(),
391  ]
392  ],
393  'modal_set' => $this->getModalSet(),
394  ]
395  ]
396  ]
397  ]
398  ]
399  ];
400  }
401 
409  protected function getHiddenColumn($columnName, $sortOrder)
410  {
411  return [
412  'arguments' => [
413  'data' => [
414  'config' => [
415  'componentType' => Form\Field::NAME,
416  'dataType' => Form\Element\DataType\Text::NAME,
417  'formElement' => Form\Element\Input::NAME,
418  'dataScope' => $columnName,
419  'visible' => false,
420  'additionalClasses' => ['_hidden' => true],
421  'sortOrder' => $sortOrder,
422  ],
423  ],
424  ],
425  ];
426  }
427 
433  protected function getModalSet()
434  {
435  return [
436  'arguments' => [
437  'data' => [
438  'config' => [
439  'sortOrder' => 60,
440  'formElement' => 'container',
441  'componentType' => 'container',
442  'dataScope' => 'bundle_button_proxy',
443  'component' => 'Magento_Catalog/js/bundle-proxy-button',
444  'provider' => 'product_form.product_form_data_source',
445  'listingDataProvider' => 'bundle_product_listing',
446  'actions' => [
447  [
448  'targetName' => 'product_form.product_form.' . static::CODE_BUNDLE_DATA . '.modal',
449  'actionName' => 'toggleModal'
450  ],
451  [
452  'targetName' => 'product_form.product_form.' . static::CODE_BUNDLE_DATA
453  . '.modal.bundle_product_listing',
454  'actionName' => 'render'
455  ]
456  ],
457  'title' => __('Add Products to Option'),
458  ],
459  ],
460  ],
461  ];
462  }
463 
469  protected function getTitleConfiguration()
470  {
471  $result['title']['arguments']['data']['config'] = [
472  'dataType' => Form\Element\DataType\Text::NAME,
473  'formElement' => Form\Element\Input::NAME,
474  'componentType' => Form\Field::NAME,
475  'dataScope' => $this->isDefaultStore() ? 'title' : 'default_title',
476  'label' => $this->isDefaultStore() ? __('Option Title') : __('Default Title'),
477  'sortOrder' => 10,
478  'validation' => ['required-entry' => true],
479  ];
480 
481  if (!$this->isDefaultStore()) {
482  $result['store_title']['arguments']['data']['config'] = [
483  'dataType' => Form\Element\DataType\Text::NAME,
484  'formElement' => Form\Element\Input::NAME,
485  'componentType' => Form\Field::NAME,
486  'dataScope' => 'title',
487  'label' => __('Store View Title'),
488  'sortOrder' => 15,
489  'validation' => ['required-entry' => true],
490  ];
491  }
492 
493  return $result;
494  }
495 
501  protected function getOptionInfo()
502  {
503  $result = [
504  'arguments' => [
505  'data' => [
506  'config' => [
507  'formElement' => 'container',
508  'componentType' => Container::NAME,
509  'component' => 'Magento_Ui/js/form/components/group',
510  'showLabel' => false,
511  'additionalClasses' => 'admin__field-group-columns admin__control-group-equal',
512  'breakLine' => false,
513  'sortOrder' => 10,
514  ],
515  ],
516  ],
517  'children' => [
518  'type' => [
519  'arguments' => [
520  'data' => [
521  'config' => [
522  'dataType' => Form\Element\DataType\Text::NAME,
523  'formElement' => Form\Element\Select::NAME,
524  'componentType' => Form\Field::NAME,
525  'component' => 'Magento_Ui/js/form/element/select',
526  'parentContainer' => 'product_bundle_container',
527  'selections' => 'bundle_selections',
528  'isDefaultIndex' => 'is_default',
529  'userDefinedIndex' => 'selection_can_change_qty',
530  'dataScope' => 'type',
531  'label' => __('Input Type'),
532  'sortOrder' => 20,
533  'options' => [
534  [
535  'label' => __('Drop-down'),
536  'value' => 'select'
537  ],
538  [
539  'label' => __('Radio Buttons'),
540  'value' => 'radio'
541  ],
542  [
543  'label' => __('Checkbox'),
544  'value' => 'checkbox'
545  ],
546  [
547  'label' => __('Multiple Select'),
548  'value' => 'multi'
549  ]
550  ],
551  'typeMap' => [
552  'select' => 'radio',
553  'radio' => 'radio',
554  'checkbox' => 'checkbox',
555  'multi' => 'checkbox'
556  ]
557  ],
558  ],
559  ],
560  ],
561  'required' => [
562  'arguments' => [
563  'data' => [
564  'config' => [
565  'dataType' => Form\Element\DataType\Number::NAME,
566  'formElement' => Form\Element\Checkbox::NAME,
567  'componentType' => Form\Field::NAME,
568  'description' => __('Required'),
569  'dataScope' => 'required',
570  'label' => ' ',
571  'value' => '1',
572  'valueMap' => [
573  'true' => '1',
574  'false' => '0',
575  ],
576  'sortOrder' => 30,
577  ],
578  ],
579  ],
580  ],
581  ],
582  ];
583 
584  return $this->arrayManager->merge('children', $result, $this->getTitleConfiguration());
585  }
586 
593  protected function getBundleSelections()
594  {
595  return [
596  'arguments' => [
597  'data' => [
598  'config' => [
599  'componentType' => Container::NAME,
600  'isTemplate' => true,
601  'component' => 'Magento_Ui/js/dynamic-rows/record',
602  'is_collection' => true,
603  'imports' => [
604  'inputType' => '${$.parentName}:inputType',
605  ],
606  'exports' => [
607  'isDefaultValue' => '${$.parentName}:isDefaultValue.${$.index}',
608  ],
609  ],
610  ],
611  ],
612  'children' => [
613  'selection_id' => $this->getHiddenColumn('selection_id', 10),
614  'option_id' => $this->getHiddenColumn('option_id', 20),
615  'product_id' => $this->getHiddenColumn('product_id', 30),
616  'delete' => $this->getHiddenColumn('delete', 40),
617  'is_default' => [
618  'arguments' => [
619  'data' => [
620  'config' => [
621  'formElement' => Form\Element\Checkbox::NAME,
622  'componentType' => Form\Field::NAME,
623  'component' => 'Magento_Bundle/js/components/bundle-checkbox',
624  'parentContainer' => 'product_bundle_container',
625  'parentSelections' => 'bundle_selections',
626  'changer' => 'option_info.type',
627  'dataType' => Form\Element\DataType\Boolean::NAME,
628  'label' => __('Is Default'),
629  'dataScope' => 'is_default',
630  'prefer' => 'radio',
631  'value' => '0',
632  'sortOrder' => 50,
633  'valueMap' => ['false' => '0', 'true' => '1']
634  ],
635  ],
636  ],
637  ],
638  'name' => [
639  'arguments' => [
640  'data' => [
641  'config' => [
642  'componentType' => Form\Field::NAME,
643  'dataType' => Form\Element\DataType\Text::NAME,
644  'formElement' => Form\Element\Input::NAME,
645  'elementTmpl' => 'ui/dynamic-rows/cells/text',
646  'label' => __('Name'),
647  'dataScope' => 'name',
648  'sortOrder' => 60,
649  ],
650  ],
651  ],
652  ],
653  'sku' => [
654  'arguments' => [
655  'data' => [
656  'config' => [
657  'componentType' => Form\Field::NAME,
658  'dataType' => Form\Element\DataType\Text::NAME,
659  'formElement' => Form\Element\Input::NAME,
660  'elementTmpl' => 'ui/dynamic-rows/cells/text',
661  'label' => __('SKU'),
662  'dataScope' => 'sku',
663  'sortOrder' => 70,
664  ],
665  ],
666  ],
667  ],
668  'selection_price_value' => $this->getSelectionPriceValue(),
669  'selection_price_type' => $this->getSelectionPriceType(),
670  'selection_qty' => [
671  'arguments' => [
672  'data' => [
673  'config' => [
674  'component' => 'Magento_Bundle/js/components/bundle-option-qty',
675  'formElement' => Form\Element\Input::NAME,
676  'componentType' => Form\Field::NAME,
677  'dataType' => Form\Element\DataType\Number::NAME,
678  'label' => __('Default Quantity'),
679  'dataScope' => 'selection_qty',
680  'value' => '1',
681  'sortOrder' => 100,
682  'validation' => [
683  'required-entry' => true,
684  'validate-number' => true,
685  'validate-greater-than-zero' => true
686  ],
687  'imports' => [
688  'isInteger' => '${ $.provider }:${ $.parentScope }.selection_qty_is_integer'
689  ],
690  ],
691  ],
692  ],
693  ],
694  'selection_can_change_qty' => [
695  'arguments' => [
696  'data' => [
697  'config' => [
698  'componentType' => Form\Field::NAME,
699  'formElement' => Form\Element\Checkbox::NAME,
700  'dataType' => Form\Element\DataType\Price::NAME,
701  'component' => 'Magento_Bundle/js/components/bundle-user-defined-checkbox',
702  'label' => __('User Defined'),
703  'dataScope' => 'selection_can_change_qty',
704  'value' => '1',
705  'valueMap' => ['true' => '1', 'false' => '0'],
706  'sortOrder' => 110,
707  'imports' => [
708  'inputType' => '${$.parentName}:inputType',
709  ],
710  ],
711  ],
712  ],
713  ],
714  'position' => $this->getHiddenColumn('position', 120),
715  'action_delete' => [
716  'arguments' => [
717  'data' => [
718  'config' => [
719  'componentType' => 'actionDelete',
720  'dataType' => Form\Element\DataType\Text::NAME,
721  'label' => '',
722  'fit' => true,
723  'sortOrder' => 130,
724  ],
725  ],
726  ],
727  ],
728  ],
729  ];
730  }
731 
737  protected function getSelectionPriceValue()
738  {
739  return [
740  'arguments' => [
741  'data' => [
742  'config' => [
743  'componentType' => Form\Field::NAME,
744  'dataType' => Form\Element\DataType\Price::NAME,
745  'formElement' => Form\Element\Input::NAME,
746  'label' => __('Price'),
747  'dataScope' => 'selection_price_value',
748  'value' => '0.00',
749  'imports' => [
750  'visible' => '!ns = ${ $.ns }, index = ' . BundlePrice::CODE_PRICE_TYPE . ':checked'
751  ],
752  'sortOrder' => 80,
753  ],
754  ],
755  ],
756  ];
757  }
758 
764  protected function getSelectionPriceType()
765  {
766  return [
767  'arguments' => [
768  'data' => [
769  'config' => [
770  'componentType' => Form\Field::NAME,
771  'dataType' => Form\Element\DataType\Boolean::NAME,
772  'formElement' => Form\Element\Select::NAME,
773  'label' => __('Price Type'),
774  'dataScope' => 'selection_price_type',
775  'value' => '0',
776  'options' => [
777  [
778  'label' => __('Fixed'),
779  'value' => '0'
780  ],
781  [
782  'label' => __('Percent'),
783  'value' => '1'
784  ]
785  ],
786  'imports' => [
787  'visible' => '!ns = ${ $.ns }, index = ' . BundlePrice::CODE_PRICE_TYPE . ':checked'
788  ],
789  'sortOrder' => 90,
790  ],
791  ],
792  ],
793  ];
794  }
795 
801  protected function isDefaultStore()
802  {
803  return $this->locator->getProduct()->getStoreId() == 0;
804  }
805 }
return false
Definition: gallery.phtml:36
getNextGroupSortOrder(array $meta, $groupCodes, $defaultSortOrder, $iteration=1)
__()
Definition: __.php:13
$price
__construct(LocatorInterface $locator, UrlInterface $urlBuilder, ShipmentType $shipmentType, ArrayManager $arrayManager)
Definition: BundlePanel.php:59