Unassigned category from product in magento 2
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
|
show 3 more comments
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
Code seems code ok.only need to add try and catchtry{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }
– Amit Bera♦
Sep 13 '18 at 4:12
checkgetCategoryIds
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
|
show 3 more comments
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
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
magento2 product category
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 catchtry{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }
– Amit Bera♦
Sep 13 '18 at 4:12
checkgetCategoryIds
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
|
show 3 more comments
Code seems code ok.only need to add try and catchtry{ }catch (MagentoFrameworkExceptionInputException $e) { }catch (MagentoFrameworkExceptionCouldNotSaveException $e ) { }
– Amit Bera♦
Sep 13 '18 at 4:12
checkgetCategoryIds
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
|
show 3 more comments
2 Answers
2
active
oldest
votes
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.
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 31 mins ago
davindavin
416
416
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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