/**
     * Retrieves the images from plentymarkets and adds them to the item.
     *
     * @return number
     */
    public function image()
    {
        $plentyId2ShopwareId = array();
        $shopwareId2PlentyPosition = array();
        $Request_GetItemsImages = new PlentySoapRequest_GetItemsImages();
        $Request_GetItemsImages->Page = 0;
        $Request_GetItemsImages->SKU = $this->PLENTY_itemId;
        // Cleanup
        $this->purge();
        /**
         * @var Media $mediaResource
         */
        $mediaResource = \Shopware\Components\Api\Manager::getResource('Media');
        do {
            /**
             * @var PlentySoapResponse_GetItemsImages $Response_GetItemsImages
             */
            $Response_GetItemsImages = PlentymarketsSoapClient::getInstance()->GetItemsImages($Request_GetItemsImages);
            if ($Response_GetItemsImages->Success == false) {
                PlentymarketsLogger::getInstance()->error('Sync:Item:Image', 'The images for the plentymarkets item id »' . $this->PLENTY_itemId . '« could not be retrieved', 3200);
                continue;
            }
            /**
             * @var $Image PlentySoapResponse_ObjectItemImage
             */
            foreach ($Response_GetItemsImages->ItemsImages->item as $Image) {
                $shopwareStoreIds = array();
                /**
                 * @var $reference PlentySoapResponse_ObjectGetItemImageReference
                 */
                foreach ($Image->References->item as $reference) {
                    if ($reference->ReferenceType == 'Mandant') {
                        try {
                            if (PlentymarketsMappingController::getShopByPlentyID($reference->ReferenceValue) > 0) {
                                $shopwareStoreId = PlentymarketsMappingController::getShopByPlentyID($reference->ReferenceValue);
                            }
                        } catch (PlentymarketsMappingExceptionNotExistant $E) {
                            continue;
                        }
                        if (isset($shopwareStoreId)) {
                            $shopwareStoreIds[] = $shopwareStoreId;
                        }
                    }
                }
                // Skip the image if it should not be shown
                if (empty($shopwareStoreIds)) {
                    continue;
                } else {
                    try {
                        $media = $this->updateMedia($Image, $mediaResource);
                    } catch (PlentymarketsMappingExceptionNotExistant $e) {
                        $media = $this->createMedia($Image, $mediaResource);
                    } catch (NotFoundException $e) {
                        $media = $this->createMedia($Image, $mediaResource);
                    } catch (Exception $e) {
                        PlentymarketsLogger::getInstance()->error('Sync:Item:Image', $e->getMessage(), 3200);
                        continue;
                    }
                    $image = new Image();
                    $image->setMain(2);
                    $image->setMedia($media);
                    $image->setPath($media->getName());
                    $image->setExtension($media->getExtension());
                    // get the main language of the shop
                    //$mainLangData = array_values(PlentymarketsTranslation::getInstance()->getShopMainLanguage($shopwareStoreId));
                    //$mainLang = PlentymarketsTranslation::getInstance()->getPlentyLocaleFormat($mainLangData[0]['locale']);
                    foreach ($Image->Names->item as $imageName) {
                        if ($imageName->Lang == 'de') {
                            // set the image title in German
                            $image->setDescription($imageName->Name);
                        }
                    }
                    if (!is_null(PlentymarketsConfig::getInstance()->getItemImageAltAttributeID()) && PlentymarketsConfig::getInstance()->getItemImageAltAttributeID() > 0 && PlentymarketsConfig::getInstance()->getItemImageAltAttributeID() <= 3) {
                        // get the attribute number for alternative text from connector's settings
                        $plenty_attributeID = PlentymarketsConfig::getInstance()->getItemImageAltAttributeID();
                        // set the value for the attribute number
                        $attribute = new \Shopware\Models\Attribute\ArticleImage();
                        call_user_func(array($attribute, 'setAttribute' . $plenty_attributeID), $Image->Names->item[0]->AlternativeText);
                        $image->setAttribute($attribute);
                    }
                    $image->setPosition($Image->Position);
                    // Generate the thumbnails
                    if (version_compare(Shopware::VERSION, '4.2') != 1) {
                        PlentymarketsImportItemImageThumbnailController::getInstance()->addMediaResource($media);
                    }
                    Shopware()->Models()->persist($image);
                    Shopware()->Models()->flush();
                    $imageId = $image->getId();
                    foreach ($shopwareStoreIds as $shopwareStoreId) {
                        // import the image title translations for all active shops of the image
                        $this->importImageTitleTranslation($imageId, $Image->Names->item, $shopwareStoreId);
                    }
                    $plentyId2ShopwareId[$Image->ImageID] = $imageId;
                    $shopwareId2PlentyPosition[$Image->Position] = $imageId;
                    Shopware()->DB()->query('
											UPDATE s_articles_img
												SET articleID = ?
												WHERE id = ?
										', array($this->SHOPWARE_itemId, $imageId));
                }
            }
        } while (++$Request_GetItemsImages->Page < $Response_GetItemsImages->Pages);
        if (empty($shopwareId2PlentyPosition)) {
            return;
        }
        ksort($shopwareId2PlentyPosition);
        $mainImageId = reset($shopwareId2PlentyPosition);
        if (empty($mainImageId)) {
            return;
        }
        /**
         * @var Shopware\Models\Article\Image $mainImage
         */
        $mainImage = Shopware()->Models()->find('Shopware\\Models\\Article\\Image', $mainImageId);
        $mainImage->setMain(1);
        Shopware()->Models()->persist($mainImage);
        Shopware()->Models()->flush();
        // Get the variant images
        $Request_GetItemsVariantImages = new PlentySoapRequest_GetItemsVariantImages();
        $Request_GetItemsVariantImages->Items = array();
        $RequestObject_GetItemsVariantImages = new PlentySoapRequestObject_GetItemsVariantImages();
        $RequestObject_GetItemsVariantImages->ItemID = $this->PLENTY_itemId;
        $Request_GetItemsVariantImages->Items[] = $RequestObject_GetItemsVariantImages;
        /**
         * @var PlentySoapResponse_GetItemsVariantImages $Response_GetItemsVariantImages
         */
        $Response_GetItemsVariantImages = PlentymarketsSoapClient::getInstance()->GetItemsVariantImages($Request_GetItemsVariantImages);
        foreach ($Response_GetItemsVariantImages->Images->item as $GetItemsVariantImagesImage) {
            try {
                $shopwareOptionId = PlentymarketsMappingController::getAttributeOptionByPlentyID($GetItemsVariantImagesImage->AttributeValueID);
                /**
                 * @var \Shopware\Models\Article\Configurator\Option $shopwareOption
                 */
                $shopwareOption = Shopware()->Models()->find('Shopware\\Models\\Article\\Configurator\\Option', $shopwareOptionId);
            } catch (PlentymarketsMappingExceptionNotExistant $e) {
                continue;
            }
            if (!isset($plentyId2ShopwareId[$GetItemsVariantImagesImage->ImageID])) {
                continue;
            }
            /** @var Shopware\Models\Article\Image $shopwareImage */
            $shopwareImageId = $plentyId2ShopwareId[$GetItemsVariantImagesImage->ImageID];
            /**
             * @var Image $shopwareImage
             */
            $shopwareImage = Shopware()->Models()->find('Shopware\\Models\\Article\\Image', $shopwareImageId);
            $mapping = new Shopware\Models\Article\Image\Mapping();
            $mapping->setImage($shopwareImage);
            $rule = new Shopware\Models\Article\Image\Rule();
            $rule->setMapping($mapping);
            $rule->setOption($shopwareOption);
            $mapping->getRules()->add($rule);
            $shopwareImage->setMappings($mapping);
            Shopware()->Models()->persist($mapping);
            $details = Shopware()->Db()->fetchCol('
				SELECT
						d.id
					FROM s_articles_details d
						INNER JOIN s_article_configurator_option_relations alias16 ON alias16.option_id = ' . $shopwareOptionId . ' AND alias16.article_id = d.id
					WHERE d.articleID = ' . $this->SHOPWARE_itemId . '
			');
            foreach ($details as $detailId) {
                /**
                 * Get the detail object
                 *
                 * @var Shopware\Models\Article\Detail $detail
                 */
                $detail = Shopware()->Models()->getReference('Shopware\\Models\\Article\\Detail', $detailId);
                // Create the variant image
                $variantImage = new Shopware\Models\Article\Image();
                $variantImage->setExtension($shopwareImage->getExtension());
                $variantImage->setMain($shopwareImage->getMain());
                $variantImage->setParent($shopwareImage);
                $variantImage->setArticleDetail($detail);
                // And persist it
                Shopware()->Models()->persist($variantImage);
            }
        }
        Shopware()->Db()->update('s_articles', array('changetime' => date('c', time())), 'id=' . $this->SHOPWARE_itemId);
        Shopware()->Models()->flush();
    }
 public function init()
 {
     $this->resource = \Shopware\Components\Api\Manager::getResource('article');
 }
 public function init()
 {
     $this->resource = \Shopware\Components\Api\Manager::getResource('translation');
 }
 /**
  * Update the prices for a variant
  *
  * @param integer $detailId
  * @return bool
  */
 public function updateVariant($detailId)
 {
     $Detail = Shopware()->Models()->find('Shopware\\Models\\Article\\Detail', $detailId);
     if (!$Detail instanceof Shopware\Models\Article\Detail) {
         return PlentymarketsLogger::getInstance()->error('Sync:Item:Price', 'The price of the item detail with the id »' . $detailId . '« could not be updated (item corrupt)', 3610);
     }
     $currentPrice = $this->PLENTY_PriceSet->Price + $this->PLENTY_markup;
     $Article = $Detail->getArticle();
     $ArticleResource = \Shopware\Components\Api\Manager::getResource('Article');
     // Update
     $ArticleResource->update($Article->getId(), array('variants' => array(array('number' => $Detail->getNumber(), 'prices' => $this->getPrices()))));
     PyLog()->message('Sync:Item:Price', 'The price of the variant with the number »' . $Detail->getNumber() . '« has been set to »' . money_format('%.2n', $currentPrice) . '«.');
 }
 /**
  * @param array $customerData
  * @return Shopware\Models\Customer\Customer
  */
 protected function saveCustomer($customerData)
 {
     $customerData = $this->toUtf8($customerData);
     $customerRepository = $this->getCustomerRepository();
     /** @var \Shopware\Components\Api\Resource\Customer $customerResource */
     $customerResource = \Shopware\Components\Api\Manager::getResource('customer');
     $customerModel = null;
     if (empty($customerData['email'])) {
         return false;
     }
     // userId and custumernumber will be ignored as there is not distinction between accountmode 0 and 1 in
     // old export files
     if (!empty($customerData['email']) && !empty($customerData['subshopID'])) {
         /** \Shopware\Models\Customer\Customer $customerModel */
         $customerModel = $customerRepository->findOneBy(array('email' => $customerData['email'], 'shopId' => $customerData['subshopID']));
     } elseif (!empty($customerData['email'])) {
         /** \Shopware\Models\Customer\Customer $customerModel */
         $customerModel = $customerRepository->findOneBy(array('email' => $customerData['email']));
     }
     // if no user was found by email, its save to find one via customernumber
     if (!$customerModel && !empty($customerData['customernumber'])) {
         /** \Shopware\Models\Customer\Billing $billingModel */
         $billingModel = Shopware()->Models()->getRepository('\\Shopware\\Models\\Customer\\Billing')->findOneBy(array('number' => $customerData['customernumber']));
         if ($billingModel) {
             /** \Shopware\Models\Customer\Customer $customerModel */
             $customerModel = $billingModel->getCustomer();
         }
     }
     if (!$customerModel) {
         /** \Shopware\Models\Customer\Customer $customerModel */
         $customerModel = new \Shopware\Models\Customer\Customer();
     }
     $customerData = $this->prepareCustomerData($customerData);
     if ($customerModel->getId() > 0) {
         $result = $customerResource->update($customerModel->getId(), $customerData);
     } else {
         $result = $customerResource->create($customerData);
     }
     return $result;
 }
Exemple #6
0
    /**
     * This function updates the buyer protection article in the database
     * @throws Exception
     * @return Array | protection items
     */
	public function updateTrustedShopsProtectionItems()
	{
		$tsDataModel = new TrustedShopsDataModel();
		$TsProducts = $tsDataModel->getProtectionItems();
		foreach($TsProducts->item as $product) {
            $articleData = array(
                'name' => 'Käuferschutz',
                'active' => true,
                'tax' => 19,
                'supplier' => 'Trusted Shops',
                'mainDetail' => array(
                    'number' => $product->tsProductID,
                    'attribute' => array(
                        'attr19' => $product->protectedAmountDecimal,
                        'attr20' => $product->protectionDurationInt
                    ),
                    'prices' => array(
                        array(
                            'customerGroupKey' => 'EK',
                            'price' => $product->grossFee,
                        ),
                    )
                )
            );
            $articleResource = \Shopware\Components\Api\Manager::getResource('article');
            try {
                $articleDetailRepostiory = Shopware()->Models()->getRepository('Shopware\Models\Article\Detail');
                $articleDetailModel = $articleDetailRepostiory->findOneBy(array('number' => $articleData['mainDetail']['number']));
                if ($articleDetailModel) {
                    $articleModel = $articleDetailModel->getArticle();
                }
                if ($articleModel) {
                    $articleResource->update($articleModel->getId(), $articleData);
                } else  {
                    $articleResource->create($articleData);
                }
            } catch (\Shopware\Components\Api\Exception\ValidationException $ve) {
                $errors = array();
                /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
                foreach ($ve->getViolations() as $violation) {
                    $errors[] = sprintf(
                        '%s: %s',
                        $violation->getPropertyPath(),
                        $violation->getMessage()
                    );
                }
                throw new \Exception(implode(', ', $errors));
            }
		}

		return $TsProducts->item;
	}
 public function init()
 {
     $this->resource = \Shopware\Components\Api\Manager::getResource('CustomerGroup');
 }
Exemple #8
0
    /**
     * @param array $customerData
     * @return Shopware\Models\Customer\Customer
     */
    protected function saveCustomer($customerData)
    {
        $customerData = $this->toUtf8($customerData);

        $customerRepository = $this->getCustomerRepository();

        /** @var \Shopware\Components\Api\Resource\Customer $customerResource */
        $customerResource = \Shopware\Components\Api\Manager::getResource('customer');

        if (empty($customerData['userID']) && empty($customerData['email'])) {
            return false;
        }

        if (!empty($customerData['userID'])) {
            /** \Shopware\Models\Customer\Customer $customerModel */
            $customerModel = $customerRepository->find($customerData['userID']);
            if (!$customerModel) {
                return false;
            }
        } elseif (!empty($customerData['email']) && empty($customerData['subshopID'])) {
            /** \Shopware\Models\Customer\Customer $customerModel */
            $customerModel = $customerRepository->findOneBy(array('email' => $customerData['email'], 'shopId' => $customerData['subshopID']));

        } elseif (!empty($customerData['email'])) {
            /** \Shopware\Models\Customer\Customer $customerModel */
            $customerModel = $customerRepository->findOneBy(array('email' => $customerData['email']));
        }

        if (!$customerModel) {
            /** \Shopware\Models\Customer\Customer $customerModel */
            $customerModel = new \Shopware\Models\Customer\Customer();
        }

        $customerData = $this->prepareCustomerData($customerData);

        if ($customerModel->getId() > 0) {
            $result = $customerResource->update($customerModel->getId(), $customerData);
        } else {
            $result = $customerResource->create($customerData);
        }

        return $result;
    }
 public function init()
 {
     $this->resource = \Shopware\Components\Api\Manager::getResource('customerWithoutExternalId');
 }
 public function init()
 {
     $this->resource = \Shopware\Components\Api\Manager::getResource('orderByExternalId');
 }
 /**
  * @param string $sOrderNumber
  * @return array|null
  */
 protected function getOrder($sOrderNumber)
 {
     /** @var \Shopware\Components\Api\Resource\Order $resource */
     $resource = \Shopware\Components\Api\Manager::getResource('order');
     try {
         return $resource->getOneByNumber($sOrderNumber);
     } catch (\Exception $exception) {
         return null;
     }
 }