Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateConfigurations.php
Go to the documentation of this file.
1 <?php
9 
11 {
15  protected $productRepository;
16 
20  protected $request;
21 
25  protected $variationHandler;
26 
30  private $keysPost = [
31  'status',
32  'sku',
33  'name',
34  'price',
35  'configurable_attribute',
36  'weight',
37  'media_gallery',
38  'swatch_image',
39  'small_image',
40  'thumbnail',
41  'image',
42  ];
43 
49  public function __construct(
53  ) {
54  $this->request = $request;
55  $this->productRepository = $productRepository;
56  $this->variationHandler = $variationHandler;
57  }
58 
68  public function afterInitialize(
69  \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject,
70  \Magento\Catalog\Model\Product $configurableProduct
71  ) {
72  $configurations = $this->getConfigurations();
73  $configurations = $this->variationHandler->duplicateImagesForVariations($configurations);
74  if (count($configurations)) {
75  foreach ($configurations as $productId => $productData) {
77  $product = $this->productRepository->getById($productId, false, $this->request->getParam('store', 0));
78  $productData = $this->variationHandler->processMediaGallery($product, $productData);
79  $product->addData($productData);
80  if ($product->hasDataChanges()) {
81  $product->save();
82  }
83  }
84  }
85  return $configurableProduct;
86  }
87 
93  protected function getConfigurations()
94  {
95  $result = [];
96  $configurableMatrix = $this->request->getParam('configurable-matrix-serialized', "[]");
97  if (isset($configurableMatrix) && $configurableMatrix != "") {
98  $configurableMatrix = json_decode($configurableMatrix, true);
99 
100  foreach ($configurableMatrix as $item) {
101  if (empty($item['was_changed'])) {
102  continue;
103  } else {
104  unset($item['was_changed']);
105  }
106 
107  if (!$item['newProduct']) {
108  $result[$item['id']] = $this->mapData($item);
109 
110  if (isset($item['qty'])) {
111  $result[$item['id']]['quantity_and_stock_status']['qty'] = $item['qty'];
112  }
113  }
114  }
115  }
116 
117  return $result;
118  }
119 
126  private function mapData(array $item)
127  {
128  $result = [];
129 
130  foreach ($this->keysPost as $key) {
131  if (isset($item[$key])) {
132  $result[$key] = $item[$key];
133  }
134  }
135 
136  return $result;
137  }
138 }
__construct(\Magento\Framework\App\RequestInterface $request, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\ConfigurableProduct\Model\Product\VariationHandler $variationHandler)
$productData