When I import product from CSV file with new categories then magento2 create new categories.

But magento assign these categories to all stores, I want to restrict this and want that these categories will be enabled only on default store.

How can I achieve this? Should I add any event in custom module which disabled back these categories to sub store views?

1 Answer 1


I found a solution for this, I don't know it is a proper way or not but for now it is working for me. If anyone has best/standard solution then please post.

Here is my solution : (this is also working when a new category is added from admin panel)

In events.xml added two events :

<event name="catalog_category_save_before">
    <observer name="custome_categorysavebefore" instance="Company\Custom\Model\CategorySaveBefore" shared="false" />
<event name="catalog_category_save_after">
    <observer name="custome_categorysaveafter" instance="Company\Custom\Model\CategorySaveAfter" shared="false" />

In CategorySaveBefore.php :

$this->category = $observer->getEvent()->getCategory();
if (!$this->category->getId()) 
    // set in session to use in after event

In CategorySaveAfter.php :

// $is_new -> check from session
if($is_new == 1 && ($store_id == 0 || $store_id == 1))
    $stores =  $this->storeManager->getStores($withDefault = false);
    foreach($stores  as $eachStoreId => $val)
        if($eachStoreId == 0 || $eachStoreId == 1)
        $category_obj = $this->objectManager->get('Magento\Catalog\Model\Category')->load($this->category->getId());
        $category_obj->setData('is_active', 0);
// unset session varaible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.