Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SkuProcessor.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  protected $productFactory;
20 
24  protected $oldSkus;
25 
38  protected $newSkus;
39 
43  protected $productTypeModels;
44 
50  private $metadataPool;
51 
57  private $productEntityLinkField;
58 
64  private $productEntityIdentifierField;
65 
69  public function __construct(
70  \Magento\Catalog\Model\ProductFactory $productFactory
71  ) {
72  $this->productFactory = $productFactory;
73  }
74 
79  public function setTypeModels($typeModels)
80  {
81  $this->productTypeModels = $typeModels;
82  return $this;
83  }
84 
90  public function getOldSkus()
91  {
92  if (!$this->oldSkus) {
93  $this->oldSkus = $this->_getSkus();
94  }
95  return $this->oldSkus;
96  }
97 
103  public function reloadOldSkus()
104  {
105  $this->oldSkus = $this->_getSkus();
106 
107  return $this;
108  }
109 
115  public function addNewSku($sku, $data)
116  {
117  $sku = strtolower($sku);
118  $this->newSkus[$sku] = $data;
119  return $this;
120  }
121 
128  public function setNewSkuData($sku, $key, $data)
129  {
130  $sku = strtolower($sku);
131  if (isset($this->newSkus[$sku])) {
132  $this->newSkus[$sku][$key] = $data;
133  }
134  return $this;
135  }
136 
141  public function getNewSku($sku = null)
142  {
143  if ($sku !== null) {
144  $sku = strtolower($sku);
145  return $this->newSkus[$sku] ?? null;
146  }
147  return $this->newSkus;
148  }
149 
155  protected function _getSkus()
156  {
157  $oldSkus = [];
158  $columns = ['entity_id', 'type_id', 'attribute_set_id', 'sku'];
159  if ($this->getProductEntityLinkField() != $this->getProductIdentifierField()) {
160  $columns[] = $this->getProductEntityLinkField();
161  }
162  foreach ($this->productFactory->create()->getProductEntitiesInfo($columns) as $info) {
163  $typeId = $info['type_id'];
164  $sku = strtolower($info['sku']);
165  $oldSkus[$sku] = [
166  'type_id' => $typeId,
167  'attr_set_id' => $info['attribute_set_id'],
168  'entity_id' => $info['entity_id'],
169  'supported_type' => isset($this->productTypeModels[$typeId]),
170  $this->getProductEntityLinkField() => $info[$this->getProductEntityLinkField()],
171  ];
172  }
173  return $oldSkus;
174  }
175 
181  private function getMetadataPool()
182  {
183  if (!$this->metadataPool) {
185  ->get(\Magento\Framework\EntityManager\MetadataPool::class);
186  }
187  return $this->metadataPool;
188  }
189 
195  private function getProductEntityLinkField()
196  {
197  if (!$this->productEntityLinkField) {
198  $this->productEntityLinkField = $this->getMetadataPool()
199  ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
200  ->getLinkField();
201  }
202  return $this->productEntityLinkField;
203  }
204 
210  private function getProductIdentifierField()
211  {
212  if (!$this->productEntityIdentifierField) {
213  $this->productEntityIdentifierField = $this->getMetadataPool()
214  ->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
215  ->getIdentifierField();
216  }
217  return $this->productEntityIdentifierField;
218  }
219 }
__construct(\Magento\Catalog\Model\ProductFactory $productFactory)
$columns
Definition: default.phtml:15
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52