Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateChildProductStep.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductEdit;
10 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex;
12 use Magento\Mtf\TestStep\TestStepInterface;
13 use Magento\Mtf\Fixture\FixtureFactory;
14 
18 class UpdateChildProductStep implements TestStepInterface
19 {
25  private $attributeKey = 'attribute_key_0';
26 
32  private $product;
33 
39  private $productGrid;
40 
46  private $productEdit;
47 
53  private $fixtureFactory;
54 
60  private $productUpdate;
61 
69  public function __construct(
70  ConfigurableProduct $product,
71  CatalogProductIndex $productGrid,
72  CatalogProductEdit $productEdit,
73  FixtureFactory $fixtureFactory,
74  array $productUpdate
75  ) {
76  $this->product = $product;
77  $this->productGrid = $productGrid;
78  $this->productEdit = $productEdit;
79  $this->fixtureFactory = $fixtureFactory;
80  $this->productUpdate = $productUpdate;
81  }
82 
88  public function run()
89  {
90  $items = explode(',', $this->productUpdate['optionNumber']);
91 
92  foreach ($items as $itemIndex) {
93  $index = (int)$itemIndex - 1;
94  $optionKey = 'option_key_' . $index;
95  $matrixIndex = $this->attributeKey . ':' . $optionKey;
96  $sku = $this->product->getConfigurableAttributesData()['matrix'][$matrixIndex]['sku'];
97  $this->fillChildProductData($sku);
98  $this->prepareResultProduct($matrixIndex, $optionKey);
99  }
100 
101  return ['product' => $this->product];
102  }
103 
111  private function prepareResultProduct($matrixIndex, $optionKey)
112  {
113  $product = $this->product->getData();
114  $attributeKey = 'configurable_attributes_data';
115 
116  if (isset($this->productUpdate['newPrice'])) {
117  $product[$attributeKey]['matrix'][$matrixIndex]['price'] = $this->productUpdate['newPrice'];
118  $product[$attributeKey]['attributes_data'][$this->attributeKey]['options'][$optionKey]['pricing_value']
119  = $this->productUpdate['newPrice'];
120  } else {
121  unset($product[$attributeKey]['matrix'][$matrixIndex]);
122  unset($product[$attributeKey]['attributes_data'][$this->attributeKey]['options'][$optionKey]);
123  }
124 
125  $product['category_ids']['category']
126  = $this->product->getDataFieldConfig('category_ids')['source']->getCategories()[0];
127  $product['price'] = $this->getLowestConfigurablePrice($product);
128 
129  if (!empty($product['configurable_attributes_data']['attributes_data'][$this->attributeKey]['options'])) {
130  $this->product = $this->fixtureFactory->createByCode('configurableProduct', ['data' => $product]);
131  }
132  }
133 
140  private function fillChildProductData($sku)
141  {
142  $this->productGrid->open();
143  $this->productGrid->getProductGrid()->searchAndOpen(['sku' => $sku]);
144 
145  if (isset($this->productUpdate['switchScope']) && $this->productUpdate['switchScope']) {
146  $store = $this->fixtureFactory->createByCode('store', ['dataset' => 'default']);
147  $this->productEdit->getFormPageActions()->changeStoreViewScope($store);
148  }
149 
150  if (isset($this->productUpdate['childProductUpdate']['unassignFromWebsite'])) {
151  $this->productEdit->getProductForm()->unassignFromWebsite(
152  $this->productUpdate['childProductUpdate']['unassignFromWebsite']
153  );
154  } else {
155  $fixture = $this->fixtureFactory->createByCode(
156  'catalogProductSimple',
157  $this->productUpdate['childProductUpdate']
158  );
159  $this->productEdit->getProductForm()->fill($fixture);
160  }
161 
162  $this->productEdit->getFormPageActions()->save();
163  }
164 
171  private function getLowestConfigurablePrice(array $product)
172  {
173  $configurableOptions = $product['configurable_attributes_data'];
174  $attributeOption = reset($configurableOptions['matrix']);
175  $price = isset($attributeOption['price']) ? $attributeOption['price'] : "0";
176 
177  foreach ($configurableOptions['matrix'] as $option) {
178  if ($price > $option['price']) {
179  $price = $option['price'];
180  }
181  }
182 
183  return $price;
184  }
185 }
$price
__construct(ConfigurableProduct $product, CatalogProductIndex $productGrid, CatalogProductEdit $productEdit, FixtureFactory $fixtureFactory, array $productUpdate)
$index
Definition: list.phtml:44
$items