cancel
Showing results for 
Search instead for 
Did you mean: 

Multilingual store categories using csv file in magento

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Multilingual store categories using csv file in magento

I am new in Magento and I need for help I want to translate my categories using CSV file
I import categories from CSV file and I want my store to be translated from English to Arabic

This is my CSV code :

 

id;store_id;name;parent_id
42;1;attajer mall;
42;2;التاجر مول;

And this is my PHP code

 /**
 * Load category by old category ID
 *
 * @param $categoryFactory
 * @param $data
 * @param $id
 * @return mixed
 */
protected function getCategoryFromCollectionByOldId($categoryFactory, $data, $id)
{
    return $categoryCollection = $categoryFactory
        ->create()
        ->getCollection()
        ->addFieldToFilter('entity_id', $data[$this->headersMap[$id]])
        ->setPageSize(1);//->addAttributeToFilter('category_id', ])
}

/**
 * Add or update category data
 *
 * @param $data
 * @param bool|false $isParent
 * @return bool
 */
protected function addOrUpdateCategory($data, $isParent = false)
{
    $categoryFactory = $this->objectManager->get('Magento\Catalog\Model\CategoryFactory');
    $categoryRepository = $this->objectManager->get('\Magento\Catalog\Api\CategoryRepositoryInterface');
    $categoryCollection = $this->getCategoryFromCollectionByOldId($categoryFactory, $data, 'id');
    if ($categoryCollection->getSize()) {
        $categoryId = $categoryCollection->getFirstItem()->getEntityId();
        $category = $categoryRepository->get($categoryId, 0);
    } else {
        $category = $categoryFactory->create();
    }
    $storeId= $data[1];
    $category->setStoreId($data[1]);
    $category->setName($data[$this->headersMap['name']]);

    $category->setIsActive($this->getOptionalAttributeValue($data, 'is_active', true));
    $category->setIncludeInMenu($this->getOptionalAttributeValue($data, 'include_in_menu', true));
    if ($isParent) {
        $category->setParentId($data[]);
    } else {
        $categoryCollection = $this->getCategoryFromCollectionByOldId($categoryFactory, $data, 'parent_id');
        if ($categoryCollection->getSize()) {
            $categoryId = $categoryCollection->getFirstItem()->getEntityId();
            $category->setParentId($categoryId);
        } else {
            $this->errors[] = 'ERROR (RECORD SKIPPED): Category "'
                . $data[$this->headersMap['name']]
                . '" does not have existing parent category!';
            return false;
        }
    }


    $additionalData = [
        'is_anchor' => $this->getOptionalAttributeValue($data, 'is_anchor', true),
        'custom_use_parent_settings' =>
            $this->getOptionalAttributeValue($data, 'custom_use_parent_settings', true)

    ];
    foreach ($this->additionalHeaders as $header) {
        if (array_key_exists($header, $this->headersMap)) {
            $additionalData[$header] = $data[$this->headersMap[$header]];
        }
    }


    $category->setCustomAttributes($additionalData);
   // $categoryRepository->save($category);

    try{
        var_dump($category->getData());
        $categoryRepository->save($category);
    } catch(\Exception $e){
        var_dump($e->getTraceAsString());die;
    }

    return true;
}

The problem is that the categories data of the two languages are not displayed in the backend (admin part)