/** * Exports the item properties */ protected function exportProperties() { $Request_SetPropertiesToItem = new PlentySoapRequest_SetPropertiesToItem(); $Request_SetPropertiesToItem->PropertyToItemList = array(); // max 50 $PropertyGroup = $this->SHOPWARE_Article->getPropertyGroup(); if (!$PropertyGroup instanceof \Shopware\Models\Property\Group) { return; } /** @var Shopware\Models\Property\Value $PropertyValue */ foreach ($this->SHOPWARE_Article->getPropertyValues() as $PropertyValue) { $PropertyOption = $PropertyValue->getOption(); try { $PLENTY_propertyID = PlentymarketsMappingController::getPropertyByShopwareID($PropertyGroup->getId() . ';' . $PropertyOption->getId()); } catch (PlentymarketsMappingExceptionNotExistant $E) { PlentymarketsLogger::getInstance()->error('Export:Initial:Item:Property', 'The property »' . $PropertyGroup->getName() . ' → ' . $PropertyOption->getName() . '« could not be added to the item with the number »' . $this->SHOPWARE_Article->getMainDetail()->getNumber() . '«', 2870); continue; } $Object_SetPropertyToItem = new PlentySoapObject_SetPropertyToItem(); $Object_SetPropertyToItem->ItemId = $this->PLENTY_itemID; // int $Object_SetPropertyToItem->Lang = 'de'; // string $Object_SetPropertyToItem->PropertyId = $PLENTY_propertyID; // int $Object_SetPropertyToItem->PropertyItemValue = $PropertyValue->getValue(); // string $Request_SetPropertiesToItem->PropertyToItemList[] = $Object_SetPropertyToItem; $this->exportPropertyValueTranslations($PropertyValue->getId(), $PLENTY_propertyID); } PlentymarketsSoapClient::getInstance()->SetPropertiesToItem($Request_SetPropertiesToItem); }
/** * @param array $data * @param \Shopware\Models\Article\Article $article * @throws \Shopware\Components\Api\Exception\CustomValidationException * @return array */ protected function preparePropertyValuesData($data, ArticleModel $article) { if (!isset($data['propertyValues'])) { return $data; } // remove assigned values if (empty($data['propertyValues'])) { return $data; } if (isset($data['propertyGroup'])) { $propertyGroup = $data['propertyGroup']; } else { $propertyGroup = $article->getPropertyGroup(); } if (!$propertyGroup instanceof \Shopware\Models\Property\Group) { throw new ApiException\CustomValidationException(sprintf("There is no filterGroup specified")); } $models = array(); foreach ($data['propertyValues'] as $valueData) { if (empty($valueData['id'])) { continue; } $model = $this->getManager()->find('Shopware\Models\Property\Value', $valueData['id']); if (!$model instanceof \Shopware\Models\Property\Value) { throw new ApiException\CustomValidationException(sprintf("Property Value by id %s not found", $valueData['id'])); } $models[] = $model; } $data['propertyValues'] = $models; return $data; }
/** * {@inheritDoc} */ public function getPropertyGroup() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getPropertyGroup', array()); return parent::getPropertyGroup(); }
/** * @param array $data * @param \Shopware\Models\Article\Article $article * @throws \Shopware\Components\Api\Exception\CustomValidationException * @return array */ protected function preparePropertyValuesData($data, ArticleModel $article) { if (!isset($data['propertyValues'])) { return $data; } // remove assigned values if (empty($data['propertyValues'])) { return $data; } $propertyRepository = $this->getManager()->getRepository('Shopware\\Models\\Property\\Group'); /** * Get group - this is required. */ if (isset($data['propertyGroup'])) { $propertyGroup = $data['propertyGroup']; } else { $propertyGroup = $article->getPropertyGroup(); } if (!$propertyGroup instanceof \Shopware\Models\Property\Group) { throw new ApiException\CustomValidationException(sprintf("There is no propertyGroup specified")); } $models = []; foreach ($data['propertyValues'] as $valueData) { $value = null; /** @var \Shopware\Models\Property\Option $option */ $option = null; // Get value by id if (isset($valueData['id'])) { $value = $this->getManager()->getRepository('\\Shopware\\Models\\Property\\Value')->find($valueData['id']); if (!$value) { throw new ApiException\CustomValidationException(sprintf("Property value by id %s not found", $valueData['id'])); } // Get / create value by name } elseif (isset($valueData['value'])) { //get option if (isset($valueData['option'])) { // get option by id if (isset($valueData['option']['id'])) { $option = $this->getManager()->getRepository('\\Shopware\\Models\\Property\\Option')->find($valueData['option']['id']); if (!$option) { throw new ApiException\CustomValidationException(sprintf("Property option by id %s not found", $valueData['option']['id'])); } $filters = array(array('property' => "options.id", 'expression' => '=', 'value' => $option->getId()), array('property' => "groups.id", 'expression' => '=', 'value' => $propertyGroup->getId())); $query = $propertyRepository->getPropertyRelationQuery($filters, null, 1, 0); /** @var \Shopware\Models\Property\Relation $relation */ $relation = $query->getOneOrNullResult(self::HYDRATE_OBJECT); if (!$relation) { $propertyGroup->addOption($option); } // get/create option depending on associated filtergroups } elseif (isset($valueData['option']['name'])) { // if a name is passed and there is a matching option/group relation, get this option // if only a name is passed, create a new option $filters = [['property' => "options.name", 'expression' => '=', 'value' => $valueData['option']['name']], ['property' => "groups.name", 'expression' => '=', 'value' => $propertyGroup->getName()]]; $query = $propertyRepository->getPropertyRelationQuery($filters, null, 1, 0); /** @var \Shopware\Models\Property\Relation $relation */ $relation = $query->getOneOrNullResult(self::HYDRATE_OBJECT); if (!$relation) { //checks if a new option was created //because the new option is not written to the database at this point $groupOption = $this->getCollectionElementByProperty($propertyGroup->getOptions(), 'name', $valueData['option']['name']); //creates a new option if ($groupOption === null) { $option = new \Shopware\Models\Property\Option(); $propertyGroup->addOption($option); } else { $option = $groupOption; } } else { $option = $relation->getOption(); } } else { throw new ApiException\CustomValidationException("A property option need to be given for each property value"); } $option->fromArray($valueData['option']); if ($option->isFilterable() === null) { $option->setFilterable(false); } } else { throw new ApiException\CustomValidationException("A property option need to be given for each property value"); } // create the value // If there is a filter value with matching name and option, load this value, else create a new one $value = $this->getManager()->getRepository('\\Shopware\\Models\\Property\\Value')->findOneBy(['value' => $valueData['value'], 'optionId' => $option->getId()]); if (!$value) { $value = new \Shopware\Models\Property\Value($option, $valueData['value']); } if (isset($valueData['position'])) { $value->setPosition($valueData['position']); } $this->getManager()->persist($value); } else { throw new ApiException\CustomValidationException("Name or id for property value required"); } $models[] = $value; } $data['propertyValues'] = $models; return $data; }