/** * Exports the images of the item */ protected function exportImages() { /** * @var Shopware\Models\Article\Image $Image * @var Shopware\Models\Media\Media $ImageMedia */ foreach ($this->SHOPWARE_Article->getImages() as $Image) { $ImageMedia = $Image->getMedia(); if (is_null($ImageMedia)) { PlentymarketsLogger::getInstance()->error('Export:Initial:Item:Image', 'The image with the id »' . $Image->getId() . '« could not be added to the item with the number »' . $this->SHOPWARE_Article->getMainDetail()->getNumber() . '« (no media associated)', 2850); continue; } try { if ('___VERSION___' !== Shopware::VERSION && version_compare(Shopware::VERSION, '5.1.0', '<')) { $fullpath = Shopware()->DocPath() . $ImageMedia->getPath(); } else { /** * @var \Shopware\Bundle\MediaBundle\MediaService $mediaService */ $mediaService = Shopware()->Container()->get('shopware_media.media_service'); $fullpath = $mediaService->getUrl($ImageMedia->getPath()); } } catch (Exception $E) { PlentymarketsLogger::getInstance()->error('Export:Initial:Item:Image', 'The image with the id »' . $Image->getId() . '« could not be added to the item with the number »' . $this->SHOPWARE_Article->getMainDetail()->getNumber() . '« (' . $E->getMessage() . ')', 2860); continue; } $Request_SetItemImages = new PlentySoapRequest_SetItemImages(); $Request_SetItemImages->Images = array(); $RequestObject_SetItemImagesImage = new PlentySoapRequestObject_SetItemImagesImage(); $RequestObject_SetItemImagesImage->ImageFileName = $Image->getPath(); $RequestObject_SetItemImagesImage->ImageFileData = base64_encode(file_get_contents($fullpath)); $RequestObject_SetItemImagesImage->ImageFileEnding = $Image->getExtension(); $RequestObject_SetItemImagesImage->ImageID = null; // int $RequestObject_SetItemImagesImage->ImageURL = null; // string $RequestObject_SetItemImagesImage->Position = $Image->getPosition(); $mappings = $Image->getMappings(); if (count($mappings)) { /** @var Shopware\Models\Article\Image\Mapping $mapping */ $mapping = $mappings->first(); $rules = $mapping->getRules(); if (count($rules)) { /** @var Shopware\Models\Article\Image\Rule $rule */ $rule = $rules->first(); $option = $rule->getOption(); $group = $option->getGroup(); // $Request_SetItemImages->ItemAttributeID = PlentymarketsMappingController::getAttributeGroupByShopwareID($group->getId()); $RequestObject_SetItemImagesImage->AttributeValueId = PlentymarketsMappingController::getAttributeOptionByShopwareID($option->getId()); } } $RequestObject_SetItemImagesImage->Names = array(); $RequestObject_SetItemImagesImageName = new PlentySoapRequestObject_SetItemImagesImageName(); 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(); //check if the attribute value is set for the image if (method_exists($Image->getAttribute(), 'getAttribute' . $plenty_attributeID)) { // set the value of the attribute as alternative text for the image $RequestObject_SetItemImagesImageName->AlternativeText = $Image->getAttribute()->{getAttribute . $plenty_attributeID}(); // string } } $RequestObject_SetItemImagesImageName->DeleteName = false; // boolean $RequestObject_SetItemImagesImageName->Lang = 'de'; // string $RequestObject_SetItemImagesImageName->Name = $Image->getDescription(); // string $RequestObject_SetItemImagesImage->Names[] = $RequestObject_SetItemImagesImageName; // export the image title translations $image_NameTranslations = $this->getImageTitleTranslations($Image->getId()); foreach ($image_NameTranslations as $lang => $titleTranslation) { $RequestObject_SetItemImagesImageName = new PlentySoapRequestObject_SetItemImagesImageName(); $RequestObject_SetItemImagesImageName->DeleteName = false; // boolean $RequestObject_SetItemImagesImageName->Lang = $lang; // string $RequestObject_SetItemImagesImageName->Name = $titleTranslation; // string $RequestObject_SetItemImagesImage->Names[] = $RequestObject_SetItemImagesImageName; } // set the plenty store ids for references $RequestObject_SetItemImagesImage->References = array(); $plentyStoreIds = array(); /** @var Shopware\Models\Category\Category $Category */ foreach ($this->SHOPWARE_Article->getCategories() as $Category) { // Get the store for this category $rootId = PlentymarketsUtils::getRootIdByCategory($Category); $shops = PlentymarketsUtils::getShopIdByCategoryRootId($rootId); foreach ($shops as $shopId) { try { $plentyStoreId = PlentymarketsMappingController::getShopByShopwareID($shopId); if (!in_array($plentyStoreId, $plentyStoreIds)) { $plentyStoreIds[] = $plentyStoreId; } } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } } } foreach ($plentyStoreIds as $storeId) { $RequestObject_SetItemImagesImageReference = new PlentySoapRequestObject_SetItemImagesImageReference(); $RequestObject_SetItemImagesImageReference->DeleteReference = false; // boolean //$Enumeration_SetItemImagesImageReferenceType = new PlentySoapEnumeration_SetItemImagesImageReferenceType(); $RequestObject_SetItemImagesImageReference->ReferenceType = 'Mandant'; $RequestObject_SetItemImagesImageReference->ReferenceValue = $storeId; // int $RequestObject_SetItemImagesImage->References[] = $RequestObject_SetItemImagesImageReference; } $Request_SetItemImages->Images[] = $RequestObject_SetItemImagesImage; $Request_SetItemImages->ItemID = $this->PLENTY_itemID; // Do the request PlentymarketsSoapClient::getInstance()->SetItemImages($Request_SetItemImages); } }
/** * Exports the item bundle head item * * @throws PlentymarketsExportException */ protected function exportHead() { // The shopware item on which the bundle is based on $shopwareBundleHead = $this->SHOPWARE_bundle->getArticle(); $shopwareBundleHeadIsVariant = !is_null($shopwareBundleHead->getConfiguratorSet()); // If the bundle head is a variant, the bundle can't be exported // since that feature is not provided by plentymarkets if ($shopwareBundleHeadIsVariant) { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the master item is a variant.', 2230); } if ($this->SHOPWARE_bundle->getDiscountType() != 'abs') { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the discount type is not supported.', 2240); } if ($this->SHOPWARE_bundle->getType() != 1) { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the bundle type is not supported.', 2250); } // The shopware bundle head needs to be added as a plenty-bundle-item // The bundle head in plentymarkets is a "special" item $bundleItemId = PlentymarketsMappingController::getItemByShopwareID($shopwareBundleHead->getId()); $sku = sprintf('%d-0', $bundleItemId); // If this item is also a bundle item in shopware, // we need to increase the quantity of it if (isset($this->PLENTY_bundleSkuList[$sku])) { $this->PLENTY_bundleSkuList[$sku] += 1; } else { $this->PLENTY_bundleSkuList[$sku] = 1; } // Create the bundle head $Request_AddItemsBase = new PlentySoapRequest_AddItemsBase(); $Request_AddItemsBase->BaseItems = array(); $Object_AddItemsBaseItemBase = new PlentySoapObject_AddItemsBaseItemBase(); $Object_ItemAvailability = new PlentySoapObject_ItemAvailability(); $validTo = $this->SHOPWARE_bundle->getValidTo(); if ($validTo instanceof DateTime) { $Object_ItemAvailability->AvailableUntil = $this->SHOPWARE_bundle->getValidTo()->getTimestamp(); } $Object_ItemAvailability->WebAPI = 1; $Object_ItemAvailability->Inactive = (int) $this->SHOPWARE_bundle->getActive(); $Object_ItemAvailability->Webshop = (int) $this->SHOPWARE_bundle->getActive(); $Object_AddItemsBaseItemBase->Availability = $Object_ItemAvailability; $storeIds = array(); $Object_AddItemsBaseItemBase->Categories = array(); $Object_AddItemsBaseItemBase->StoreIDs = array(); foreach ($shopwareBundleHead->getCategories() as $category) { /** @var Shopware\Models\Category\Category $category */ try { $categoryPath = PlentymarketsMappingController::getCategoryByShopwareID($category->getId()); } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } $Object_ItemCategory = new PlentySoapObject_ItemCategory(); $Object_ItemCategory->ItemCategoryPath = $categoryPath; // string $Object_AddItemsBaseItemBase->Categories[] = $Object_ItemCategory; // Get the store for this category $rootId = PlentymarketsUtils::getRootIdByCategory($category); $shops = PlentymarketsUtils::getShopIdByCategoryRootId($rootId); foreach ($shops as $shopId) { try { $storeId = PlentymarketsMappingController::getShopByShopwareID($shopId); } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } if (!isset($storeIds[$storeId])) { // Activate the item for this store $Object_Integer = new PlentySoapObject_Integer(); $Object_Integer->intValue = $storeId; $Object_AddItemsBaseItemBase->StoreIDs[] = $Object_Integer; // Cache $storeIds[$storeId] = true; } } } $Object_AddItemsBaseItemBase->ExternalItemID = 'Swag/Bundle/' . $this->SHOPWARE_bundle->getId(); // string $Object_AddItemsBaseItemBase->ItemNo = $this->SHOPWARE_bundle->getNumber(); // string $Object_ItemPriceSet = new PlentySoapObject_ItemPriceSet(); $defaultCustomerGroupKey = PlentymarketsConfig::getInstance()->get('DefaultCustomerGroupKey'); $price = null; $isPriceFound = false; foreach ($this->SHOPWARE_bundle->getPrices() as $price) { /** @var Shopware\CustomModels\Bundle\Price $price */ if ($price->getCustomerGroup()->getKey() == $defaultCustomerGroupKey) { $isPriceFound = true; break; } } if ($isPriceFound && $price instanceof Shopware\CustomModels\Bundle\Price) { $tax = $this->SHOPWARE_bundle->getArticle()->getTax()->getTax(); $priceNet = $price->getPrice(); $price = $priceNet + $priceNet / 100 * $tax; $Object_ItemPriceSet->Price = $price; $Object_ItemPriceSet->VAT = $tax; } else { // If there is no price, we have to set one anyway. // Otherwise the re-import will crash $Object_ItemPriceSet->Price = 1; } $Object_AddItemsBaseItemBase->PriceSet = $Object_ItemPriceSet; $Object_AddItemsBaseItemBase->VATInternalID = PlentymarketsMappingController::getVatByShopwareID($this->SHOPWARE_bundle->getArticle()->getTax()->getId()); $Object_AddItemsBaseItemBase->ProducerID = PlentymarketsMappingController::getProducerByShopwareID($shopwareBundleHead->getSupplier()->getId()); // int $Object_AddItemsBaseItemBase->Published = null; // int $Object_ItemTexts = new PlentySoapObject_ItemTexts(); $Object_ItemTexts->Name = $this->SHOPWARE_bundle->getName(); // string $Object_AddItemsBaseItemBase->Texts = $Object_ItemTexts; $Request_AddItemsBase->BaseItems[] = $Object_AddItemsBaseItemBase; $Response_AddItemsBase = PlentymarketsSoapClient::getInstance()->AddItemsBase($Request_AddItemsBase); $ResponseMessage = $Response_AddItemsBase->ResponseMessages->item[0]; if (!$Response_AddItemsBase->Success || $ResponseMessage->Code != 100) { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« could not be exported', 2210); } $PLENTY_priceID = null; foreach ($ResponseMessage->SuccessMessages->item as $SubMessage) { if ($SubMessage->Key == 'ItemID') { $this->PLENTY_bundleHeadId = (int) $SubMessage->Value; } else { if ($SubMessage->Key == 'PriceID') { $PLENTY_priceID = (int) $SubMessage->Value; } } } if ($this->PLENTY_bundleHeadId && $PLENTY_priceID) { PlentymarketsLogger::getInstance()->message('Export:Initial:Item:Bundle', 'The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« has been created with the id »' . $this->PLENTY_bundleHeadId . '«.'); PlentymarketsMappingController::addItemBundle($this->SHOPWARE_bundle->getId(), $this->PLENTY_bundleHeadId); } else { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« could not be exported', 2210); } }
/** * Export the missing categories from shopware to plenty */ protected function main() { $shopwareCategories = Shopware()->Models()->getRepository('Shopware\\Models\\Category\\Category')->findBy(array('path' => null)); /** @var Shopware\Models\Category\Category $shopwareCategory */ foreach ($shopwareCategories as $shopwareCategory) { // Skip "Root" if ((int) $shopwareCategory->getParentId() == 0) { continue; } if ($shopwareCategory->getBlog()) { continue; } // Get the store for this category $rootId = PlentymarketsUtils::getRootIdByCategory($shopwareCategory); $shops = PlentymarketsUtils::getShopIdByCategoryRootId($rootId); if (!$shops) { $shops = array(0); } foreach ($shops as $shopId) { try { $storeId = PlentymarketsMappingController::getShopByShopwareID($shopId); } catch (PlentymarketsMappingExceptionNotExistant $E) { $storeId = 0; } $this->export($shopwareCategory->getChildren(), $this->PLENTY_CategoryTree2ShopID, $storeId); } } }