コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function getList($entityTypeCode, \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 {
     if (!$entityTypeCode) {
         throw InputException::requiredField('entity_type_code');
     }
     /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Collection $attributeCollection */
     $attributeCollection = $this->attributeCollectionFactory->create();
     $attributeCollection->addFieldToFilter('entity_type_code', ['eq' => $entityTypeCode]);
     $attributeCollection->join(['entity_type' => $attributeCollection->getTable('eav_entity_type')], 'main_table.entity_type_id = entity_type.entity_type_id', []);
     $attributeCollection->join(['eav_entity_attribute' => $attributeCollection->getTable('eav_entity_attribute')], 'main_table.attribute_id = eav_entity_attribute.attribute_id', []);
     $attributeCollection->join(['additional_table' => $attributeCollection->getTable('catalog_eav_attribute')], 'main_table.attribute_id = additional_table.attribute_id', []);
     //Add filters from root filter group to the collection
     foreach ($searchCriteria->getFilterGroups() as $group) {
         $this->addFilterGroupToCollection($group, $attributeCollection);
     }
     /** @var \Magento\Framework\Api\SortOrder $sortOrder */
     foreach ((array) $searchCriteria->getSortOrders() as $sortOrder) {
         $attributeCollection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC');
     }
     $totalCount = $attributeCollection->getSize();
     // Group attributes by id to prevent duplicates with different attribute sets
     $attributeCollection->addAttributeGrouping();
     $attributeCollection->setCurPage($searchCriteria->getCurrentPage());
     $attributeCollection->setPageSize($searchCriteria->getPageSize());
     $attributes = [];
     /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
     foreach ($attributeCollection as $attribute) {
         $attributes[] = $this->get($entityTypeCode, $attribute->getAttributeCode());
     }
     $searchResults = $this->searchResultsFactory->create();
     $searchResults->setSearchCriteria($searchCriteria);
     $searchResults->setItems($attributes);
     $searchResults->setTotalCount($totalCount);
     return $searchResults;
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
 {
     $attributeSetId = $this->retrieveAttributeSetIdFromSearchCriteria($searchCriteria);
     if (!$attributeSetId) {
         throw InputException::requiredField('attribute_set_id');
     }
     try {
         $this->setRepository->get($attributeSetId);
     } catch (\Exception $exception) {
         throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
     }
     $collection = $this->groupListFactory->create();
     $collection->setAttributeSetFilter($attributeSetId);
     $collection->setSortOrder();
     $searchResult = $this->searchResultsFactory->create();
     $searchResult->setSearchCriteria($searchCriteria);
     $searchResult->setItems($collection->getItems());
     $searchResult->setTotalCount($collection->getSize());
     return $searchResult;
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function setProductLinks($sku, array $items)
 {
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     // Check if product link type is set and correct
     if (!empty($items)) {
         foreach ($items as $newLink) {
             $type = $newLink->getLinkType();
             if ($type == null) {
                 throw InputException::requiredField("linkType");
             }
             if (!isset($linkTypes[$type])) {
                 throw new NoSuchEntityException(__('Provided link type "%1" does not exist', $type));
             }
         }
     }
     $product = $this->productRepository->get($sku);
     // Replace only links of the specified type
     $existingLinks = $product->getProductLinks();
     $newLinks = [];
     if (!empty($existingLinks)) {
         foreach ($existingLinks as $link) {
             if ($link->getLinkType() != $type) {
                 $newLinks[] = $link;
             }
         }
         $newLinks = array_merge($newLinks, $items);
     } else {
         $newLinks = $items;
     }
     $product->setProductLinks($newLinks);
     try {
         $this->productRepository->save($product);
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return true;
 }
コード例 #4
0
ファイル: Repository.php プロジェクト: nja78/magento2
 /**
  * {@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;
 }
コード例 #5
0
ファイル: WriteService.php プロジェクト: aiesh/magento2
 /**
  * {@inheritdoc}
  */
 public function update(AttributeSet $attributeSetData)
 {
     if (!$attributeSetData->getId()) {
         throw InputException::requiredField('id');
     }
     $attributeSetModel = $this->setFactory->create()->load($attributeSetData->getId());
     $requiredEntityTypeId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
     if (!$attributeSetModel->getId() || $attributeSetModel->getEntityTypeId() != $requiredEntityTypeId) {
         throw NoSuchEntityException::singleField('id', $attributeSetData->getId());
     }
     $attributeSetModel->setAttributeSetName($attributeSetData->getName());
     $attributeSetModel->setSortOrder($attributeSetData->getSortOrder());
     $attributeSetModel->save();
     return $attributeSetModel->getId();
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function create(\Magento\Catalog\Service\V1\Data\Eav\AttributeMetadata $attributeMetadata)
 {
     /**
      * @var $model \Magento\Catalog\Model\Resource\Eav\Attribute
      */
     $model = $this->attributeFactory->create();
     $data = $attributeMetadata->__toArray();
     // unset attribute id because we create new attribute (does not rewrite existing one)
     unset($data[AttributeMetadata::ATTRIBUTE_ID]);
     // define frontend label
     if (!$attributeMetadata->getFrontendLabel()) {
         throw InputException::requiredField(AttributeMetadata::FRONTEND_LABEL);
     }
     $data[AttributeMetadata::FRONTEND_LABEL] = [];
     foreach ($attributeMetadata->getFrontendLabel() as $label) {
         $data[AttributeMetadata::FRONTEND_LABEL][$label->getStoreId()] = $label->getLabel();
     }
     if (!isset($data[AttributeMetadata::FRONTEND_LABEL][0]) || !$data[AttributeMetadata::FRONTEND_LABEL][0]) {
         throw InputException::invalidFieldValue(AttributeMetadata::FRONTEND_LABEL, null);
     }
     $data[AttributeMetadata::ATTRIBUTE_CODE] = $attributeMetadata->getAttributeCode() ?: $this->generateCode($data[AttributeMetadata::FRONTEND_LABEL][0]);
     $this->validateCode($data[AttributeMetadata::ATTRIBUTE_CODE]);
     $this->validateFrontendInput($attributeMetadata->getFrontendInput());
     $data[AttributeMetadata::BACKEND_TYPE] = $model->getBackendTypeByInput($attributeMetadata->getFrontendInput());
     $data[AttributeMetadata::SOURCE_MODEL] = $this->helper->getAttributeSourceModelByInputType($attributeMetadata->getFrontendInput());
     $data[AttributeMetadata::BACKEND_MODEL] = $this->helper->getAttributeBackendModelByInputType($attributeMetadata->getFrontendInput());
     $model->addData($data);
     $model->setEntityTypeId($this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId())->setIsUserDefined(1);
     return $model->save()->getAttributeCode();
 }