
在 prestashop 1.7 中,许多开发者尝试通过直接修改 src/prestashopbundle/resources/views/admin/product/catalogpage/lists/products_table.html.twig 和 src/prestashopbundle/resources/views/admin/product/catalogpage/lists/list.html.twig 等 twig 模板文件来添加新的产品列。然而,这种方法通常无法成功显示数据(例如显示“n/a”),且存在以下主要问题:
为了安全、稳定且可持续地扩展 PrestaShop 后台功能,我们应该利用其提供的钩子(Hooks)机制。
PrestaShop 的钩子机制允许模块在特定事件发生时执行自定义代码,从而在不修改核心文件的情况下扩展或修改系统行为。对于后台产品列表,actionAdminProductsListingFieldsModifier 钩子是一个非常强大的工具,它允许我们在产品列表的字段定义和数据查询阶段进行干预。
actionAdminProductsListingFieldsModifier 钩子在产品列表数据被查询和字段被渲染之前触发。通过实现这个钩子,我们可以:
首先,我们需要创建一个新的 PrestaShop 模块。模块的结构通常如下:
mycustomwholesaleprice/ ├── mycustomwholesaleprice.php ├── config.xml ├── README.md ├── views/ │ └── templates/ │ └── admin/ │ └── configure.tpl
在 mycustomwholesaleprice.php 文件中,我们将定义模块的基本信息并注册钩子。
示例代码:mycustomwholesaleprice.php
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class MyCustomWholesalePrice extends Module
{
public function __construct()
{
$this->name = 'mycustomwholesaleprice';
$this->tab = 'front_office_features'; // 模块分类
$this->version = '1.0.0';
$this->author = 'Your Name';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.7.0.0',
'max' => _PS_VERSION_,
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Custom Wholesale Price Column');
$this->description = $this->l('Adds a wholesale price column to the product listing in the back office.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');
}
public function install()
{
if (!parent::install() ||
!$this->registerHook('actionAdminProductsListingFieldsModifier')) {
return false;
}
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!$this->unregisterHook('actionAdminProductsListingFieldsModifier')) {
return false;
}
return true;
}
/**
* Hook to modify the fields and SQL query for the product listing.
*
* @param array $params Contains 'fields', 'sql_select', 'sql_join', 'sql_where', 'sql_group_by', 'sql_order_by'
*/
public function hookActionAdminProductsListingFieldsModifier(array $params)
{
// 添加 wholesale_price 到 SELECT 语句
// 'ps' 是 ps_product_shop 表的常用别名
$params['sql_select'] .= ', ps.wholesale_price';
// 定义新的列
// 'wholesale_price' 键必须与 SQL_SELECT 中选择的字段名一致
$params['fields']['wholesale_price'] = [
'title' => $this->l('Wholesale price'), // 列标题,支持翻译
'align' => 'text-center', // 对齐方式
'type' => 'price', // 数据类型,PrestaShop 会自动格式化为货币
'filter_key' => 'ps!wholesale_price', // 用于过滤的键 (表别名!字段名)
'orderby_key' => 'ps!wholesale_price', // 用于排序的键
'search' => true, // 允许在此列进行搜索
'havingFilter' => true, // 允许在此列进行过滤
];
}
}
在上面的 mycustomwholesaleprice.php 文件中,我们已经包含了 hookActionAdminProductsListingFieldsModifier 方法的实现。让我们详细解释其内部逻辑:
$params['sql_select'] .= ', ps.wholesale_price';
$params['fields']['wholesale_price'] = [...]
通过利用 PrestaShop 1.7 的 actionAdminProductsListingFieldsModifier 钩子,我们可以以一种安全、高效且可维护的方式,在后台产品列表页面添加自定义列。这种模块化的方法不仅避免了直接修改核心文件带来的风险,还为开发者提供了极大的灵活性,以满足各种业务需求,同时确保了系统的长期稳定运行。掌握钩子的使用是成为一名合格 PrestaShop 开发者不可或缺的技能。
以上就是PrestaShop 1.7 后台产品列表添加批发价列的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号