/** * 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); } }
/** * Constructor method * * @param PlentySoapObject_ItemBase $ItemBase * @param Shopware\Models\Shop\Shop $Shop */ public function __construct($ItemBase, Shopware\Models\Shop\Shop $Shop) { $this->ItemBase = $ItemBase; $this->Shop = $Shop; $this->storeId = PlentymarketsMappingController::getShopByShopwareID($Shop->getId()); }
/** * 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); } }
/** * Returns the shop id or null * * @return integer|null */ protected function getShopId() { // Sub-objects $Shop = $this->Order->getShop(); // Shop if ($Shop) { try { return PlentymarketsMappingController::getShopByShopwareID($Shop->getId()); } catch (PlentymarketsMappingExceptionNotExistant $E) { } } return null; }
/** * Exports the customer */ protected function exportCustomer() { if (is_null($this->BillingAddress)) { throw new PlentymarketsExportEntityException('The customer with the email address »' . $this->Customer->getEmail() . '« could not be exported (no billing address)', 2100); } try { if ($this->BillingAddress instanceof \Shopware\Models\Customer\Billing) { $this->PLENTY_customerID = PlentymarketsMappingController::getCustomerBillingAddressByShopwareID($this->BillingAddress->getId()); } else { if ($this->BillingAddress instanceof \Shopware\Models\Order\Billing) { $this->PLENTY_customerID = PlentymarketsMappingController::getCustomerByShopwareID($this->BillingAddress->getId()); } } // Already exported return; } catch (PlentymarketsMappingExceptionNotExistant $E) { } // Logging PlentymarketsLogger::getInstance()->message('Export:Customer', 'Export of the customer with the number »' . $this->getCustomerNumber() . '«'); $city = trim($this->BillingAddress->getCity()); $street_arr = PlentymarketsUtils::extractStreetAndHouseNo($this->BillingAddress->getStreet()); if (isset($street_arr['street']) && strlen($street_arr['street']) > 0) { $streetName = $street_arr['street']; } else { $streetName = trim($this->BillingAddress->getStreet()); } if (isset($street_arr['houseNo']) && strlen($street_arr['houseNo']) > 0) { $streetHouseNumber = $street_arr['houseNo']; } else { //no house number was found in the street string $streetHouseNumber = ''; } $zip = trim($this->BillingAddress->getZipCode()); if (empty($city)) { $city = PlentymarketsConfig::getInstance()->get('CustomerDefaultCity'); } if (!isset($streetHouseNumber) || $streetHouseNumber == '') { $streetHouseNumber = PlentymarketsConfig::getInstance()->get('CustomerDefaultHouseNumber'); } if (!isset($streetName) || $streetName == '') { $streetName = PlentymarketsConfig::getInstance()->get('CustomerDefaultStreet'); } if ($zip == '') { $zip = PlentymarketsConfig::getInstance()->get('CustomerDefaultZipcode'); } $Request_SetCustomers = new PlentySoapRequest_SetCustomers(); $Request_SetCustomers->Customers = array(); $Object_SetCustomersCustomer = new PlentySoapObject_Customer(); $Object_SetCustomersCustomer->City = $city; $Object_SetCustomersCustomer->Company = $this->BillingAddress->getCompany(); $Object_SetCustomersCustomer->CountryID = $this->getBillingCountryID(); // int $Object_SetCustomersCustomer->CustomerClass = $this->getCustomerClassId(); $Object_SetCustomersCustomer->CustomerNumber = $this->getCustomerNumber(); // string $Object_SetCustomersCustomer->CustomerSince = $this->Customer->getFirstLogin()->getTimestamp(); // int $Object_SetCustomersCustomer->Email = $this->Customer->getEmail(); // string $Object_SetCustomersCustomer->ExternalCustomerID = PlentymarketsUtils::getExternalCustomerID($this->Customer->getId()); // string $Object_SetCustomersCustomer->FormOfAddress = $this->getBillingFormOfAddress(); // string $Object_SetCustomersCustomer->Fax = $this->BillingAddress->getFax(); $Object_SetCustomersCustomer->FirstName = $this->BillingAddress->getFirstName(); $Object_SetCustomersCustomer->HouseNo = $streetHouseNumber; $Object_SetCustomersCustomer->IsBlocked = !$this->Customer->getActive(); $Object_SetCustomersCustomer->Newsletter = (int) $this->Customer->getNewsletter(); $Object_SetCustomersCustomer->PayInvoice = true; // boolean $Object_SetCustomersCustomer->Street = $streetName; $Object_SetCustomersCustomer->Surname = $this->BillingAddress->getLastName(); $Object_SetCustomersCustomer->Telephone = $this->BillingAddress->getPhone(); $Object_SetCustomersCustomer->VAT_ID = $this->BillingAddress->getVatId(); $Object_SetCustomersCustomer->ZIP = $zip; // Store id try { $Object_SetCustomersCustomer->StoreID = PlentymarketsMappingController::getShopByShopwareID($this->Customer->getShop()->getId()); $Object_SetCustomersCustomer->Language = strtolower(substr($this->Customer->getShop()->getLocale()->getLocale(), 0, 2)); } catch (PlentymarketsMappingExceptionNotExistant $E) { } // Customer class if ($this->Customer->getGroup()->getId() > 0) { try { $Object_SetCustomersCustomer->CustomerClass = PlentymarketsMappingController::getCustomerClassByShopwareID($this->Customer->getGroup()->getId()); } catch (PlentymarketsMappingExceptionNotExistant $E) { } } $Request_SetCustomers->Customers[] = $Object_SetCustomersCustomer; $Response_SetCustomers = PlentymarketsSoapClient::getInstance()->SetCustomers($Request_SetCustomers); if (!$Response_SetCustomers->Success) { throw new PlentymarketsExportEntityException('The customer with the number »' . $this->getCustomerNumber() . '« could not be exported', 2110); } if ($Response_SetCustomers->ResponseMessages->item[0]->Code == 100 || $Response_SetCustomers->ResponseMessages->item[0]->Code == 200) { $this->PLENTY_customerID = (int) $Response_SetCustomers->ResponseMessages->item[0]->SuccessMessages->item[0]->Value; if ($this->BillingAddress instanceof \Shopware\Models\Customer\Billing) { PlentymarketsMappingController::addCustomerBillingAddress($this->BillingAddress->getId(), $this->PLENTY_customerID); } else { if ($this->BillingAddress instanceof \Shopware\Models\Order\Billing) { PlentymarketsMappingController::addCustomer($this->BillingAddress->getId(), $this->PLENTY_customerID); } } } }
/** * Updates the stack */ public function update() { PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', 'Starting update'); $ShopRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Shop\\Shop'); $Shops = $ShopRepository->findBy(array('active' => 1), array('default' => 'DESC')); // Remember the time $timestamp = time(); // Is this the first run? $firstBlood = (int) PlentymarketsConfig::getInstance()->getImportItemStackFirstRunTimestamp() == 0; $Request_GetItemsUpdated = new PlentySoapRequest_GetItemsUpdated(); $Request_GetItemsUpdated->LastUpdateFrom = (int) PlentymarketsConfig::getInstance()->getImportItemStackLastUpdateTimestamp(); // Cache to avoid duplicate inserts of the same id with multiple shops $itemIdsStacked = array(); /** @var Shopware\Models\Shop\Shop $Shop */ foreach ($Shops as $Shop) { $Request_GetItemsUpdated->Page = 0; $Request_GetItemsUpdated->StoreID = PlentymarketsMappingController::getShopByShopwareID($Shop->getId()); do { // Do the request $Response_GetItemsUpdated = PlentymarketsSoapClient::getInstance()->GetItemsUpdated($Request_GetItemsUpdated); foreach ((array) $Response_GetItemsUpdated->Items->item as $Object_Integer) { $itemId = $Object_Integer->intValue; // Skip existing items on the first run if ($Request_GetItemsUpdated->LastUpdateFrom == 0 && $firstBlood) { try { PlentymarketsMappingController::getItemByPlentyID($itemId); continue; } catch (PlentymarketsMappingExceptionNotExistant $E) { } } $this->addItem($itemId, $Request_GetItemsUpdated->StoreID); $itemIdsStacked[$itemId] = true; } } while (++$Request_GetItemsUpdated->Page < $Response_GetItemsUpdated->Pages); } // Upcomming last update timestamp PlentymarketsConfig::getInstance()->setImportItemStackLastUpdateTimestamp($timestamp); if ($firstBlood) { // Remember your very first time :) PlentymarketsConfig::getInstance()->setImportItemStackFirstRunTimestamp(time()); } // Log $numberOfItemsStacked = count($itemIdsStacked); if (!$numberOfItemsStacked) { PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', 'No item has been added to the stack'); } else { if ($numberOfItemsStacked == 1) { PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', '1 item has been added to the stack'); } else { PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', count($itemIdsStacked) . ' items have been added to the stack'); } } PlentymarketsLogger::getInstance()->message('Sync:Stack:Item', 'Update finished'); }
/** * 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); } } }