Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Join Date
    Aug 2005
    Location
    Vic, Oz
    Posts
    1,905
    Plugin Contributions
    5

    Default 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?

  2. #2
    Join Date
    Jul 2012
    Posts
    16,816
    Plugin Contributions
    17

    Default 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:
    Code:
    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...

  3. #3
    Join Date
    Aug 2007
    Location
    Gijón, Asturias, Spain
    Posts
    2,826
    Plugin Contributions
    31

    Default Re: Mass unlink all Categories from one particular category

    I think this mod will do it:
    http://www.zen-cart.com/showthread.p...py-Move-Delete

    and this has also some useful features along the same lines:
    http://www.zen-cart.com/showthread.p...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...

  4. #4
    Join Date
    Aug 2005
    Location
    Vic, Oz
    Posts
    1,905
    Plugin Contributions
    5

    Default Re: Mass unlink all Categories from one particular category

    Quote Originally Posted by mc12345678 View Post
    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:
    Code:
    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 :)
    Code:
    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 the admin you need to add the table prefix to the second instance of the table reference.
    If run from phpmyadmin you need to add the table prefix to both instances of the table reference.





    Quote Originally Posted by torvista View Post
    This is one of my favorite "must have" mods but unfortunately won't do what I need in this case

    Quote Originally Posted by torvista View Post
    and this has also some useful features along the same lines:
    http://www.zen-cart.com/showthread.p...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.
    Last edited by gilby; 14 Nov 2014 at 01:27 PM.

  5. #5
    Join Date
    Jul 2012
    Posts
    16,816
    Plugin Contributions
    17

    Default 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...

  6. #6
    Join Date
    Aug 2005
    Location
    Vic, Oz
    Posts
    1,905
    Plugin Contributions
    5

    Default Re: Mass unlink all Categories from one particular category

    Quote Originally Posted by mc12345678 View Post
    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

  7. #7
    Join Date
    Jul 2012
    Posts
    16,816
    Plugin Contributions
    17

    Default 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.
    Last edited by mc12345678; 14 Nov 2014 at 02:19 PM. Reason: code did not get carried over... :/
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  8. #8
    Join Date
    Jul 2012
    Posts
    16,816
    Plugin Contributions
    17

    Default 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 Code:
    <?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...

  9. #9
    Join Date
    Aug 2005
    Location
    Vic, Oz
    Posts
    1,905
    Plugin Contributions
    5

    Default Re: Mass unlink all Categories from one particular category

    Quote Originally Posted by mc12345678 View Post
    Perhaps unnecessary for you, but others that land on this page, the php version of the statement would be:

    PHP Code:
    <?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 Code:
    <?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
    Code:
    [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?

  10. #10
    Join Date
    Sep 2003
    Location
    Ohio
    Posts
    69,402
    Plugin Contributions
    6

    Default 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!

 

 
Page 1 of 2 12 LastLast

Similar Threads

  1. how do mass delete all products from one manufacturer?
    By alibaba99 in forum Setting Up Categories, Products, Attributes
    Replies: 18
    Last Post: 17 Feb 2013, 09:36 AM
  2. Replies: 68
    Last Post: 4 Feb 2011, 09:38 PM
  3. Replies: 0
    Last Post: 10 Nov 2010, 11:52 AM
  4. Code to show all categories on a particular category page?
    By davemehta in forum General Questions
    Replies: 3
    Last Post: 5 Jul 2010, 07:45 PM
  5. All Zones EXCEPT particular one
    By PJD in forum Built-in Shipping and Payment Modules
    Replies: 0
    Last Post: 25 Apr 2008, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg