
本教程详细介绍了如何在PrestaShop 1.7后台产品目录列表中添加“批发价”列。文章将解释为何直接修改模板文件可能无效,并提供使用actionAdminProductsListingFieldsModifier钩子创建自定义模块的专业解决方案,确保数据正确显示且系统可升级。
1. 问题背景与传统方法局限性
PrestaShop 1.7的后台产品目录列表默认不显示产品的批发价(wholesale_price)。许多用户尝试通过直接编辑主题或模块的Twig模板文件(例如products_table.html.twig和list.html.twig)来添加这一列。尽管可以在模板中添加列的标题和数据占位符,但通常会发现{{ product.wholesale_price }}变量显示为“N/A”或空值,这表明产品数据对象在传递给模板时并未包含批发价信息。直接修改核心文件不仅容易在系统升级时被覆盖,也无法解决数据未加载的根本问题。
2. 推荐解决方案:使用actionAdminProductsListingFieldsModifier钩子
在PrestaShop中,最佳实践是利用其强大的钩子(Hooks)系统进行功能扩展。对于修改后台列表的字段,actionAdminProductsListingFieldsModifier钩子是专门为此目的设计的。这个钩子允许开发者在产品列表数据被渲染之前,动态地添加、修改或删除列表中的字段定义和对应的数据。
2.1 钩子原理
actionAdminProductsListingFieldsModifier钩子会在PrestaShop后台产品列表加载时触发。它会接收一个包含当前列表字段定义($params[‘fields’])和产品数据数组($params[‘list’])的参数。通过在这个钩子的回调函数中操作这些参数,我们可以实现:
- 添加新的列定义:在$params[‘fields’]中添加批发价列的标题、类型、对齐方式等。
- 注入数据:遍历$params[‘list’]中的每个产品,从数据库或产品对象中获取其批发价,并将其添加到对应的产品数据数组中。
2.2 实现步骤:创建自定义模块
为了实现这一功能,我们需要创建一个简单的PrestaShop模块。
步骤1:创建模块基本结构
在PrestaShop的modules目录下创建一个新文件夹,例如mywholesale。在该文件夹内创建主模块文件mywholesale.php。
/modules
/mywholesale
mywholesale.php
/views
/templates
/admin
// 可选:如果需要自定义显示,可以在这里放twig文件
步骤2:编写模块主文件 mywholesale.php
以下是一个mywholesale.php的示例代码,展示了如何注册钩子并实现其回调函数:
<?php
/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2020 PrestaShop SA
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class MyWholesale extends Module
{
public function __construct()
{
$this->name = 'mywholesale';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Your Name';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Wholesale Price Column');
$this->description = $this->l('Adds a wholesale price column to the product catalog list in the back office.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
if (parent::install() && $this->registerHook('actionAdminProductsListingFieldsModifier')) {
return true;
}
return false;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return true;
}
/**
* Hook to modify the product listing fields and data in the back office.
*
* @param array $params Contains 'fields' (column definitions) and 'list' (product data).
*/
public function hookActionAdminProductsListingFieldsModifier(array &$params)
{
// 1. Add the new column definition
$params['fields']['wholesale_price'] = [
'title' => $this->l('Wholesale Price'),
'align' => 'text-center',
'class' => 'fixed-width-xl', // Adjust width as needed
'type' => 'price', // 'text', 'decimal', 'price' etc.
'orderby' => true, // Make it sortable
'search' => false, // Not searchable by default
];
// 2. Iterate through the products to inject wholesale price data
foreach ($params['list'] as &$product) {
// Ensure product ID is available
if (isset($product['id_product'])) {
// Load the full Product object to get wholesale_price
$productObj = new Product($product['id_product'], false, $this->context->language->id);
// Add wholesale_price to the product data array
// Format as currency if type is 'price'
$product['wholesale_price'] = Tools::displayPrice($productObj->wholesale_price, $this->context->currency);
} else {
$product['wholesale_price'] = $this->l('N/A'); // Fallback if ID is missing
}
}
}
}
代码解释:
- __construct(): 模块的基本信息,如名称、作者、描述等。
- install(): 在模块安装时调用。这里我们注册了actionAdminProductsListingFieldsModifier钩子。
- uninstall(): 在模块卸载时调用。
-
hookActionAdminProductsListingFieldsModifier(array &$params): 这是核心函数。
- $params[‘fields’][‘wholesale_price’] = […]: 定义了名为wholesale_price的新列。title是列头显示文本,type指定了数据的显示类型(price会自动进行货币格式化),orderby允许该列排序。
- foreach ($params[‘list’] as &$product): 遍历当前页面显示的所有产品。
- $productObj = new Product($product[‘id_product’], false, $this->context->language->id);: 根据产品ID加载完整的Product对象。这是获取wholesale_price的关键,因为列表数据通常是精简的。
- $product[‘wholesale_price’] = Tools::displayPrice($productObj->wholesale_price, $this->context->currency);: 将获取到的批发价添加到当前产品的数组中。Tools::displayPrice用于将数值格式化为带货币符号的价格字符串。
步骤3:安装并激活模块
- 将mywholesale文件夹上传到PrestaShop根目录下的modules文件夹。
- 登录PrestaShop后台,导航到“模块”->“模块管理器”。
- 在模块列表中搜索“Wholesale Price Column”(或你设置的displayName)。
- 点击“安装”按钮安装并激活该模块。
步骤4:验证效果
安装并激活模块后,导航到“目录”->“产品”,您应该能在产品列表的表格中看到新添加的“批发价”列,并显示正确的数据。
3. 注意事项与最佳实践
- 模块化开发:始终通过自定义模块进行功能扩展,避免直接修改核心文件,以确保系统升级的兼容性。
- 性能考量:在hookActionAdminProductsListingFieldsModifier中加载完整的Product对象可能会对性能产生轻微影响,尤其是在产品数量非常庞大时。对于极大规模的列表,可能需要考虑更优化的数据库查询方式,但对于一般情况,这种方法是可接受且简便的。
- 数据格式化:如果列的type设置为price,建议使用Tools::displayPrice()进行格式化,以确保价格显示符合商店的货币设置。
- 参考示例:如果遇到困难,可以参考GitHub上的一些示例模块,例如FuenRob提供的addcolumninlist模块,它展示了类似的功能实现。
4. 总结
通过利用PrestaShop的actionAdminProductsListingFieldsModifier钩子,我们可以专业且安全地在后台产品目录中添加“批发价”列。这种方法不仅解决了数据不显示的问题,还遵循了PrestaShop的开发最佳实践,确保了系统的可维护性和可升级性。希望本教程能帮助您成功定制PrestaShop后台功能。
以上就是在PrestaShop 1.7后台产品目录中添加批发价列的教程的详细内容,更多请关注php中文网其它相关文章!


