Mageno2: Unassign category from a product in magento2.1Magento2.1 Category custom attribute dropdownMagento2.1 - Show custom category attribute on frontendMagento 2: Products with internal IDs > ~16500 not showing in categoryShow subcategories in a parent category Magento2.1Magento2.1 change product attribute via mysqlSet title tag on Product thumb images Magento2.1on Magento2.1 how to import product with multiple categoryFotorama zoom magento2.1Magento2.1 wishlist on category pagemagento2.1 dropdown attribute list.phtml

What was Apollo 13's "Little Jolt" after MECO?

What to do with someone that cheated their way through university and a PhD program?

Can a stored procedure reference the database in which it is stored?

Co-worker works way more than he should

What *exactly* is electrical current, voltage, and resistance?

Nails holding drywall

Does a large simulator bay have standard public address announcements?

"The cow" OR "a cow" OR "cows" in this context

Why did Rep. Omar conclude her criticism of US troops with the phrase "NotTodaySatan"?

Which big number is bigger?

Von Neumann Extractor - Which bit is retained?

Contradiction proof for inequality of P and NP?

Where was the County of Thurn und Taxis located?

How do I produce this Greek letter koppa: Ϟ in pdfLaTeX?

How to not starve gigantic beasts

Negative Resistance

All ASCII characters with a given bit count

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

Is it acceptable to use working hours to read general interest books?

Restricting the options of a lookup field, based on the value of another lookup field?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

Check if a string is entirely made of the same substring

Is Diceware more secure than a long passphrase?

A ​Note ​on ​N!



Mageno2: Unassign category from a product in magento2.1


Magento2.1 Category custom attribute dropdownMagento2.1 - Show custom category attribute on frontendMagento 2: Products with internal IDs > ~16500 not showing in categoryShow subcategories in a parent category Magento2.1Magento2.1 change product attribute via mysqlSet title tag on Product thumb images Magento2.1on Magento2.1 how to import product with multiple categoryFotorama zoom magento2.1Magento2.1 wishlist on category pagemagento2.1 dropdown attribute list.phtml






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to unassign a category from a product. This category has a large number of products assigned to it (like 1500+). I used the code



`$CategoryLinkRepository ->deleteByIds($categoryId, $productSku);`


but this function essentially is un-tagging all this single product from category's assigned product list and saving the category. Which in turn is resulting in a lot of big queries (like real big queries which you won't want to see getting fired at your database) being fired at MySQL and getting timed out? A code snippet is as follows:



 public function deleteByIds($categoryId, $sku)

$category = $this->categoryRepository->get($categoryId);
$product = $this->productRepository->get($sku);
$productPositions = $category->getProductsPosition();

$productID = $product->getId();
if (!isset($productPositions[$productID]))
throw new InputException(__('Category does not contain specified product'));

$backupPosition = $productPositions[$productID];
unset($productPositions[$productID]);

$category->setPostedProducts($productPositions);
try
$category->save();
catch (Exception $e)
throw new CouldNotSaveException(
__(
'Could not save product "%product" with position %position to category %category',
[
"product" => $product->getId(),
"position" => $backupPosition,
"category" => $category->getId()
]
),
$e
);

return true;










share|improve this question
























  • If I use a query like delete from catalog_category_product where category_id = 4 and product_id = 46;. Should this work safely? I understand that there could be some garbage data left like urls etc.

    – Gaurav Pandey
    Apr 21 at 17:03


















0















I am trying to unassign a category from a product. This category has a large number of products assigned to it (like 1500+). I used the code



`$CategoryLinkRepository ->deleteByIds($categoryId, $productSku);`


but this function essentially is un-tagging all this single product from category's assigned product list and saving the category. Which in turn is resulting in a lot of big queries (like real big queries which you won't want to see getting fired at your database) being fired at MySQL and getting timed out? A code snippet is as follows:



 public function deleteByIds($categoryId, $sku)

$category = $this->categoryRepository->get($categoryId);
$product = $this->productRepository->get($sku);
$productPositions = $category->getProductsPosition();

$productID = $product->getId();
if (!isset($productPositions[$productID]))
throw new InputException(__('Category does not contain specified product'));

$backupPosition = $productPositions[$productID];
unset($productPositions[$productID]);

$category->setPostedProducts($productPositions);
try
$category->save();
catch (Exception $e)
throw new CouldNotSaveException(
__(
'Could not save product "%product" with position %position to category %category',
[
"product" => $product->getId(),
"position" => $backupPosition,
"category" => $category->getId()
]
),
$e
);

return true;










share|improve this question
























  • If I use a query like delete from catalog_category_product where category_id = 4 and product_id = 46;. Should this work safely? I understand that there could be some garbage data left like urls etc.

    – Gaurav Pandey
    Apr 21 at 17:03














0












0








0








I am trying to unassign a category from a product. This category has a large number of products assigned to it (like 1500+). I used the code



`$CategoryLinkRepository ->deleteByIds($categoryId, $productSku);`


but this function essentially is un-tagging all this single product from category's assigned product list and saving the category. Which in turn is resulting in a lot of big queries (like real big queries which you won't want to see getting fired at your database) being fired at MySQL and getting timed out? A code snippet is as follows:



 public function deleteByIds($categoryId, $sku)

$category = $this->categoryRepository->get($categoryId);
$product = $this->productRepository->get($sku);
$productPositions = $category->getProductsPosition();

$productID = $product->getId();
if (!isset($productPositions[$productID]))
throw new InputException(__('Category does not contain specified product'));

$backupPosition = $productPositions[$productID];
unset($productPositions[$productID]);

$category->setPostedProducts($productPositions);
try
$category->save();
catch (Exception $e)
throw new CouldNotSaveException(
__(
'Could not save product "%product" with position %position to category %category',
[
"product" => $product->getId(),
"position" => $backupPosition,
"category" => $category->getId()
]
),
$e
);

return true;










share|improve this question
















I am trying to unassign a category from a product. This category has a large number of products assigned to it (like 1500+). I used the code



`$CategoryLinkRepository ->deleteByIds($categoryId, $productSku);`


but this function essentially is un-tagging all this single product from category's assigned product list and saving the category. Which in turn is resulting in a lot of big queries (like real big queries which you won't want to see getting fired at your database) being fired at MySQL and getting timed out? A code snippet is as follows:



 public function deleteByIds($categoryId, $sku)

$category = $this->categoryRepository->get($categoryId);
$product = $this->productRepository->get($sku);
$productPositions = $category->getProductsPosition();

$productID = $product->getId();
if (!isset($productPositions[$productID]))
throw new InputException(__('Category does not contain specified product'));

$backupPosition = $productPositions[$productID];
unset($productPositions[$productID]);

$category->setPostedProducts($productPositions);
try
$category->save();
catch (Exception $e)
throw new CouldNotSaveException(
__(
'Could not save product "%product" with position %position to category %category',
[
"product" => $product->getId(),
"position" => $backupPosition,
"category" => $category->getId()
]
),
$e
);

return true;







magento2 magento-2.1






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 22 at 9:32









Navin Bhudiya

8391125




8391125










asked Apr 21 at 16:51









Gaurav PandeyGaurav Pandey

1013




1013












  • If I use a query like delete from catalog_category_product where category_id = 4 and product_id = 46;. Should this work safely? I understand that there could be some garbage data left like urls etc.

    – Gaurav Pandey
    Apr 21 at 17:03


















  • If I use a query like delete from catalog_category_product where category_id = 4 and product_id = 46;. Should this work safely? I understand that there could be some garbage data left like urls etc.

    – Gaurav Pandey
    Apr 21 at 17:03

















If I use a query like delete from catalog_category_product where category_id = 4 and product_id = 46;. Should this work safely? I understand that there could be some garbage data left like urls etc.

– Gaurav Pandey
Apr 21 at 17:03






If I use a query like delete from catalog_category_product where category_id = 4 and product_id = 46;. Should this work safely? I understand that there could be some garbage data left like urls etc.

– Gaurav Pandey
Apr 21 at 17:03











0






active

oldest

votes












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%2f270888%2fmageno2-unassign-category-from-a-product-in-magento2-1%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f270888%2fmageno2-unassign-category-from-a-product-in-magento2-1%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

Sum ergo cogito? 1 nng

419 nièngy_Soadمي 19bal1.5o_g

Queiggey Chernihivv 9NnOo i Zw X QqKk LpB