/** * Get configurable product options * * @param FixtureInterface|null $product [optional] * @return array * @throws \Exception */ public function getOptions(FixtureInterface $product) { /** @var ConfigurableProduct $product */ $attributesData = $product->hasData('configurable_attributes_data') ? $product->getConfigurableAttributesData()['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 SimpleElement $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'; foreach ($optionData['options'] as $key => $value) { $optionData['options'][$key]['price'] = $this->getOptionPrice($title, $value['title']); } $result[$title] = $optionData; } return $result; }
/** * 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(); $assignedProducts = $configurableAttributesData->getProducts(); $matrixData = $product->getConfigurableAttributesData()['matrix']; $result = []; foreach ($matrixData as $variationKey => $variation) { // For assigned products doesn't send data about them if (isset($assignedProducts[$variationKey])) { continue; } $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; }
/** * Get configurable attributes options prices * * @param FixtureInterface $product * @return array */ public function getOptionsPrices(FixtureInterface $product) { /** @var ConfigurableProduct $product */ $attributesData = []; $productVariations = []; if ($product->hasData('configurable_attributes_data')) { $attributesData = $product->getConfigurableAttributesData()['attributes_data']; $productVariations = $product->getConfigurableAttributesData()['matrix']; } $productVariations = array_keys($productVariations); $result = []; foreach ($productVariations as $variation) { $variationOptions = explode(' ', $variation); $result[$variation]['price'] = $this->getOptionPrice($variationOptions, $attributesData); } return $result; }
/** * Fill in the option specified for the product * * @param FixtureInterface $product * @return void */ public function fillOptions(FixtureInterface $product) { /** @var ConfigurableProduct $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']]; } $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->getCustomOptionsBlock()->fillCustomOptions($checkoutOptionsData); parent::fillOptions($product); }
/** * 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 ConfigurableProduct) { 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['options']['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; }