/** * Fill in the option specified for the product * * @param FixtureInterface $product * @return void */ public function fillOptions(FixtureInterface $product) { if ($product instanceof InjectableFixture) { /** @var ConfigurableProductInjectable $product */ $attributesData = $product->getConfigurableAttributesData()['attributes_data']; $checkoutData = $product->getCheckoutData(); // Prepare attribute data foreach ($attributesData as $attributeKey => $attribute) { $attributesData[$attributeKey] = ['type' => $attribute['frontend_input'], 'title' => $attribute['label'], 'options' => []]; foreach ($attribute['options'] as $optionKey => $option) { $attributesData[$attributeKey]['options'][$optionKey] = ['title' => $option['label']]; } } } else { // TODO: Removed after refactoring(removed) old product fixture. /** @var ConfigurableProduct $product */ $attributesData = $product->getConfigurableAttributes(); $checkoutData = $product->getCheckoutData(); // Prepare attributes data foreach ($attributesData as $attributeKey => $attribute) { $attributesData[$attributeKey] = ['type' => 'dropdown', 'title' => $attribute['label']['value']]; unset($attribute['label']); foreach ($attribute as $optionKey => $option) { $attributesData[$attributeKey]['options'][$optionKey] = ['title' => $option['option_label']['value']]; } } } $configurableCheckoutData = isset($checkoutData['configurable_options']) ? $checkoutData['configurable_options'] : []; $checkoutOptionsData = $this->prepareCheckoutData($attributesData, $configurableCheckoutData); $this->getCustomOptionsBlock()->fillCustomOptions($checkoutOptionsData); parent::fillOptions($product); }
/** * Get configurable product options * * @param FixtureInterface|null $product [optional] * @return array * @throws \Exception */ public function getOptions(FixtureInterface $product) { if ($product instanceof InjectableFixture) { /** @var ConfigurableProductInjectable $product */ $attributesData = $product->hasData('configurable_attributes_data') ? $product->getConfigurableAttributesData()['attributes_data'] : []; } else { /** @var ConfigurableProduct $product */ $attributesData = $product->getConfigurableAttributes(); foreach ($attributesData as $key => $attributeData) { $attributeData['label'] = $attributeData['label']['value']; $attributeData['frontend_input'] = 'dropdown'; $attributesData[$key] = $attributeData; } } $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]; $typeMethod = preg_replace('/[^a-zA-Z]/', '', $option['frontend_input']); $getTypeData = 'get' . ucfirst(strtolower($typeMethod)) . 'Data'; $optionData = $this->{$getTypeData}($optionElement); $optionData['title'] = $title; $optionData['type'] = $option['frontend_input']; $optionData['is_require'] = $optionElement->find($this->required, Locator::SELECTOR_XPATH)->isVisible() ? 'Yes' : 'No'; $result[$title] = $optionData; } return $result; }
/** * Get configurable product price * * @param FixtureInterface $product * @throws \Exception * @return int */ protected function getProductPrice(FixtureInterface $product) { $price = $product->getPrice(); if (!$this->productsIsConfigured) { return $price; } if (!$product instanceof ConfigurableProductInjectable) { throw new \Exception("Product '{$product->getName}()' is not configurable product."); } $checkoutData = $product->getCheckoutData(); if ($checkoutData === null) { return 0; } $attributesData = $product->getConfigurableAttributesData()['attributes_data']; foreach ($checkoutData['configurable_options'] as $option) { $itemOption = $attributesData[$option['title']]['options'][$option['value']]; $itemPrice = $itemOption['is_percent'] == 'No' ? $itemOption['pricing_value'] : $product->getPrice() / 100 * $itemOption['pricing_value']; $price += $itemPrice; } return $price; }
/** * Preparing matrix data * * @param FixtureInterface $product * @return array */ protected function prepareVariationsMatrix(FixtureInterface $product) { /** @var ConfigurableAttributesData $configurableAttributesData */ $configurableAttributesData = $product->getDataFieldConfig('configurable_attributes_data')['source']; $attributesData = $configurableAttributesData->getAttributesData(); $matrixData = $product->getConfigurableAttributesData()['matrix']; $result = []; foreach ($matrixData as $variationKey => $variation) { $compositeKeys = explode(' ', $variationKey); $keyIds = []; $configurableAttribute = []; foreach ($compositeKeys as $compositeKey) { list($attributeKey, $optionKey) = explode(':', $compositeKey); $attribute = $attributesData[$attributeKey]; $keyIds[] = $attribute['options'][$optionKey]['id']; $configurableAttribute[] = sprintf('"%s":"%s"', $attribute['attribute_code'], $attribute['options'][$optionKey]['id']); } $keyIds = implode('-', $keyIds); $variation['configurable_attribute'] = '{' . implode(',', $configurableAttribute) . '}'; $result[$keyIds] = $variation; } return $result; }