Unassigned category from product in magento 2












4















I have tried following code but does't work



$all_categoty=$_product->getCategoryIds();
$CategoryLinkRepository = $objectManager->get('MagentoCatalogModelCategoryLinkRepository');
foreach($all_categoty as $cat){
if($cat=="15"){
continue;
}
$CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
}


Then try with reindexing but doesn't remove the category



$productCategoryIndexer = $objectManager ->get('MagentoFrameworkIndexerIndexerRegistry')
->get(MagentoCatalogModelIndexerProductCategory::INDEXER_ID);
$productCategoryIndexer->reindexRow($_product->getId());









share|improve this question

























  • Code seems code ok.only need to add try and catch try{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }

    – Amit Bera
    Sep 13 '18 at 4:12











  • check getCategoryIds value.i guess that have issue only with it

    – Amit Bera
    Sep 13 '18 at 4:13











  • getCategoryIds its working fine

    – Rahul Vaghela
    Sep 13 '18 at 5:35











  • After run of $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku()); .Please do indexing from admin and see what happen?

    – Amit Bera
    Sep 13 '18 at 7:28











  • in product edit page in admin show category as it is.

    – Rahul Vaghela
    Sep 13 '18 at 8:25
















4















I have tried following code but does't work



$all_categoty=$_product->getCategoryIds();
$CategoryLinkRepository = $objectManager->get('MagentoCatalogModelCategoryLinkRepository');
foreach($all_categoty as $cat){
if($cat=="15"){
continue;
}
$CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
}


Then try with reindexing but doesn't remove the category



$productCategoryIndexer = $objectManager ->get('MagentoFrameworkIndexerIndexerRegistry')
->get(MagentoCatalogModelIndexerProductCategory::INDEXER_ID);
$productCategoryIndexer->reindexRow($_product->getId());









share|improve this question

























  • Code seems code ok.only need to add try and catch try{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }

    – Amit Bera
    Sep 13 '18 at 4:12











  • check getCategoryIds value.i guess that have issue only with it

    – Amit Bera
    Sep 13 '18 at 4:13











  • getCategoryIds its working fine

    – Rahul Vaghela
    Sep 13 '18 at 5:35











  • After run of $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku()); .Please do indexing from admin and see what happen?

    – Amit Bera
    Sep 13 '18 at 7:28











  • in product edit page in admin show category as it is.

    – Rahul Vaghela
    Sep 13 '18 at 8:25














4












4








4


1






I have tried following code but does't work



$all_categoty=$_product->getCategoryIds();
$CategoryLinkRepository = $objectManager->get('MagentoCatalogModelCategoryLinkRepository');
foreach($all_categoty as $cat){
if($cat=="15"){
continue;
}
$CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
}


Then try with reindexing but doesn't remove the category



$productCategoryIndexer = $objectManager ->get('MagentoFrameworkIndexerIndexerRegistry')
->get(MagentoCatalogModelIndexerProductCategory::INDEXER_ID);
$productCategoryIndexer->reindexRow($_product->getId());









share|improve this question
















I have tried following code but does't work



$all_categoty=$_product->getCategoryIds();
$CategoryLinkRepository = $objectManager->get('MagentoCatalogModelCategoryLinkRepository');
foreach($all_categoty as $cat){
if($cat=="15"){
continue;
}
$CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
}


Then try with reindexing but doesn't remove the category



$productCategoryIndexer = $objectManager ->get('MagentoFrameworkIndexerIndexerRegistry')
->get(MagentoCatalogModelIndexerProductCategory::INDEXER_ID);
$productCategoryIndexer->reindexRow($_product->getId());






magento2 product category






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 13 '18 at 3:52









Amit Bera

57.7k1474172




57.7k1474172










asked Sep 13 '18 at 2:41









Rahul VaghelaRahul Vaghela

528




528













  • Code seems code ok.only need to add try and catch try{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }

    – Amit Bera
    Sep 13 '18 at 4:12











  • check getCategoryIds value.i guess that have issue only with it

    – Amit Bera
    Sep 13 '18 at 4:13











  • getCategoryIds its working fine

    – Rahul Vaghela
    Sep 13 '18 at 5:35











  • After run of $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku()); .Please do indexing from admin and see what happen?

    – Amit Bera
    Sep 13 '18 at 7:28











  • in product edit page in admin show category as it is.

    – Rahul Vaghela
    Sep 13 '18 at 8:25



















  • Code seems code ok.only need to add try and catch try{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }

    – Amit Bera
    Sep 13 '18 at 4:12











  • check getCategoryIds value.i guess that have issue only with it

    – Amit Bera
    Sep 13 '18 at 4:13











  • getCategoryIds its working fine

    – Rahul Vaghela
    Sep 13 '18 at 5:35











  • After run of $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku()); .Please do indexing from admin and see what happen?

    – Amit Bera
    Sep 13 '18 at 7:28











  • in product edit page in admin show category as it is.

    – Rahul Vaghela
    Sep 13 '18 at 8:25

















Code seems code ok.only need to add try and catch try{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }

– Amit Bera
Sep 13 '18 at 4:12





Code seems code ok.only need to add try and catch try{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }

– Amit Bera
Sep 13 '18 at 4:12













check getCategoryIds value.i guess that have issue only with it

– Amit Bera
Sep 13 '18 at 4:13





check getCategoryIds value.i guess that have issue only with it

– Amit Bera
Sep 13 '18 at 4:13













getCategoryIds its working fine

– Rahul Vaghela
Sep 13 '18 at 5:35





getCategoryIds its working fine

– Rahul Vaghela
Sep 13 '18 at 5:35













After run of $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku()); .Please do indexing from admin and see what happen?

– Amit Bera
Sep 13 '18 at 7:28





After run of $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku()); .Please do indexing from admin and see what happen?

– Amit Bera
Sep 13 '18 at 7:28













in product edit page in admin show category as it is.

– Rahul Vaghela
Sep 13 '18 at 8:25





in product edit page in admin show category as it is.

– Rahul Vaghela
Sep 13 '18 at 8:25










2 Answers
2






active

oldest

votes


















0














you're forgot to save category repository after modify:



$CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
$CategoryLinkRepository->save($category);


Also I recommend didn't use "magic numbers" like this one:



if($cat=="15"){
continue;
}


This is a bad practice, because categories id's may be changed when database is moved, plus when you or other developer will working with that code is remember why category id=15 must be skipped aren't easy.






share|improve this answer
























  • any other idea to skip this category??

    – Rahul Vaghela
    Sep 13 '18 at 7:12













  • $CategoryLinkRepository->save($category); which $category i have passed??

    – Rahul Vaghela
    Sep 13 '18 at 7:23





















0














please check this Magento2: Unassigned category from product and if your are updating a product a the same time is not gonna work



https://github.com/magento/magento2/issues/10555






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242030%2funassigned-category-from-product-in-magento-2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    you're forgot to save category repository after modify:



    $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
    $CategoryLinkRepository->save($category);


    Also I recommend didn't use "magic numbers" like this one:



    if($cat=="15"){
    continue;
    }


    This is a bad practice, because categories id's may be changed when database is moved, plus when you or other developer will working with that code is remember why category id=15 must be skipped aren't easy.






    share|improve this answer
























    • any other idea to skip this category??

      – Rahul Vaghela
      Sep 13 '18 at 7:12













    • $CategoryLinkRepository->save($category); which $category i have passed??

      – Rahul Vaghela
      Sep 13 '18 at 7:23


















    0














    you're forgot to save category repository after modify:



    $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
    $CategoryLinkRepository->save($category);


    Also I recommend didn't use "magic numbers" like this one:



    if($cat=="15"){
    continue;
    }


    This is a bad practice, because categories id's may be changed when database is moved, plus when you or other developer will working with that code is remember why category id=15 must be skipped aren't easy.






    share|improve this answer
























    • any other idea to skip this category??

      – Rahul Vaghela
      Sep 13 '18 at 7:12













    • $CategoryLinkRepository->save($category); which $category i have passed??

      – Rahul Vaghela
      Sep 13 '18 at 7:23
















    0












    0








    0







    you're forgot to save category repository after modify:



    $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
    $CategoryLinkRepository->save($category);


    Also I recommend didn't use "magic numbers" like this one:



    if($cat=="15"){
    continue;
    }


    This is a bad practice, because categories id's may be changed when database is moved, plus when you or other developer will working with that code is remember why category id=15 must be skipped aren't easy.






    share|improve this answer













    you're forgot to save category repository after modify:



    $CategoryLinkRepository->deleteByIds($cat ,$_product->getSku());
    $CategoryLinkRepository->save($category);


    Also I recommend didn't use "magic numbers" like this one:



    if($cat=="15"){
    continue;
    }


    This is a bad practice, because categories id's may be changed when database is moved, plus when you or other developer will working with that code is remember why category id=15 must be skipped aren't easy.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 13 '18 at 6:32









    Serge YudinSerge Yudin

    212112




    212112













    • any other idea to skip this category??

      – Rahul Vaghela
      Sep 13 '18 at 7:12













    • $CategoryLinkRepository->save($category); which $category i have passed??

      – Rahul Vaghela
      Sep 13 '18 at 7:23





















    • any other idea to skip this category??

      – Rahul Vaghela
      Sep 13 '18 at 7:12













    • $CategoryLinkRepository->save($category); which $category i have passed??

      – Rahul Vaghela
      Sep 13 '18 at 7:23



















    any other idea to skip this category??

    – Rahul Vaghela
    Sep 13 '18 at 7:12







    any other idea to skip this category??

    – Rahul Vaghela
    Sep 13 '18 at 7:12















    $CategoryLinkRepository->save($category); which $category i have passed??

    – Rahul Vaghela
    Sep 13 '18 at 7:23







    $CategoryLinkRepository->save($category); which $category i have passed??

    – Rahul Vaghela
    Sep 13 '18 at 7:23















    0














    please check this Magento2: Unassigned category from product and if your are updating a product a the same time is not gonna work



    https://github.com/magento/magento2/issues/10555






    share|improve this answer




























      0














      please check this Magento2: Unassigned category from product and if your are updating a product a the same time is not gonna work



      https://github.com/magento/magento2/issues/10555






      share|improve this answer


























        0












        0








        0







        please check this Magento2: Unassigned category from product and if your are updating a product a the same time is not gonna work



        https://github.com/magento/magento2/issues/10555






        share|improve this answer













        please check this Magento2: Unassigned category from product and if your are updating a product a the same time is not gonna work



        https://github.com/magento/magento2/issues/10555







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 31 mins ago









        davindavin

        416




        416






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f242030%2funassigned-category-from-product-in-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Polycentropodidae

            Magento 2 Error message: Invalid state change requested

            Paulmy