Forums / General Questions / Mass unlink all Categories from one particular category

Mass unlink all Categories from one particular category

Views: 1,294

Results 1 to 11 of 11
13 Nov 2014, 11:54 AM
#1
gilby avatar

gilby

Totally Zenned

Join Date:
Aug 2005
Location:
Vic, Oz
Posts:
1,816
Plugin Contributions:
0

Mass unlink all Categories from one particular category

On one store I manage
When products are sold out I "disable" them
They are "moved" to a particular "disabled" category for storage
This changes their "master categories id" to this storage category
I would then like to "unlink" all other categories from products in this category
I can do this for particular categories, category by category in Multiple Categories Link Manager

Can anyone suggest a sql to do this in one go?

13 Nov 2014, 12:33 PM
#2
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Mass unlink all Categories from one particular category

Sounds like this "move" process doesn't move the product to a new category so much as link it to the category. I say that because if it were truly moved, there should be no remnant/reference of the product to a previous category.

The table that contains the information I think you are trying to remove is the products_to_categories table.
Thinking:

delete from products_to_categories where products_id in (select products_id from products_to_categories where categories_id=15) and categories_id != 15;

Where the categories_id of the "disabled" category is 15.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

13 Nov 2014, 1:45 PM
#3
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,875
Plugin Contributions:
7

Re: Mass unlink all Categories from one particular category

I think this mod will do it:
http://www.zen-cart.com/showthread.php?180447-Multiple-Products-Copy-Move-Delete

and this has also some useful features along the same lines:
http://www.zen-cart.com/showthread.php?172698-Multiple-Categories-Link-Manager-Sorted

and yes I think they should both be added to the core!

Steve
github.com/torvista: BackupMySQL, Structured Data, Multiple Copy-Move-Delete, Google reCaptcha, Image Checker, Spanish Language Pack and more...

14 Nov 2014, 12:22 PM
#4
gilby avatar

gilby

Totally Zenned

Join Date:
Aug 2005
Location:
Vic, Oz
Posts:
1,816
Plugin Contributions:
0

Re: Mass unlink all Categories from one particular category

mc12345678:

Sounds like this "move" process doesn't move the product to a new category so much as link it to the category. I say that because if it were truly moved, there should be no remnant/reference of the product to a previous category.

The table that contains the information I think you are trying to remove is the products_to_categories table.
Thinking:

delete from products_to_categories where products_id in (select products_id from products_to_categories where categories_id=15) and categories_id != 15;

> 
> Where the categories_id of the "disabled" category is 15.
No the "move" moves the master_categories_id from where it is now to the specified category. Other linked categories remain the same.

Your code inspired me but would not work as written, had to create an alias before it worked :)

delete from products_to_categories
where products_id
in (select * from
(select products_id
from products_to_categories
where categories_id=15)
as old_table)
and categories_id != 15;

If run from phpmyadmin you need to add the table prefix to both instances of the table reference.





> **torvista:**
>
> I think this mod will do it:
> <http://www.zen-cart.com/showthread.php?180447-Multiple-Products-Copy-Move-Delete>
This is one of my favorite "must have" mods but unfortunately won't do what I need in this case

> **torvista:**
>
> and this has also some useful features along the same lines:
> <http://www.zen-cart.com/showthread.php?172698-Multiple-Categories-Link-Manager-Sorted>
> 
> and yes I think they should both be added to the core!I have never tried this but will check it out.

Thanks to you both.
14 Nov 2014, 12:35 PM
#5
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Mass unlink all Categories from one particular category

Well the reason I stated that it doesn't completely move the product is evidenced by the issue being encountered as a change of master_category_id doesn't fully move the product away from it's current category otherwise the issue encountered wouldn't occur.

Interesting the difference in implementation needed for the SQL. The provided SQL works in a $db->Execute() without an additional temporary table. Glad it worked though. (Known to work as have used in further development of Stock By Attributes.)

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

14 Nov 2014, 12:44 PM
#6
gilby avatar

gilby

Totally Zenned

Join Date:
Aug 2005
Location:
Vic, Oz
Posts:
1,816
Plugin Contributions:
0

Re: Mass unlink all Categories from one particular category

mc12345678:

Interesting the difference in implementation needed for the SQL. The provided SQL works in a $db->Execute() without an additional temporary table. Glad it worked though. (Known to work as have used in further development of Stock By Attributes.)
I didn't try it as part of a php file
I ran it as a sql file
I will keep that in mind if I need to add to my website code

14 Nov 2014, 1:17 PM
#7
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Mass unlink all Categories from one particular category

Something went wrong with post. Need more time to write it again so am deleting this basically.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

14 Nov 2014, 1:20 PM
#8
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Mass unlink all Categories from one particular category

Perhaps unnecessary for you, but others that land on this page, the php version of the statement would be:

<?php 
$cat_holding = 15; // category that contains products to stay in category. 
$db->Execute("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id in (select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id=" . $cat_holding .") and categories_id != ". $cat_holding);

That method will include any table prefix associated with the database and not require consideration/addition of it.

ZC Installation/Maintenance Support <- Site
Contribution for contributions welcome...

16 Nov 2014, 7:03 AM
#9
gilby avatar

gilby

Totally Zenned

Join Date:
Aug 2005
Location:
Vic, Oz
Posts:
1,816
Plugin Contributions:
0

Re: Mass unlink all Categories from one particular category

mc12345678:

Perhaps unnecessary for you, but others that land on this page, the php version of the statement would be:

<?php $cat_holding = 15; // category that contains products to stay in category. $db->Execute("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id in (select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id=" . $cat_holding .") and categories_id != ". $cat_holding); ``` > > That method will include any table prefix associated with the database and not require consideration/addition of it.I tried that in a stand alone php file and it didn't work????```php <?php require('includes/application_top.php'); global $db; $cat_holding = 15; // category that contains products to stay in category. $db->Execute("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id in (select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id=" . $cat_holding .") and categories_id != ". $cat_holding); ``` Using zen 1.5.1 mysql 5.1.68-rel14.6-log php 5.4.12 table prefix zen_ I get the same error as when I used the SQL version without an alias ``` [16-Nov-2014 17:47:44 Australia/Melbourne] PHP Fatal error: 1093:You can't specify target table 'zen_products_to_categories' for update in FROM clause :: delete from zen_products_to_categories where products_id in (select products_id from zen_products_to_categories where categories_id=15) and categories_id != 15 in /home/******/public_html/zen151/includes/classes/db/mysql/query_factory.php on line 120 ``` (The table prefix isn't an issue) What versions are you using?
16 Nov 2014, 3:12 PM
#10
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Mass unlink all Categories from one particular category

Out of curiosity, would it work better for you if this feature to move the Product to categories_id 15 and unlink it from all all categories happened when the Order was made by the customer?

This could be customized for when the order is made and eliminate the whole mess and work involved in managing the Sold Out products ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

16 Nov 2014, 10:25 PM
#11
gilby avatar

gilby

Totally Zenned

Join Date:
Aug 2005
Location:
Vic, Oz
Posts:
1,816
Plugin Contributions:
0

Re: Mass unlink all Categories from one particular category

Ajeh:

Out of curiosity, would it work better for you if this feature to move the Product to categories_id 15 and unlink it from all all categories happened when the Order was made by the customer?

This could be customized for when the order is made and eliminate the whole mess and work involved in managing the Sold Out products ...In this store probably not...
These products go in and out of stock all the time.
At some point in time they "could" be discontinued, and that's when they are manually moved to an archived category.