Skip to main content

Category

Examples for import category.

First, we will pull the data from the store manager.

Get category data
 // Get token for authentication
$token = $this->tokenService->getApiToken(); // Method to get token from Storemanager
$urlGetCategories = $configParameters['baseUrl'] . 'api/v1/categories?TokenID=' . $token . '&navigationstypNr=' . $navigationType . $lastChange;

$this->adegaRrpConnectorImportLogger->info('Url for getting categories tree ' . $urlGetCategories);

$httpClient = HttpClient::create();
$response = $httpClient->request('GET', $urlGetCategories);
  • $content = Data that are converted from the JSON response. This content is sorted by 'ParentCatNr'.
  • $configParameters = ERP connector settings that are defined in the administration Settings.
  • $rootCategory = It is a created root category into which all other categories are imported Default Category - ERP.
Import single category
 // Import single category
foreach ($content as $category) {
$this->importCategory($category, $rootCategory, $configParameters);
}

Importing category into Shopware

Import category
    /**
* Import category
* @param $category
* @param $rootCategory
* @param $configParameters
* @return void
*/
public function importCategory($category, $rootCategory, $configParameters): void
{
// Get category model
$categoryModel = $this->getCategoryModel('customFields.custom_category_CatNr', $category['CatNr']);

//Get parent category
$parentCategoryModel = $this->getCategoryModel(
'customFields.custom_category_CatNr',
$category['ParentCatNr']
) ?? $rootCategory;

//Check if category can be imported by status
if ($category['Status'] == 0) {
if ($categoryModel) {
$this->categoryRepository->delete([['id' => $categoryModel->getId()]], $this->context);
}
} else {
$languages = isset($category['language']) ? $category['language'] : null;
// Uploading preview images
$mediaId = $this->saveCategoryImage($category, $categoryModel, $configParameters['baseUrl']);
// Description according to languages
$description = $this->getDescription($languages);
// Get previous category
$prevCategory = $this->getPrevCategory($category);

$subtitle = null;
if(!empty($languages) && isset($languages[0]["SubTitle"])) {
$subtitle = $languages[0]["SubTitle"];
}
//Update or create category
$this->categoryRepository->upsert(
[
[
'id' => $categoryModel ? $categoryModel->getId() : Uuid::randomHex(),
'customFields' => [
'custom_category_SubTitle' => $subtitle,
'custom_category_ParentCatNr' => $category['ParentCatNr'],
'custom_category_CatNr' => $category['CatNr'],
'custom_category_Style' => $category['Style'],
'custom_category_CatNumber' => $category['CatNumber'],
'custom_category_Status' => $category['Status'],
'custom_category_ShowSubCatItems' => $category['ShowSubCatItems'],
'custom_category_ShowItemGroupItems' => $category['ShowItemGroupItems'],
'custom_category_ShowPromoItems' => $category['ShowPromoItems'],
'custom_category_ShowSuggestions' => $category['ShowSuggestions'],
'custom_category_SortOrder' => $category['SortOrder'],
],
'parentId' => $parentCategoryModel->id,
'mediaId' => $mediaId,
'description' => $description,
'metaTitle' => $this->getMetaTitle($languages),
'active' => true,
'displayNestedProducts' => true,
'visible' => true,
'afterCategoryId' => $prevCategory,
'type' => 'page',
'name' => $this->getCategoryName($languages),
'children' => [
],
],
],
$this->context
);
$this->importedCategory++;
}
}