/** * Get configurable product options. * * @param InjectableFixture|null $product [optional] * @return array * @throws \Exception */ public function getOptions(InjectableFixture $product) { /** @var ConfigurableProduct $product */ $attributesData = $product->hasData('configurable_options') ? $product->getConfigurableOptions()['attributes_data'] : []; $listOptions = $this->getListOptions(); $result = []; foreach ($attributesData as $option) { $title = $option['label']; if (!isset($listOptions[$title])) { throw new \Exception("Can't find option: \"{$title}\""); } /** @var Element $optionElement */ $optionElement = $listOptions[$title]; $type = $option['frontend_input']; $option['frontend_input'] = explode('/', $option['frontend_input'])[1]; $typeMethod = preg_replace('/[^a-zA-Z]/', '', $option['frontend_input']); $getTypeData = 'get' . ucfirst(strtolower($typeMethod)) . 'Data'; $optionData = $this->{$getTypeData}($optionElement); $optionData['title'] = $title; $optionData['type'] = $type; $isRequire = $this->_rootElement->find(sprintf($this->required, $title), Locator::SELECTOR_XPATH)->isVisible(); $optionData['is_require'] = $isRequire ? 'Yes' : 'No'; $result[$title] = $optionData; } return $result; }
/** * Get configurable product price. * * @param InjectableFixture $product * @return int */ protected function getProductPrice(InjectableFixture $product) { $price = $product->getPrice(); if (!$this->productsIsConfigured) { return $price; } $checkoutData = $product->getCheckoutData(); if ($checkoutData === null) { return 0; } $attributesData = $product->getConfigurableOptions()['attributes_data']; foreach ($checkoutData['options']['configurable_options'] as $option) { $itemOption = $attributesData[$option['title']]['options'][$option['value']]; $price += $itemOption['price_type'] == 'Fixed' ? $itemOption['price'] : $product->getPrice() / 100 * $itemOption['price']; } return $price; }
/** * Fill in the option specified for the product. * * @param InjectableFixture $product * @return void */ public function fillOptions(InjectableFixture $product) { /** @var ConfigurableProduct $product */ $attributesData = $product->getConfigurableOptions()['attributes_data']; $checkoutData = $product->getCheckoutData(); // Prepare attribute data foreach ($attributesData as $attributeKey => $attribute) { $attributesData[$attributeKey] = ['type' => $attribute['frontend_input'], 'title' => $attribute['frontend_label'], 'options' => []]; foreach ($attribute['options'] as $optionKey => $option) { $attributesData[$attributeKey]['options'][$optionKey] = ['title' => $option['label']]; } $attributesData[$attributeKey]['options'] = array_values($attributesData[$attributeKey]['options']); } $attributesData = array_values($attributesData); $configurableCheckoutData = isset($checkoutData['options']['configurable_options']) ? $checkoutData['options']['configurable_options'] : []; $checkoutOptionsData = $this->prepareCheckoutData($attributesData, $configurableCheckoutData); $this->getConfigurableOptionsBlock()->fillOptions($checkoutOptionsData); }