/** * ES field used in attribute autocomplete. * * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute $attribute Attribute. * * @return string */ public function getAttributeAutocompleteField(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute) { $fieldName = $attribute->getAttributeCode(); if ($attribute->usesSource()) { $fieldName = $this->mappingHelper->getOptionTextFieldName($fieldName); } return $fieldName; }
/** * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context * @param array $config * @return \Magento\Ui\Component\Listing\Columns\ColumnInterface */ public function create($attribute, $context, array $config = []) { $columnName = $attribute->getAttributeCode(); $config = array_merge(['label' => __($attribute->getDefaultFrontendLabel()), 'dataType' => $this->getDataType($attribute), 'sorting' => 'asc', 'align' => 'left', 'add_field' => true, 'visible' => $attribute->getIsVisibleInGrid()], $config); if ($attribute->usesSource()) { $config['options'] = $attribute->getSource()->getAllOptions(); } $arguments = ['data' => ['js_config' => ['component' => $this->getJsComponent($config['dataType'])], 'config' => $config], 'context' => $context]; return $this->componentFactory->create($columnName, 'column', $arguments); }
/** * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context * @param array $config * @return \Magento\Ui\Component\Listing\Columns\ColumnInterface */ public function create($attribute, $context, array $config = []) { $columnName = $attribute->getAttributeCode(); $config = array_merge(['label' => __($attribute->getDefaultFrontendLabel()), 'dataType' => $this->getDataType($attribute), 'add_field' => true, 'visible' => $attribute->getIsVisibleInGrid(), 'filter' => $attribute->getIsFilterableInGrid() ? $this->getFilterType($attribute->getFrontendInput()) : null], $config); if ($attribute->usesSource()) { $config['options'] = $attribute->getSource()->getAllOptions(); } $config['component'] = $this->getJsComponent($config['dataType']); $arguments = ['data' => ['config' => $config], 'context' => $context]; return $this->componentFactory->create($columnName, 'column', $arguments); }
/** * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $attribute * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context * @param array $config * @return \Magento\Ui\Component\Listing\Columns\ColumnInterface */ public function create($attribute, $context, $config = []) { $columnName = $attribute->getAttributeCode(); $config = array_merge(['dataScope' => $columnName, 'label' => __($attribute->getDefaultFrontendLabel())], $config); if ($attribute->usesSource() && $attribute->getSourceModel()) { $config['options'] = $attribute->getSource()->getAllOptions(); $config['caption'] = __('Select...'); } $arguments = ['data' => ['config' => $config], 'context' => $context]; return $this->componentFactory->create($columnName, $this->getFilterType($attribute), $arguments); }
/** * {@inheritdoc} * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function save(\Magento\Catalog\Api\Data\ProductAttributeInterface $attribute) { if ($attribute->getAttributeId()) { $existingModel = $this->get($attribute->getAttributeCode()); if (!$existingModel->getAttributeId()) { throw NoSuchEntityException::singleField('attribute_code', $existingModel->getAttributeCode()); } $attribute->setAttributeId($existingModel->getAttributeId()); $attribute->setIsUserDefined($existingModel->getIsUserDefined()); $attribute->setFrontendInput($existingModel->getFrontendInput()); if (is_array($attribute->getFrontendLabels())) { $frontendLabel[0] = $existingModel->getDefaultFrontendLabel(); foreach ($attribute->getFrontendLabels() as $item) { $frontendLabel[$item->getStoreId()] = $item->getLabel(); } $attribute->setDefaultFrontendLabel($frontendLabel); } if (!$attribute->getIsUserDefined()) { // Unset attribute field for system attributes $attribute->setApplyTo(null); } } else { $attribute->setAttributeId(null); if (!$attribute->getFrontendLabels() && !$attribute->getDefaultFrontendLabel()) { throw InputException::requiredField('frontend_label'); } $frontendLabels = []; if ($attribute->getDefaultFrontendLabel()) { $frontendLabels[0] = $attribute->getDefaultFrontendLabel(); } if ($attribute->getFrontendLabels() && is_array($attribute->getFrontendLabels())) { foreach ($attribute->getFrontendLabels() as $label) { $frontendLabels[$label->getStoreId()] = $label->getLabel(); } if (!isset($frontendLabels[0]) || !$frontendLabels[0]) { throw InputException::invalidFieldValue('frontend_label', null); } $attribute->setDefaultFrontendLabel($frontendLabels); } $attribute->setAttributeCode($attribute->getAttributeCode() ?: $this->generateCode($frontendLabels[0])); $this->validateCode($attribute->getAttributeCode()); $this->validateFrontendInput($attribute->getFrontendInput()); $attribute->setBackendType($attribute->getBackendTypeByInput($attribute->getFrontendInput())); $attribute->setSourceModel($this->productHelper->getAttributeSourceModelByInputType($attribute->getFrontendInput())); $attribute->setBackendModel($this->productHelper->getAttributeBackendModelByInputType($attribute->getFrontendInput())); $attribute->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE)->getId()); $attribute->setIsUserDefined(1); } $this->attributeResource->save($attribute); return $attribute; }
/** * Whether attribute can have default value * * @param ProductAttributeInterface $attribute * @return bool */ private function canDisplayUseDefault(ProductAttributeInterface $attribute) { $attributeCode = $attribute->getAttributeCode(); /** @var Product $product */ $product = $this->locator->getProduct(); if (isset($this->canDisplayUseDefault[$attributeCode])) { return $this->canDisplayUseDefault[$attributeCode]; } return $this->canDisplayUseDefault[$attributeCode] = $attribute->getScope() != ProductAttributeInterface::SCOPE_GLOBAL_TEXT && $product && $product->getId() && $product->getStoreId(); }
/** * Generates child products for configurable product * * @param \Magento\Catalog\Api\Data\ProductAttributeInterface $configurableAttribute * @return array * @throws \Exception */ protected function createConfigurableChildren($configurableAttribute) { $availableOptions = $configurableAttribute->getOptions(); /* Not sure why the zero index has no value for all attributes. Maybe will be fixed in next Magento versions */ unset($availableOptions[0]); if (!count($availableOptions) > 0) { throw new \Exception('The selected configurable attribute has no values'); } // Create child simple products $availableProductsCount = $this->getCount() - $this->processedProducts - 1; if ($availableProductsCount >= self::CONFIGURABLE_CHILD_LIMIT) { $childrenLimit = self::CONFIGURABLE_CHILD_LIMIT; } else { $childrenLimit = $availableProductsCount; } if ($childrenLimit > count($availableOptions)) { $childrenLimit = count($availableOptions); } $childrenCount = rand(1, $childrenLimit); $childProductsIds = $configurableOptionsValues = []; for ($optCount = 0; $optCount < $childrenCount; $optCount++) { $product = $this->createSimpleProduct(true, true); $currentOptionId = array_rand($availableOptions); $optValueId = $availableOptions[$currentOptionId]->getValue(); unset($availableOptions[$currentOptionId]); $product->setCustomAttribute($configurableAttribute->getAttributeCode(), $optValueId); $optionValue = $this->optionValue; $optionValue->setValueIndex($optValueId); $configurableOptionsValues[] = $optionValue; $product = $this->productRepository->save($product); $childProductsIds[] = $product->getId(); } return ['child_products_ids' => $childProductsIds, 'configurable_options_values' => $configurableOptionsValues]; }