【工作日记】清除zen-cart中的冗余产品信息

  Zen-Cart 是一个非常强大的网上售货平台,因为其对用户开放全部源代码而受到了国内外众多外贸商的青睐,在国内拥有最多的用户。
  用得人多了,问题也随之出现,zen-cart拥有一个庞大的插件支持,数量众多的插件极大地方便了我们的使用,而easypopulate这个工具就是其中之一,它为我们提供了一个指添加产品的口径。
  但是由于操作上的不熟练,和zen-cart本身的缺陷,在长期使用easypopulate工具进行上传产品操作,和使用zen-cart后台进行删除产品目录的操作之后,数据库中会存在大量的冗余数据,这些数据轻则会占用大量的数据库空间,重则影响到你前台的显示。比如,你的网站以前卖婚纱礼服,但是现在不卖了,你对它进行了删除操作之后(删除了婚纱的目录,而没有事先在后台对这个目录中的产品进行逐个的删除操作),这样的话,数据库中就会残留婚纱的数据,而如果你新上传的产品使用了婚纱产品以前所使用的module号或products_id号的话,(因为products_to_categories表没有主键,所以,只要产品ID和所属目录只要不重复,插入ID值重复的产品是完全有可能的)已经被“删除”了的婚纱产品,就有可能借助新添加的产品被前台显示出来——(死灰复燃?)。
  而这种情形是我们非常不愿意看到的,因为我们需要清除掉这些没有必要的产品:
首先我们要做的是验证(当然,如果你非常自信你的数据库备份完好,不怕误删,那么可以直接进行最后两步的操作):
1、编写一个函数,用来查询产品的根目录(为什么要查在下面会提及);

/* 根据任何一个目录ID(categories_id)查询其根目录 */
drop function if exists get_parent1;
create function get_parent1(cid char(10))
returns integer
BEGIN
  SET @pid = cid;
  SET cid = get_parent_id(cid);
  WHILE cid <> 0 DO
    SET @pid = cid;
    SET cid = get_parent_id(cid);
  END WHILE;
  return @pid;
END

2、验证一下正常的产品信息总数和后台显示的是不是一致的:

SELECT count(*) FROM zen_products_to_categories ptc where
get_parent1(ptc.categories_id) = 2162
or get_parent1(ptc.categories_id) = 2586
or get_parent1(ptc.categories_id) = 2341
or get_parent1(ptc.categories_id) = 2349
or get_parent1(ptc.categories_id) = 2412
or get_parent1(ptc.categories_id) = 2474
or get_parent1(ptc.categories_id) = 2696
or get_parent1(ptc.categories_id) = 2744
or get_parent1(ptc.categories_id) = 3032;

如果数量是一致的话,那么就可以进行最后两步了:
3、删除zen_products_to_categories表中categories_id的没有根目录或根目录为无效(因为删除产品的时候是直接删除根目录的,子目录的记录还留在数据库中)的记录

delete from zen_products_to_categories where get_parent1(categories_id)<> 2162
and get_parent1(categories_id) <> 2586
and get_parent1(categories_id) <> 2341
and get_parent1(categories_id) <> 2349
and get_parent1(categories_id) <> 2412
and get_parent1(categories_id) <> 2474
and get_parent1(categories_id) <> 2696
and get_parent1(categories_id) <> 2744
and get_parent1(categories_id) <> 3032;

4、根据 zen_products_to_categories 表中的products_id判断不需要的产品,从各产品相关表中删除记录
delete from zen_product_music_extra where products_id not in(select products_id from zen_products_to_categories);
delete from zen_products where products_id not in(select products_id from zen_products_to_categories);
delete from zen_products_attributes where products_id not in(select products_id from zen_products_to_categories);
delete from zen_products_description where products_id not in(select products_id from zen_products_to_categories);
delete from zen_products_discount_quantity where products_id not in(select products_id from zen_products_to_categories);
delete from zen_products_notifications where products_id not in(select products_id from zen_products_to_categories);
delete from zen_products_with_attributes_stock where products_id not in(select products_id from zen_products_to_categories);

drop function if exists get_parent_id;
create function get_parent_id(var_id char(10))
returns integer
begin
SET @var_prt_id = (select parent_id from zen_categories where categories_id = var_id);
return @var_prt_id;
end

此条目发表在 zen-cart 分类目录,贴了 , 标签。将固定链接加入收藏夹。

【工作日记】清除zen-cart中的冗余产品信息》有 2 条评论

  1. ptwuxd 说:

    你好,请问这部分的程序要在哪里进行逐步操作? 谢谢

  2. Max 说:

    你好,查看了您首页的zen cart技术文章,觉得您对这个商业平台有很深的了解和认识,我遇到一点小麻烦,不知道可不可以请您帮看一下:

    我的网站有中英2种语言,用的是pureblue模板,英文正常,中文在点击导航条里”我的账号”以及创建新账号成功后打开的页面都是空白的,或者在首页选其他币种时也是空白.

    然后我运行了define(‘STRICT_ERROR_REPORTING’, true);来调试,结果有如下几个Warning:
    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /home5/newrocks/public_html/includes/classes/class.phpbb.php:34) in /home5/newrocks/public_html/includes/functions/sessions.php on line 108

    Warning: Cannot modify header information – headers already sent by (output started at /home5/newrocks/public_html/includes/classes/class.phpbb.php:34) in /home5/newrocks/public_html/includes/init_includes/init_templates.php on line 78

    Notice: Undefined index: customer_id in /home5/newrocks/public_html/includes/modules/pages/account/header_php.php on line 16

    Warning: Cannot modify header information – headers already sent by (output started at /home5/newrocks/public_html/includes/classes/class.phpbb.php:34) in /home5/newrocks/public_html/includes/functions/functions_general.php on line 44

    请您指点一下好吗?谢谢您的帮助!这个折磨我两三天了.

发表评论

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>