/** * New method to create categories * @param array $category */ public function sCategory($category = array()) { // In order to be compatible with the old API syntax but to also be able to use ->fromArray(), // we map from the old keys to doctrine keys $mappings = array('description' => 'name', 'cmsheadline' => 'cmsHeadline', 'metakeywords' => 'metaKeywords', 'metadescription' => 'metaDescription'); foreach ($mappings as $original => $new) { if (isset($category[$original])) { $category[$new] = $category[$original]; unset($category[$original]); } } $model = null; $categoryRepository = $this->getCategoryRepository(); // If user wants to update the category if ($category['updateID']) { $model = $categoryRepository->find((int) $category['updateID']); if ($model === null) { $this->sAPI->sSetError("Category {$category['updateID']} not found", 10405); return false; } $model->fromArray($category); Shopware()->Models()->persist($model); Shopware()->Models()->flush(); } // Create a new category if no model was set, yet if (!$model) { // try to find a existing category by name and parent if (isset($category['parent']) && isset($category['name'])) { $model = $categoryRepository->findOneBy(array('parent' => $category['parent'], 'name' => $category['name'])); } if (!$model) { $model = new \Shopware\Models\Category\Category(); } if (isset($category['parent'])) { $parentModel = $categoryRepository->find((int) $category['parent']); if ($parentModel === null) { $this->sAPI->sSetError("Parent category {$category['parent']} not found", 10406); return false; } } $model->fromArray($category); $model->setParent($parentModel); Shopware()->Models()->persist($model); Shopware()->Models()->flush(); } // set category attributes $upset = array(); for ($i = 1; $i <= 6; $i++) { if (isset($category['ac_attr' . $i])) { $upset['attribute' . $i] = (string) $category['ac_attr' . $i]; } elseif (isset($category['attr'][$i])) { $upset['attribute' . $i] = (string) $category['attr'][$i]; } } if (!empty($upset)) { $attributeID = Shopware()->Db()->fetchOne("SELECT id FROM s_categories_attributes WHERE categoryID=?", array($model->getId())); if ($attributeID === false) { $upset['categoryID'] = $model->getId(); Shopware()->Db()->insert('s_categories_attributes', $upset); } else { Shopware()->Db()->update('s_categories_attributes', $upset, array('categoryID = ?' => $model->getId())); } } return $model->getId(); }
/** * Creates the dummy data * * @return \Shopware\Models\Category\Category */ private function getDummyData() { $dummyModel = new \Shopware\Models\Category\Category(); $dummyData = $this->dummyData; $dummyModel->fromArray($dummyData); //set category parent $parent = $this->repository->find($dummyData['parentId']); $dummyModel->setParent($parent); return $dummyModel; }
public function testCanInjectParameters() { $category = new \Shopware\Models\Category\Category(); $categoryId = 1; $translationId = 12; $customerGroupId = 23; $category->setPrimaryIdentifier($categoryId); $sArticles = new sArticles($category, $translationId, $customerGroupId); $this->assertsArticlesState($sArticles, $categoryId, $translationId, $customerGroupId); }
public function sCategoryPath($category) { $parts = $this->repository->getPathById($category, 'name'); $level = $this->baseCategory->getLevel() + 1; $parts = array_slice($parts, $level); return $parts; }
/** * @param string $categorypaths * @return array */ public function createCategoriesByCategoryPaths($categorypaths) { $categoryIds = array(); $categorypaths = explode("\n", $categorypaths); foreach ($categorypaths as $categorypath) { $categorypath = trim($categorypath); if (empty($categorypath)) { continue; } $categories = explode('|', $categorypath); $categoryId = 1; foreach ($categories as $categoryName) { $categoryName = trim($categoryName); if (empty($categoryName)) { break; } $categoryModel = $this->getCategoryRepository()->findOneBy(array('name' => $categoryName, 'parentId' => $categoryId)); if (!$categoryModel) { $parent = $this->getCategoryRepository()->find($categoryId); if (!$parent) { throw new \Exception(sprintf('Could not find %s ')); } $categoryModel = new \Shopware\Models\Category\Category(); $categoryModel->setParent($parent); $categoryModel->setName($categoryName); $this->getManager()->persist($categoryModel); $this->getManager()->flush(); $this->getManager()->clear(); } $categoryId = $categoryModel->getId(); if (empty($categoryId)) { continue; } if (!in_array($categoryId, $categoryIds)) { $categoryIds[] = $categoryId; } } } return $categoryIds; }
/** * Returns the root category id * * @param \Shopware\Models\Category\Category $category * @return integer */ public static function getRootIdByCategory(Shopware\Models\Category\Category $category) { while ($category->getParentId()) { $parent = $category->getParent(); if ($parent->getLevel() == 0) { break; } $category = $parent; } return $category->getId(); }
/** * Creates the category in plentymarkets * * @param \Shopware\Models\Category\Category $category * @param $storeId * @param $shopId * @param null $categoryId * @return int|null * @throws PlentymarketsExportException */ private function exportCategory(Shopware\Models\Category\Category $category, $storeId, $shopId, $categoryId = null) { $level = $category->getLevel() - 1; if ($level == 1) { $parentId = null; } else { $parentId = PlentymarketsMappingEntityCategory::getCategoryByShopwareID($category->getParentId(), $shopId); } $Request_SetCategories = new PlentySoapRequest_SetCategories(); $Request_SetCategories->SetCategories = array(); $RequestObject_SetCategories = new PlentySoapRequestObject_SetCategories(); $RequestObject_SetCategories->CategoryID = $categoryId; $RequestObject_CreateCategory = new PlentySoapRequestObject_CreateCategory(); $RequestObject_CreateCategory->Description = null; // string $RequestObject_CreateCategory->Description2 = null; // string $RequestObject_CreateCategory->FulltextActive = null; // string $RequestObject_CreateCategory->Image = null; // string $RequestObject_CreateCategory->Image1Path = null; // string $RequestObject_CreateCategory->Image2 = null; // string $RequestObject_CreateCategory->Image2Path = null; // string $RequestObject_CreateCategory->ItemListView = null; // string $RequestObject_CreateCategory->Lang = 'de'; // string $RequestObject_CreateCategory->Level = $level; // int $RequestObject_CreateCategory->MetaDescription = $category->getMetaDescription(); // string $RequestObject_CreateCategory->MetaKeywords = $category->getMetaKeywords(); // string $RequestObject_CreateCategory->MetaTitle = $category->getName(); // string $RequestObject_CreateCategory->Name = $category->getName() ?: 'Category ' . $category->getId(); // string $RequestObject_CreateCategory->NameURL = null; // string $RequestObject_CreateCategory->PageView = null; // string $RequestObject_CreateCategory->PlaceholderTranslation = null; // string $RequestObject_CreateCategory->Position = $category->getPosition(); // int $RequestObject_CreateCategory->PreviewPath = null; // string $RequestObject_CreateCategory->RootPath = null; // string $RequestObject_CreateCategory->ShortDescription = null; // string $RequestObject_CreateCategory->SingleItemView = null; // string $RequestObject_CreateCategory->WebTemplateExist = null; // string $RequestObject_CreateCategory->WebstoreID = $storeId; // int $RequestObject_CreateCategory->ParentCategoryID = $parentId; //int $RequestObject_SetCategories->CreateCategory = $RequestObject_CreateCategory; $Request_SetCategories->SetCategories[] = $RequestObject_SetCategories; $Response_SetCategories = PlentymarketsSoapClient::getInstance()->SetCategories($Request_SetCategories); if (!$Response_SetCategories->Success) { throw new PlentymarketsExportException('The category could not be saved! ', 2920); } else { $categoryId = (int) $Response_SetCategories->Categories->item[0]->CategoryID; $shopId = PlentymarketsMappingController::getShopByPlentyID($storeId); PlentymarketsMappingEntityCategory::addCategory($category->getId(), $shopId, $categoryId, $storeId); return $categoryId; } }