zencart index.php,ZenCart深入二次开发-核心文件说明

想对ZenCart深入了解的程序猿可以认真看看zencart的文件结构…

1.大家可以看到ZenCart根目录index.php文件中第一句话是包含include目录下的application_top.php文件,如:require(’includes/application_top.php’);

zencart系统中application_top.php负责的是初始化工作,比如加载配置文件include(’includes/configure.php’);,如果系统没检测到该文件的存在则会尝试调用安装文件。

然后它会自动遍历include/extra_configures下的配置文件并包含进来。

在加载了系统配置文件以后接下来是一个非常重要的文件,这也导致了zencart和oscommerce感觉上很大不同的原因,首先调用一个文件require(’includes/initsystem.php’);

在initsystem.php中最先加载include/auto_loaders/config.core.php,config.core.php 是一个二围数组$autoLoadConfig,即以数组的形式保存文件的信息供后面文件调用,然后系统会接着加载完 include/auto_loaders目录下所有文件名匹配$loaderPrefix(默认为config)的文件。

上面程序执行完以后就是加载自动执行程序了require(’includes/autoload_func.php’);在这里它会遍 历$autoLoadConfig数组,它最终执行的效果会包含所有必须用到的函数或者类的定义,还有变量的初始化,config.core.php里面 的注释比较清楚比如

$autoLoadConfig[0][] = array(’autoType’=>’class’,’loadFile’=>’class.base.php’);

在autoload_func.php里面执行完以后的效果就是require(DIR_WS_CLASSES . ‘class.base.php’),大部分的初始化化工作是通过包含init_includes目录下的文件来实现的,如:

$autoLoadConfig[110][] = array(’autoType’=>’init_script’,’loadFile’=> ‘init_templates.php’);

它在执行完autoload_func.php文件后就已经加载了init_includes目录下的init_templates.php文件。

2.下面来介绍下ZenCart是怎么根据摸版把内容显示出来的。

在index.php的第29行有句

$directory_array = $template->get_template_part($code_page_directory, ‘/^header_php/’);

由于所有初始化工作已经完成,所以我们就可以在上面的文件找到他们的定义,如

$autoLoadConfig[100][] = array(’autoType’=>’classInstantiate’,’className’=>’template_func’,’objectName’=>’template’);

在这里就定义了$template = new template_func(); ,然后$code_page_directory变量的定义是在init_includes/init_sanitize.php文件中定义在这里必须要 对class/template_func.php中定义的template_func类比较熟悉,在该类中主要定义了两个方法 get_template_dir()和get_template_part();

这两个方法在zencart的模板使用中起到了决定性的作用。

get_template_dir方法function get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false),它定义了5个参数,第一个参数一般是个文件名,它是用来判断后两个参数组成的目录中有没有匹配$template_code 的这个文件,该类复写了默认的系统函数file_exists所以很多初学者可能会比较迷惑

function get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false) {

//echo ‘template_default/’ . $template_dir . ‘=’ . $template_code;

if($this->file_exists($current_template . $current_page, $template_code)){

return $current_template . $current_page . ‘/’;

}elseif ($this->file_exists(DIR_WS_TEMPLATES . ‘template_default/’ . $current_page, ereg_replace(’/’, ”, $template_code), $debug)){

return DIR_WS_TEMPLATES . ‘template_default/’ . $current_page;

} elseif ($this->file_exists($current_template . $template_dir, ereg_replace(’/’, ”, $template_code), $debug)){

return $current_template . $template_dir;

} else {

return DIR_WS_TEMPLATES . ‘template_default/’ . $template_dir;

//return $current_template . $template_dir;

}

}

/*

includes/templates/zccn/index

includes/templates/template_default/index

includes/templates/zccn/common

includes/templates/template_default/common

*/

get_template_part()方法有两个函数,第一个参数是文件目录,第二个参数是匹配的条件,执行的结果是包含该目录下所有文件名匹配这个条件的文件

比如$directory_array = $template->get_template_part($code_page_directory, ‘/^header_php/’);

这句话执行的结果就是返回目录下$code_page_directory所有文件名以header_php开头的文件

如此时的url(http://localhost/zencart/index.php?main_page=product_info&cPath=49_27&products_id=83)

你现在应该查看init_sanitize.php中$code_page_directory的定义此时的$code_page_directory的值应该是includes/modules/product_info/

所以它就应该包含该目录下所有以header_php开头的文件,在这里好象就只有一个header_php.php

$directory_array = $template->get_template_part($code_page_directory, ‘/^header_php/’);这个包含文件其实是初始化前台不同页面显示所需要用到的变量函数,主要是初始化数据库的东西,因为每个页面需要的数据 资料都有可能不同,所以index.php?main_page=index 当main_page的值不同是在includes/modules/目录下都会有个对应的目录,这里是index目录

只要知道了这两个方法的用法,你就会知道模板文件都是怎么显示出来的了

再来解释一 require($template->get_template_dir(’html_header.php’,DIR_WS_TEMPLATE, $current_page_base,’common’). ‘/html_header.php’);

假设当前url:http://localhost/zencart/index.php?main_page=index&cPath=48

DIR_WS_TEMPLATE 定义是在includes/init_templates.php中定义define(’DIR_WS_TEMPLATE’, DIR_WS_TEMPLATES . $template_dir . ‘/’);,因为我现在用的是默认的zccn模板

所以现在的DIR_WS_TEMPLATE=includes/templates/zccn/

$current_page_base在这里已经就是index

上面已经解释了$template->get_template_dir()的方法了

程序会依次在

includes/templates/zccn/index

includes/templates/template_default/index

includes/templates/zccn/common

includes/templates/template_default/common

在以上四个目录中找html_header.php,最终在template_default/common目录下找到html_header.php,最先优先当前模板的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP 中,可以使用函数来实现购物车的加减功能。以下是一个基本的示例代码: ``` <?php session_start(); // 添加商品到购物车 function addToCart($product_id, $quantity) { // 如果购物车中已经有该商品,则增加数量 if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id] += $quantity; } // 否则,将商品添加到购物车 else { $_SESSION['cart'][$product_id] = $quantity; } } // 从购物车中删除商品 function removeFromCart($product_id) { if (isset($_SESSION['cart'][$product_id])) { unset($_SESSION['cart'][$product_id]); } } // 更新购物车中商品的数量 function updateCart($product_id, $quantity) { if (isset($_SESSION['cart'][$product_id])) { $_SESSION['cart'][$product_id] = $quantity; } } // 获取购物车中商品的数量 function getCartQuantity($product_id) { if (isset($_SESSION['cart'][$product_id])) { return $_SESSION['cart'][$product_id]; } else { return 0; } } ?> ``` 在 ZenCart 中,可以使用以下代码来在购物车页面中实现加减框: ``` <input type="text" name="products_quantity[]" size="4" value="<?php echo $products_quantity; ?>"> <input type="button" value="-" onclick="javascript:update_quantity(-1, '<?php echo $products_id; ?>');"> <input type="button" value="+" onclick="javascript:update_quantity(1, '<?php echo $products_id; ?>');"> <script> function update_quantity(change, product_id) { var quantity_input = document.getElementsByName('products_quantity[' + product_id + ']')[0]; var quantity = parseInt(quantity_input.value) + change; if (quantity < 1) { quantity = 1; } quantity_input.value = quantity; } </script> ``` 这段代码会在每个商品数量输入框旁边生成一个“-”和“+”按钮,点击“-”按钮会将商品数量减1,点击“+”按钮会将商品数量加1。需要注意的是,这段代码中的 `$products_id` 和 `$products_quantity` 变量需要根据具体情况进行替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值