/**
  * Exports the item variants
  */
 protected function exportVariants()
 {
     // Verknüpfung mit den Attribut(-werten)
     $ConfiguratorSet = $this->SHOPWARE_Article->getConfiguratorSet();
     if (!$ConfiguratorSet instanceof Shopware\Models\Article\Configurator\Set) {
         return;
     }
     // Active the attribute values at the item --------------------------------------------------------------------
     $objectsActivateLinks = array();
     $Request_SetItemAttributeLinks = new PlentySoapRequest_SetItemAttributeLinks();
     $Request_SetItemAttributeLinks->ItemID = $this->PLENTY_itemID;
     // int
     $Request_SetItemAttributeLinks->AttributeLinks = array();
     /** @var Shopware\Models\Article\Configurator\Option $ConfiguratorOption */
     foreach ($ConfiguratorSet->getOptions() as $ConfiguratorOption) {
         $PLENTY_attributeID = PlentymarketsMappingController::getAttributeGroupByShopwareID($ConfiguratorOption->getGroup()->getId());
         $PLENTY_attributeValueID = PlentymarketsMappingController::getAttributeOptionByShopwareID($ConfiguratorOption->getId());
         $Object_AttributeLink = new PlentySoapObject_AttributeLink();
         $Object_AttributeLink->AttributeID = $PLENTY_attributeID;
         // int
         $Object_AttributeLink->AttributeValueID = $PLENTY_attributeValueID;
         // int
         $Object_AttributeLink->Activate = true;
         $objectsActivateLinks[] = $Object_AttributeLink;
     }
     // Run the calls
     foreach (array_chunk($objectsActivateLinks, 100) as $activateLinks) {
         $Request_SetItemAttributeLinks->AttributeLinks = $activateLinks;
         PlentymarketsSoapClient::getInstance()->SetItemAttributeLinks($Request_SetItemAttributeLinks);
     }
     // generate the attribute value sets --------------------------------------------------------------------------
     $objectsSetAttributeValueSets = array();
     $cacheAttributeValueSets = array();
     $Request_SetItemAttributeVariants = new PlentySoapRequest_SetItemAttributeVariants();
     $Request_SetItemAttributeVariants->ItemID = $this->PLENTY_itemID;
     // int
     $Request_SetItemAttributeVariants->SetAttributeValueSets = array();
     $Details = $this->SHOPWARE_Article->getDetails();
     /**
      * @var Shopware\Models\Article\Detail $ItemVariation
      * @var Shopware\Models\Article\Configurator\Option $ConfiguratorOption
      */
     foreach ($Details as $ItemVariation) {
         $cacheAttributeValueSets[$ItemVariation->getId()] = array();
         $Object_AttributeVariantList = new PlentySoapObject_AttributeVariantList();
         foreach ($ItemVariation->getConfiguratorOptions() as $ConfiguratorOption) {
             $PLENTY_attributeValueID = PlentymarketsMappingController::getAttributeOptionByShopwareID($ConfiguratorOption->getId());
             $cacheAttributeValueSets[$ItemVariation->getId()][] = $PLENTY_attributeValueID;
             $Object_Integer = new PlentySoapObject_Integer();
             $Object_Integer->intValue = $PLENTY_attributeValueID;
             $Object_AttributeVariantList->AttributeValueIDs[] = $Object_Integer;
         }
         $objectsSetAttributeValueSets[] = $Object_AttributeVariantList;
     }
     foreach (array_chunk($objectsSetAttributeValueSets, 100) as $setAttributeValueSets) {
         // Complete the request
         $Request_SetItemAttributeVariants->SetAttributeValueSets = $setAttributeValueSets;
         // and go for it
         $Response_SetItemAttributeVariants = PlentymarketsSoapClient::getInstance()->SetItemAttributeVariants($Request_SetItemAttributeVariants);
         // Matching der Varianten
         foreach ($Response_SetItemAttributeVariants->ResponseMessages->item as $ResponseMessage) {
             if ($ResponseMessage->IdentificationKey != 'AttributeValueIDs') {
                 continue;
             }
             // If there is an error message, go ahead
             if (!is_null($ResponseMessage->ErrorMessages)) {
                 continue;
             }
             $PLENTY_attributeValueIDs = array_map('intval', explode(';', $ResponseMessage->IdentificationValue));
             $PLENTY_variantID = (int) $ResponseMessage->SuccessMessages->item[0]->Value;
             foreach ($cacheAttributeValueSets as $SHOPWARE_variantID => $attributeValueIDs) {
                 if (PlentymarketsUtils::arraysAreEqual($attributeValueIDs, $PLENTY_attributeValueIDs)) {
                     PlentymarketsMappingController::addItemVariant($SHOPWARE_variantID, sprintf('%s-%s-%s', $this->PLENTY_itemID, $this->PLENTY_priceID, $PLENTY_variantID));
                     break;
                 }
             }
         }
     }
     // Set the variation details ----------------------------------------------------------------------------------
     $objectsAttributeValueSetsDetails = array();
     // start the request
     $Request_SetAttributeValueSetsDetails = new PlentySoapRequest_SetAttributeValueSetsDetails();
     $Request_SetAttributeValueSetsDetails->AttributeValueSetsDetails = array();
     /** @var Shopware\Models\Article\Detail $ItemVariation */
     foreach ($Details as $ItemVariation) {
         try {
             $sku = PlentymarketsMappingController::getItemVariantByShopwareID($ItemVariation->getId());
         } catch (PlentymarketsMappingExceptionNotExistant $E) {
             // Roll back the item
             $this->rollback();
             // and quit
             throw new PlentymarketsExportException('The item variation with the number »' . $ItemVariation->getNumber() . '« could not be created (corrupt data)', 2880);
         }
         $Object_SetAttributeValueSetsDetails = new PlentySoapObject_SetAttributeValueSetsDetails();
         $Object_SetAttributeValueSetsDetails->Availability = $ItemVariation->getActive();
         // int
         $Object_SetAttributeValueSetsDetails->EAN1 = $ItemVariation->getEan();
         // string
         $Object_SetAttributeValueSetsDetails->MaxStock = null;
         // float
         $Object_SetAttributeValueSetsDetails->PurchasePrice = null;
         // float
         $Object_SetAttributeValueSetsDetails->SKU = $sku;
         $Object_SetAttributeValueSetsDetails->Variantnumber = $ItemVariation->getNumber();
         // string
         $objectsAttributeValueSetsDetails[] = $Object_SetAttributeValueSetsDetails;
     }
     foreach (array_chunk($objectsAttributeValueSetsDetails, 50) as $attributeValueSetsDetails) {
         $Request_SetAttributeValueSetsDetails->AttributeValueSetsDetails = $attributeValueSetsDetails;
         PlentymarketsSoapClient::getInstance()->SetAttributeValueSetsDetails($Request_SetAttributeValueSetsDetails);
     }
 }
Example #2
0
    /**
     * @param array $data
     * @param \Shopware\Models\Article\Article $article
     * @throws \Shopware\Components\Api\Exception\CustomValidationException
     * @throws \Exception
     * @return array
     */
    protected function prepareConfiguratorSet($data, ArticleModel $article)
    {
        if (!isset($data['configuratorSet'])) {
            return $data;
        }

        $configuratorSet = $article->getConfiguratorSet();
        if (!$configuratorSet) {
            $configuratorSet = new \Shopware\Models\Article\Configurator\Set();
            if (isset($data['mainDetail']['number'])) {
                $number = $data['mainDetail']['number'];
            } else {
                $number = $article->getMainDetail()->getNumber();
            }

            $configuratorSet->setName('Set-' . $number);
            $configuratorSet->setPublic(false);
        }

        if (isset($data['configuratorSet']['type'])) {
            $configuratorSet->setType($data['configuratorSet']['type']);
        }

        if (isset($data['configuratorSet']['name'])) {
            $configuratorSet->setName($data['configuratorSet']['name']);
        }

        $allOptions = array();
        $allGroups = array();

        $groupPosition = 0;

        foreach ($data['configuratorSet']['groups'] as $groupData) {
            $group = null;
            if (isset($groupData['id'])) {
                $group = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Group')->find($groupData['id']);
                if (!$group) {
                    throw new ApiException\CustomValidationException(sprintf("ConfiguratorGroup by id %s not found", $groupData['id']));
                }
            } elseif (isset($groupData['name'])) {
                $group = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Group')->findOneBy(array('name' => $groupData['name']));

                if (!$group) {
                    $group = new \Shopware\Models\Article\Configurator\Group();
                    $group->setPosition($groupPosition);
                }
            } else {
                throw new ApiException\CustomValidationException('At least the groupname is required');
            }

            $groupOptions = array();
            $optionPosition = 0;
            foreach ($groupData['options'] as $optionData) {
                $option = null;
                if ($group->getId() > 0) {
                    $option = $this->getManager()->getRepository('Shopware\Models\Article\Configurator\Option')->findOneBy(array(
                        'name'    => $optionData['name'],
                        'groupId' => $group->getId()
                    ));
                }

                if (!$option) {
                    $option = new \Shopware\Models\Article\Configurator\Option();
                }

                $option->fromArray($optionData);
                $option->setGroup($group);
                $option->setPosition($optionPosition++);
                $allOptions[]   = $option;
                $groupOptions[] = $option;
            }

            $groupData['options'] = $groupOptions;
            $group->fromArray($groupData);
            $allGroups[] = $group;
        }

        $configuratorSet->setOptions($allOptions);
        $configuratorSet->setGroups($allGroups);

        $data['configuratorSet'] = $configuratorSet;

        return $data;
    }
Example #3
0
 /**
  * Internal helper function to remove all article variants for the deselected options.
  * @param \Shopware\Models\Article\Article $article
  * @param array                            $selectedOptions
  *
  */
 protected function deleteVariantsForAllDeactivatedOptions($article, $selectedOptions)
 {
     $configuratorSet = $article->getConfiguratorSet();
     $oldOptions = $configuratorSet->getOptions();
     $ids = array();
     /**@var $oldOption \Shopware\Models\Article\Configurator\Option*/
     foreach ($oldOptions as $oldOption) {
         if (!array_key_exists($oldOption->getId(), $selectedOptions)) {
             $details = $this->getRepository()->getArticleDetailByConfiguratorOptionIdQuery($article->getId(), $oldOption->getId())->setHydrationMode(\Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT)->getResult();
             if (!empty($details)) {
                 /**@var $detail \Shopware\Models\Article\Detail*/
                 foreach ($details as $detail) {
                     if ($detail->getKind() === 1) {
                         $article->setMainDetail(null);
                     }
                     $ids[] = $detail->getId();
                     Shopware()->Models()->remove($detail);
                 }
                 Shopware()->Models()->flush();
             }
         }
     }
     if (!empty($ids)) {
         $builder = Shopware()->Models()->createQueryBuilder();
         $builder->delete('Shopware\\Models\\Attribute\\Article', 'attribute')->where('attribute.articleDetailId IN (:articleDetailIds)')->setParameters(array('articleDetailIds' => $ids))->getQuery()->execute();
     }
 }
Example #4
0
 /**
  * Resolves the passed configuratorOptions parameter for a single variant.
  * Each passed configurator option, has to be configured in the article configurator set.
  *
  * @param array $data
  * @param ArticleModel $article
  * @param Detail $variant
  * @return \Doctrine\Common\Collections\Collection
  * @throws \Shopware\Components\Api\Exception\CustomValidationException
  */
 protected function prepareConfigurator(array $data, ArticleModel $article, Detail $variant)
 {
     if (!$article->getConfiguratorSet()) {
         throw new ApiException\CustomValidationException('A configurator set has to be defined');
     }
     $availableGroups = $article->getConfiguratorSet()->getGroups();
     $options = new ArrayCollection();
     foreach ($data['configuratorOptions'] as $optionData) {
         $availableGroup = $this->getAvailableGroup($availableGroups, array('id' => $optionData['groupId'], 'name' => $optionData['group']));
         //group is in the article configurator set configured?
         if (!$availableGroup) {
             continue;
         }
         //check if the option is available in the configured article configurator set.
         $option = $this->getAvailableOption($availableGroup->getOptions(), array('id' => $optionData['optionId'], 'name' => $optionData['option']));
         if (!$option) {
             if (!$optionData['option']) {
                 throw new ApiException\CustomValidationException('A new configurator option requires a name');
             }
             $option = new Option();
             $option->setPosition(0);
             $option->setName($optionData['option']);
             $option->setGroup($availableGroup);
             $this->getManager()->persist($option);
         }
         $options->add($option);
     }
     $data['configuratorOptions'] = $options;
     $variant->setConfiguratorOptions($options);
     return $data;
 }
Example #5
0
    /**
     * Internal helper function to remove all article variants for the deselected options.
     * @param \Shopware\Models\Article\Article $article
     * @param array $selectedOptions
     *
     */
    protected  function deleteVariantsForAllDeactivatedOptions($article, $selectedOptions)
    {
        $configuratorSet = $article->getConfiguratorSet();
        $oldOptions = $configuratorSet->getOptions();

        /**@var $oldOption \Shopware\Models\Article\Configurator\Option*/
        foreach($oldOptions as $oldOption) {
            if (!array_key_exists($oldOption->getId(), $selectedOptions)) {
                $details = $this->getRepository()
                                ->getArticleDetailByConfiguratorOptionIdQuery($article->getId(), $oldOption->getId())
                                ->setHydrationMode(\Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT)
                                ->getResult();

                foreach($details as $detail) {
                    Shopware()->Models()->remove($detail);
                }

                Shopware()->Models()->flush();
            }
        }
    }
 /**
  * {@inheritDoc}
  */
 public function getConfiguratorSet()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getConfiguratorSet', array());
     return parent::getConfiguratorSet();
 }
Example #7
0
 /**
  * Creates the article image mappings for the passed article and image entity.
  * The mappings parameter contains a multi dimensional array with configurator options.
  * The first level of the mappings defines the OR conditions of the image mappings.
  * The second level of the mappings defines a single rule.
  * Example:
  * $mappings = array(
  *    array(
  *       array('name' => 'red')
  *       AND
  *       array('name' => 'small')
  *    )
  *    //OR
  *    array('name' => 'blue')
  * )
  *
  * @param Image $image
  * @param ArticleModel $article
  * @param array $mappings
  * @throws \Shopware\Components\Api\Exception\CustomValidationException
  */
 protected function createImageMappings(Image $image, ArticleModel $article, array $mappings)
 {
     if (!$article->getConfiguratorSet()) {
         throw new ApiException\CustomValidationException("Article is no configurator article. Image mapping can only be created on configurator articles");
     }
     $configuratorOptions = $article->getConfiguratorSet()->getOptions();
     foreach ($mappings as $mappingData) {
         $options = new ArrayCollection();
         foreach ($mappingData as $option) {
             $available = $this->getCollectionElementByProperties($configuratorOptions, ['id' => $option['id'], 'name' => $option['name']]);
             if (!$available) {
                 $property = $option['id'] ? $option['id'] : $option['name'];
                 throw new ApiException\CustomValidationException(sprintf("Passed option %s do not exist in the configurator set of the article", $property));
             }
             $options->add($available);
         }
         if (empty($options)) {
             throw new ApiException\CustomValidationException("No available option exists");
         }
         $this->getVariantResource()->createImageMappingForOptions($options, $image);
     }
 }