网站开发 · 2025年4月26日

Zen-cart扩展国家表管理

Zen-cart中国家表的管理比较粗糙,特别是在要对应国家下拉列表进行调整时,比如把常用的国家调整到最前面,那么就需要扩展一下国家表字段了。

Zen-cart扩展国家表管理插图

涉及到国家表管理的是admin/countries.php文件,需要对这个文件做一些改动:

 
///////////////////////
$cnt
=
$db
->metaColumns(TABLE_COUNTRIES);
if
(!isset(
$cnt
[
strtoupper
(
'countries_name_cn'
)])){ 
    
$db
->Execute(
"ALTER TABLE "
.TABLE_COUNTRIES.
" ADD countries_name_cn VARCHAR( 64 ) NULL DEFAULT ''"
);
}
if
(!isset(
$cnt
[
strtoupper
(
'order_by'
)])){  
    
$db
->Execute(
"ALTER TABLE "
.TABLE_COUNTRIES.
" ADD order_by int(11) NOT NULL DEFAULT '0'"
);
}

///////////////////////
然后就是对插入编辑时的SQL进行修改,当然接下来还有修改表单(这里忽略)。

Zen-cart扩展国家表管理插图1

这样可以添加中文名称和排序码,排序码添加了之后还要修改两个获取国家下拉列表的函数(前台后台分别对应一个):

 
#includes/functions/functions_lookups.php
  
function
zen_get_countries(
$countries_id
=
''
,
$with_iso_codes
= false) {
    
global
$db
;  
    
$countries_array
=
array
();
    
if
(zen_not_null(
$countries_id
)) {
    
}
else
{
      
///////////////////////
      
$cnt
=
$db
->metaColumns(TABLE_COUNTRIES);
      
if
(!isset(
$cnt
[
strtoupper
(
'order_by'
)])){
        
$db
->Execute(
"ALTER TABLE "
.TABLE_COUNTRIES.
" ADD order_by int(11) NOT NULL DEFAULT '0'"
);
      
}
      
///////////////////////
      
$countries
= "select countries_id, countries_name
                    
from
" . TABLE_COUNTRIES . "
                    
order by order_by, countries_name";  
      
$countries_values
=
$db
->Execute(
$countries
);  
      
while
(!
$countries_values
->EOF) {
        
$countries_array
[] =
array
(
'countries_id'
=>
$countries_values
->fields[
'countries_id'
],
                                   
'countries_name'
=>
$countries_values
->fields[
'countries_name'
]);  
        
$countries_values
->MoveNext();
      
}
    
}  
    
return
$countries_array
;
  
}  
#admin/includes/functions/general.php
  
function
zen_get_countries(
$default
=
''
) {
    
global
$db
;
    
$countries_array
=
array
();
    
if
(
$default
) {
      
$countries_array
[] =
array
(
'id'
=>
''
,
                                 
'text'
=>
$default
);
    
}
    
    
///////////////////////
    
$cnt
=
$db
->metaColumns(TABLE_COUNTRIES);
    
if
(!isset(
$cnt
[
strtoupper
(
'countries_name_cn'
)])){ 
        
$db
->Execute(
"ALTER TABLE "
.TABLE_COUNTRIES.
" ADD countries_name_cn VARCHAR( 64 ) NULL DEFAULT ''"
);
    
}
    
if
(!isset(
$cnt
[
strtoupper
(
'order_by'
)])){  
        
$db
->Execute(
"ALTER TABLE "
.TABLE_COUNTRIES.
" ADD order_by int(11) NOT NULL DEFAULT '0'"
);
    
}
    
///////////////////////  
    
$countries
=
$db
->Execute("select countries_id, countries_name,countries_name_cn
                               
from
" . TABLE_COUNTRIES . "
                               
order by order_by, countries_name");  
    
while
(!
$countries
->EOF) {
      
$countries_array
[] =
array
(
'id'
=>
$countries
->fields[
'countries_id'
],
                                 
'text'
=>
$countries
->fields[
'countries_name'
].
" - "
.
$countries
->fields[
'countries_name_cn'
]);
      
$countries
->MoveNext();
    
}  
    
return
$countries_array
;
  
}

Zen-cart扩展国家表管理插图2

Zen-cart扩展国家表管理插图3

函数中加入了判断对应字段是否存在的逻辑,防止出错。