[TomatoCart] Prevent Disabled Products from Being Enabled Automatically as Changing Category Title

One user contacted me several days ago to report the following issue:

Every time I make a change to a Category title, all the disabled products under that category are enabled and showing in product pages to the public.

Once tested it locally, I noticed that the issue was really existed in current TomatoCart version. If there are a lot of products in one category, this issue will make definitely make you angry because all of disabled products will be enabled again after you modify the category name or other general information.

I disabled the products in desktop category as follow:

Snip20141116_6

Then, I modified the Desktops category to desk:

Snip20141116_7

The result was that the previous disabled products were enabled again:

Snip20141116_11

After verifying the correlative code, I find the reason of the problem:

In original code logic, when the category is modified and saved, the category status will be checked overtime whenever it is modified or not. If the categories status is being enabled, all of the disabled products in the category or its sub-categories will be enabled automatically. This is absolutely wrong. So, we need to adjust the code to change the logic.

Steps to clear this issue

1. Go to admin/includes/classes/categories.php.

2. Find following code snippet:

function save($id = null, $data) {
      global $osC_Database, $osC_Language;
      
      $category_id = '';
      $error = false;

      $osC_Database->startTransaction();

      if ( is_numeric($id) ) {
        //editing the parent category
        if (isset($data['subcategories'])) {
          $data['subcategories'][] = $id;
          
          $Qcat = $osC_Database->query('update :table_categories set categories_status = :categories_status, sort_order = :sort_order, last_modified = now() where categories_id in (:categories_ids)');
          $Qcat->bindRaw(':categories_ids', implode(',', $data['subcategories']));
        }else {
          $Qcat = $osC_Database->query('update :table_categories set categories_status = :categories_status, sort_order = :sort_order, last_modified = now() where categories_id = :categories_id');
          $Qcat->bindInt(':categories_id', $id);
        }
      } else {
        $Qcat = $osC_Database->query('insert into :table_categories (parent_id, categories_status, sort_order, date_added) values (:parent_id, :categories_status, :sort_order, now())');
        $Qcat->bindInt(':parent_id', $data['parent_id']);
      }

      $Qcat->bindTable(':table_categories', TABLE_CATEGORIES);
      $Qcat->bindInt(':sort_order', $data['sort_order']);
      $Qcat->bindInt(':categories_status', $data['categories_status']);
      $Qcat->setLogging($_SESSION['module'], $id);
      $Qcat->execute();
      
      if ( !$osC_Database->isError() ) {
        $category_id = (is_numeric($id)) ? $id : $osC_Database->nextID();
        
        if(is_numeric($id)) {
          if($data['categories_status']){
            //editing the parent category
            if (isset($data['subcategories'])) {
              $data['subcategories'][] = $id;
              
              $Qpstatus = $osC_Database->query('update :table_products set products_status = 1 where products_id in (select products_id from :table_products_to_categories where categories_id in (:categories_ids))');
              $Qpstatus->bindRaw(':categories_ids', implode(',', $data['subcategories']));
            }else {
              $Qpstatus = $osC_Database->query('update :table_products set products_status = 1 where products_id in (select products_id from :table_products_to_categories where categories_id = :categories_id)');
              $Qpstatus->bindInt(":categories_id", $id);
            }
            
            $Qpstatus->bindTable(':table_products', TABLE_PRODUCTS);
            $Qpstatus->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
            $Qpstatus->execute(); 
          }else{
            if($data['flag']) {
              //editing the parent category
              if (isset($data['subcategories'])) {
                $data['subcategories'][] = $id;
                
                $Qpstatus = $osC_Database->query('update :table_products set products_status = 0 where products_id in (select products_id from :table_products_to_categories where categories_id in (:categories_ids))');
                $Qpstatus->bindRaw(':categories_ids', implode(',', $data['subcategories']));
              }else {
                $Qpstatus = $osC_Database->query('update :table_products set products_status = 0 where products_id in (select products_id from :table_products_to_categories where categories_id = :categories_id)');
                $Qpstatus->bindInt(":categories_id", $id);
              }
            
              $Qpstatus->bindTable(':table_products', TABLE_PRODUCTS);
              $Qpstatus->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
              $Qpstatus->execute();
            }          
          }
        }
        
        if($osC_Database->isError()){
          $error = true;
        }
        
        foreach ($osC_Language->getAll() as $l) {
          if ( is_numeric($id) ) {
            $Qcd = $osC_Database->query('update :table_categories_description set categories_name = :categories_name, categories_url = :categories_url, categories_page_title = :categories_page_title, categories_meta_keywords = :categories_meta_keywords, categories_meta_description = :categories_meta_description where categories_id = :categories_id and language_id = :language_id');
          } else {
            $Qcd = $osC_Database->query('insert into :table_categories_description (categories_id, language_id, categories_name, categories_url, categories_page_title, categories_meta_keywords, categories_meta_description) values (:categories_id, :language_id, :categories_name, :categories_url, :categories_page_title, :categories_meta_keywords, :categories_meta_description)');
          }

          $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
          $Qcd->bindInt(':categories_id', $category_id);
          $Qcd->bindInt(':language_id', $l['id']);
          $Qcd->bindValue(':categories_name', $data['name'][$l['id']]);
          $Qcd->bindValue(':categories_url', $data['url'][$l['id']]);
          $Qcd->bindValue(':categories_page_title', $data['page_title'][$l['id']]);
          $Qcd->bindValue(':categories_meta_keywords', $data['meta_keywords'][$l['id']]);
          $Qcd->bindValue(':categories_meta_description', $data['meta_description'][$l['id']]);
          $Qcd->setLogging($_SESSION['module'], $category_id);
          $Qcd->execute();

          if ( $osC_Database->isError() ) {
            $error = true;
            break;
          }
        }
        
        $Qdelete = $osC_Database->query('delete from :toc_categories_ratings where categories_id = :categories_id');
        $Qdelete->bindTable(':toc_categories_ratings', TABLE_CATEGORIES_RATINGS);
        $Qdelete->bindInt(':categories_id', $category_id);
        $Qdelete->execute();
          
        if ( !empty($data['ratings']) ) {
          $ratings = explode(',', $data['ratings']);
          
          foreach($ratings as $ratings_id){
            $Qinsert = $osC_Database->query('insert into :toc_categories_ratings (categories_id, ratings_id) values (:categories_id, :ratings_id)');
            $Qinsert->bindTable(':toc_categories_ratings', TABLE_CATEGORIES_RATINGS);
            $Qinsert->bindInt(':categories_id', $category_id);
            $Qinsert->bindInt(':ratings_id', $ratings_id);
            $Qinsert->execute();
            
            if ( $osC_Database->isError() ) {
            	$error = true;
            	break;
            }
          }
        }

        if ( $error === false ) {
          $categories_image = new upload($data['image'], realpath('../' . DIR_WS_IMAGES . 'categories'));
          
          if ( $categories_image->exists() && $categories_image->parse() && $categories_image->save() ) {

            $Qimage = $osC_Database->query('select categories_image from :table_categories where categories_id = :categories_id');
            $Qimage->bindTable(':table_categories', TABLE_CATEGORIES);
            $Qimage->bindInt(':categories_id', $category_id);
            $Qimage->execute();

            $old_image = $Qimage->value('categories_image');
          
            if (!empty($old_image)) {
              $Qcheck = $osC_Database->query('select count(*) as image_count from :table_categories where categories_image = :categories_image');
              $Qcheck->bindTable(':table_categories', TABLE_CATEGORIES);
              $Qcheck->bindValue(':categories_image', $old_image);
              $Qcheck->execute();
              
              if ($Qcheck->valueInt('image_count') == 1) {
                $path = realpath('../' . DIR_WS_IMAGES . 'categories') . '/' . $old_image;
                unlink($path);
              }
            }

            $Qcf = $osC_Database->query('update :table_categories set categories_image = :categories_image where categories_id = :categories_id');
            $Qcf->bindTable(':table_categories', TABLE_CATEGORIES);
            $Qcf->bindValue(':categories_image', $categories_image->filename);
            $Qcf->bindInt(':categories_id', $category_id);
            $Qcf->setLogging($_SESSION['module'], $category_id);
            $Qcf->execute();

            if ( $osC_Database->isError() ) {
              $error = true;
            }
          }
        }
      }

      if ( $error === false ) {
        $osC_Database->commitTransaction();

        osC_Cache::clear('categories');
        osC_Cache::clear('category_tree');
        osC_Cache::clear('also_purchased');

        return $category_id;
      }

      $osC_Database->rollbackTransaction();

      return false;
    }

Replace it with:

function save($id = null, $data) {
      global $osC_Database, $osC_Language;
      
      $category_id = '';
      $error = false;
      $original_categories_status;

      $osC_Database->startTransaction();

      if ( is_numeric($id) ) {
      	$Qcat_status = $osC_Database->query('select categories_status from :table_categories where categories_id = :categories_id limit 1');
      	$Qcat_status->bindTable(':table_categories', TABLE_CATEGORIES);
      	$Qcat_status->bindInt(':categories_id', $id);
      	$Qcat_status->execute();
      	
      	$original_categories_status = $Qcat_status->valueInt('categories_status');
      	
      	$Qcat_status->freeResult();
      	
        //editing the parent category
        if (isset($data['subcategories'])) {
          $data['subcategories'][] = $id;
          
          $Qcat = $osC_Database->query('update :table_categories set categories_status = :categories_status, sort_order = :sort_order, last_modified = now() where categories_id in (:categories_ids)');
          $Qcat->bindRaw(':categories_ids', implode(',', $data['subcategories']));
        }else {
          $Qcat = $osC_Database->query('update :table_categories set categories_status = :categories_status, sort_order = :sort_order, last_modified = now() where categories_id = :categories_id');
          $Qcat->bindInt(':categories_id', $id);
        }
      } else {
        $Qcat = $osC_Database->query('insert into :table_categories (parent_id, categories_status, sort_order, date_added) values (:parent_id, :categories_status, :sort_order, now())');
        $Qcat->bindInt(':parent_id', $data['parent_id']);
      }

      $Qcat->bindTable(':table_categories', TABLE_CATEGORIES);
      $Qcat->bindInt(':sort_order', $data['sort_order']);
      $Qcat->bindInt(':categories_status', $data['categories_status']);
      $Qcat->setLogging($_SESSION['module'], $id);
      $Qcat->execute();
      
      if ( !$osC_Database->isError() ) {
        $category_id = (is_numeric($id)) ? $id : $osC_Database->nextID();
        
        if(is_numeric($id)) {
          //fix issue#241 - Disabled products in one category been enabled automatically
          if ($data['categories_status'] != $original_categories_status) {
          	if($data['categories_status'] ){
          		//editing the parent category
          		if (isset($data['subcategories'])) {
          			$data['subcategories'][] = $id;
          	
          			$Qpstatus = $osC_Database->query('update :table_products set products_status = 1 where products_id in (select products_id from :table_products_to_categories where categories_id in (:categories_ids))');
          			$Qpstatus->bindRaw(':categories_ids', implode(',', $data['subcategories']));
          		}else {
          			$Qpstatus = $osC_Database->query('update :table_products set products_status = 1 where products_id in (select products_id from :table_products_to_categories where categories_id = :categories_id)');
          			$Qpstatus->bindInt(":categories_id", $id);
          		}
          	
          		$Qpstatus->bindTable(':table_products', TABLE_PRODUCTS);
          		$Qpstatus->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
          		$Qpstatus->execute();
          	}else{
          		if($data['flag']) {
          			//editing the parent category
          			if (isset($data['subcategories'])) {
          				$data['subcategories'][] = $id;
          	
          				$Qpstatus = $osC_Database->query('update :table_products set products_status = 0 where products_id in (select products_id from :table_products_to_categories where categories_id in (:categories_ids))');
          				$Qpstatus->bindRaw(':categories_ids', implode(',', $data['subcategories']));
          			}else {
          				$Qpstatus = $osC_Database->query('update :table_products set products_status = 0 where products_id in (select products_id from :table_products_to_categories where categories_id = :categories_id)');
          				$Qpstatus->bindInt(":categories_id", $id);
          			}
          	
          			$Qpstatus->bindTable(':table_products', TABLE_PRODUCTS);
          			$Qpstatus->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
          			$Qpstatus->execute();
          		}
          	}
          }	
        }
        
        if($osC_Database->isError()){
          $error = true;
        }
        
        foreach ($osC_Language->getAll() as $l) {
          if ( is_numeric($id) ) {
            $Qcd = $osC_Database->query('update :table_categories_description set categories_name = :categories_name, categories_url = :categories_url, categories_page_title = :categories_page_title, categories_meta_keywords = :categories_meta_keywords, categories_meta_description = :categories_meta_description where categories_id = :categories_id and language_id = :language_id');
          } else {
            $Qcd = $osC_Database->query('insert into :table_categories_description (categories_id, language_id, categories_name, categories_url, categories_page_title, categories_meta_keywords, categories_meta_description) values (:categories_id, :language_id, :categories_name, :categories_url, :categories_page_title, :categories_meta_keywords, :categories_meta_description)');
          }

          $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
          $Qcd->bindInt(':categories_id', $category_id);
          $Qcd->bindInt(':language_id', $l['id']);
          $Qcd->bindValue(':categories_name', $data['name'][$l['id']]);
          $Qcd->bindValue(':categories_url', $data['url'][$l['id']]);
          $Qcd->bindValue(':categories_page_title', $data['page_title'][$l['id']]);
          $Qcd->bindValue(':categories_meta_keywords', $data['meta_keywords'][$l['id']]);
          $Qcd->bindValue(':categories_meta_description', $data['meta_description'][$l['id']]);
          $Qcd->setLogging($_SESSION['module'], $category_id);
          $Qcd->execute();

          if ( $osC_Database->isError() ) {
            $error = true;
            break;
          }
        }
        
        $Qdelete = $osC_Database->query('delete from :toc_categories_ratings where categories_id = :categories_id');
        $Qdelete->bindTable(':toc_categories_ratings', TABLE_CATEGORIES_RATINGS);
        $Qdelete->bindInt(':categories_id', $category_id);
        $Qdelete->execute();
          
        if ( !empty($data['ratings']) ) {
          $ratings = explode(',', $data['ratings']);
          
          foreach($ratings as $ratings_id){
            $Qinsert = $osC_Database->query('insert into :toc_categories_ratings (categories_id, ratings_id) values (:categories_id, :ratings_id)');
            $Qinsert->bindTable(':toc_categories_ratings', TABLE_CATEGORIES_RATINGS);
            $Qinsert->bindInt(':categories_id', $category_id);
            $Qinsert->bindInt(':ratings_id', $ratings_id);
            $Qinsert->execute();
            
            if ( $osC_Database->isError() ) {
            	$error = true;
            	break;
            }
          }
        }

        if ( $error === false ) {
          $categories_image = new upload($data['image'], realpath('../' . DIR_WS_IMAGES . 'categories'));
          
          if ( $categories_image->exists() && $categories_image->parse() && $categories_image->save() ) {

            $Qimage = $osC_Database->query('select categories_image from :table_categories where categories_id = :categories_id');
            $Qimage->bindTable(':table_categories', TABLE_CATEGORIES);
            $Qimage->bindInt(':categories_id', $category_id);
            $Qimage->execute();

            $old_image = $Qimage->value('categories_image');
          
            if (!empty($old_image)) {
              $Qcheck = $osC_Database->query('select count(*) as image_count from :table_categories where categories_image = :categories_image');
              $Qcheck->bindTable(':table_categories', TABLE_CATEGORIES);
              $Qcheck->bindValue(':categories_image', $old_image);
              $Qcheck->execute();
              
              if ($Qcheck->valueInt('image_count') == 1) {
                $path = realpath('../' . DIR_WS_IMAGES . 'categories') . '/' . $old_image;
                unlink($path);
              }
            }

            $Qcf = $osC_Database->query('update :table_categories set categories_image = :categories_image where categories_id = :categories_id');
            $Qcf->bindTable(':table_categories', TABLE_CATEGORIES);
            $Qcf->bindValue(':categories_image', $categories_image->filename);
            $Qcf->bindInt(':categories_id', $category_id);
            $Qcf->setLogging($_SESSION['module'], $category_id);
            $Qcf->execute();

            if ( $osC_Database->isError() ) {
              $error = true;
            }
          }
        }
      }

      if ( $error === false ) {
        $osC_Database->commitTransaction();

        osC_Cache::clear('categories');
        osC_Cache::clear('category_tree');
        osC_Cache::clear('also_purchased');

        return $category_id;
      }

      $osC_Database->rollbackTransaction();

      return false;
    }

Okay now. After changing the code as above, the issue will be cleared.

Final Words

If you need help, same thing, drop me a message via email or Skype. I’d be glad to help.

Email: support@tomatocart.com 

Skype: tocjack

 Looking for quality TomatoCart Hosting! Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under TomatoCart | RSS 2.0

Author Spotlight

Jack Yin

TomatoCart Developer & Co Founder - Arvixe Web Hosting / TomatoCart Community Liaison

Leave a Reply

Your email address will not be published. Required fields are marked *