/** * Exports the categories * * @param $shopwareCategories * @param $plentyTree * @param $storeId */ private function export($shopwareCategories, $plentyTree, $storeId) { try { $shopId = PlentymarketsMappingController::getShopByPlentyID($storeId); } catch (PlentymarketsMappingExceptionNotExistant $e) { return; } /** @var Shopware\Models\Category\Category $shopwareCategory */ foreach ($shopwareCategories as $shopwareCategory) { if ($shopwareCategory->getBlog()) { continue; } try { PlentymarketsMappingEntityCategory::getCategoryByShopwareID($shopwareCategory->getId(), $shopId); continue; } catch (PlentymarketsMappingExceptionNotExistant $e) { } $branchId = null; if (!isset($plentyTree['children'])) { $plentyTree['children'] = array(); } $shopwareName = trim($shopwareCategory->getName()); /** @var array $plentyChild1 */ foreach ($plentyTree['children'] as $name => $plentyChild1) { if ($name == $shopwareName) { $branchId = $plentyChild1['id']; PlentymarketsMappingEntityCategory::addCategory($shopwareCategory->getId(), $shopId, $branchId, $storeId); break; } } $branchId = $this->exportCategory($shopwareCategory, $storeId, $shopId, $branchId); // Active the category if ($storeId > 0 && $shopwareCategory->getActive()) { $Request_SetStoreCategories = new PlentySoapRequest_SetStoreCategories(); $Request_SetStoreCategories->StoreCategories = array(); $Object_SetStoreCategory = new PlentySoapObject_SetStoreCategory(); $Object_SetStoreCategory->BranchID = $branchId; $Object_SetStoreCategory->StoreID = $storeId; $Request_SetStoreCategories->StoreCategories[] = $Object_SetStoreCategory; PlentymarketsSoapClient::getInstance()->SetStoreCategories($Request_SetStoreCategories); } $shopwareChildren1 = $shopwareCategory->getChildren(); // search for the next categories of shopware in plentymarkets $this->export($shopwareChildren1, $plentyChild1, $storeId); } }
/** * Does the actual import */ public function import() { $parentId = Shopware()->Models()->find('Shopware\\Models\\Shop\\Shop', $this->shopId)->getCategory()->getId(); // Trigger to indicate an error while creating new category $addError = false; foreach ($this->plentyCategoryBranch as $plentyCategory) { $plentyCategoryId = $plentyCategory['branchId']; $plentyCategoryName = $plentyCategory['name']; if ($this->categoryNodeLang != $this->shopLang) { $plentyLocalCategoryName = $this->getLocalCategoryName($plentyCategoryId); if ($plentyLocalCategoryName) { $plentyCategoryName = $plentyLocalCategoryName; } } // Root category id (out of the shop) $CategoryFound = self::$CategoryRepository->findOneBy(array('name' => $plentyCategoryName, 'parentId' => $parentId)); if ($CategoryFound instanceof Shopware\Models\Category\Category) { // PlentymarketsMappingEntityCategory::addCategory($CategoryFound->getId(), $this->shopId, $plentyCategoryId, $this->storeId); $parentId = $CategoryFound->getId(); } else { $params = array(); $params['name'] = $plentyCategoryName; $params['parentId'] = $parentId; try { // Create $CategoryModel = self::$CategoryApi->create($params); // Add mapping and save into the object PlentymarketsMappingEntityCategory::addCategory($CategoryModel->getId(), $this->shopId, $plentyCategoryId, $this->storeId); // Parent $parentCategoryName = $CategoryModel->getParent()->getName(); // Log PlentymarketsLogger::getInstance()->message('Sync:Item:Category', 'The category »' . $plentyCategoryName . '« has been created beneath the category »' . $parentCategoryName . '«'); // Id to connect with the item $parentId = $CategoryModel->getId(); } catch (Exception $E) { // Log PlentymarketsLogger::getInstance()->error('Sync:Item:Category', 'The category »' . $plentyCategoryName . '« with the parentId »' . $parentId . '« could not be created (' . $E->getMessage() . ')', 3300); // Set the trigger - the category will not be connected with the item $addError = true; } } } if ($addError) { return false; } else { return $parentId; } }