Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductCustomOptionsDataProvider.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
10 use Magento\Catalog\Model\Product\Option\Repository as ProductOptionRepository;
11 use Magento\Catalog\Model\Product\Option\Value as ProductOptionValueModel;
15 
23 {
28  protected $request;
29 
35 
41 
56  public function __construct(
57  $name,
60  CollectionFactory $collectionFactory,
62  ProductOptionRepository $productOptionRepository,
63  ProductOptionValueModel $productOptionValueModel,
64  array $addFieldStrategies = [],
65  array $addFilterStrategies = [],
66  array $meta = [],
67  array $data = []
68  ) {
69  parent::__construct(
70  $name,
73  $collectionFactory,
76  $meta,
77  $data
78  );
79 
80  $this->request = $request;
81  $this->productOptionRepository = $productOptionRepository;
82  $this->productOptionValueModel = $productOptionValueModel;
83  }
84 
89  public function getData()
90  {
91  if (!$this->getCollection()->isLoaded()) {
92  $currentProductId = (int)$this->request->getParam('current_product_id');
93 
94  if (0 !== $currentProductId) {
95  $this->getCollection()->getSelect()->where('e.entity_id != ?', $currentProductId);
96  }
97 
98  $this->getCollection()->getSelect()->distinct()->join(
99  ['opt' => $this->getCollection()->getTable('catalog_product_option')],
100  'opt.product_id = e.entity_id',
101  null
102  );
103  $this->getCollection()->load();
104 
106  foreach ($this->getCollection() as $product) {
107  $options = [];
108 
110  foreach ($this->productOptionRepository->getProductOptions($product) as $option) {
111  $option->setData(
112  'values',
113  $this->productOptionValueModel->getValuesCollection($option)->toArray()['items']
114  );
115 
116  $options[] = $option->toArray();
117  }
118 
119  $product->setOptions($options);
120  }
121  }
122 
123  $items = $this->getCollection()->toArray();
124 
125  return [
126  'totalRecords' => $this->getCollection()->getSize(),
127  'items' => array_values($items),
128  ];
129  }
130 }
__construct( $name, $primaryFieldName, $requestFieldName, CollectionFactory $collectionFactory, RequestInterface $request, ProductOptionRepository $productOptionRepository, ProductOptionValueModel $productOptionValueModel, array $addFieldStrategies=[], array $addFilterStrategies=[], array $meta=[], array $data=[])
$items