
PrestaShop 1.7的后台产品目录列表默认不显示产品的批发价(wholesale_price)。许多用户尝试通过直接编辑主题或模块的Twig模板文件(例如products_table.html.twig和list.html.twig)来添加这一列。尽管可以在模板中添加列的标题和数据占位符,但通常会发现{{ product.wholesale_price }}变量显示为“N/A”或空值,这表明产品数据对象在传递给模板时并未包含批发价信息。直接修改核心文件不仅容易在系统升级时被覆盖,也无法解决数据未加载的根本问题。
在PrestaShop中,最佳实践是利用其强大的钩子(Hooks)系统进行功能扩展。对于修改后台列表的字段,actionAdminProductsListingFieldsModifier钩子是专门为此目的设计的。这个钩子允许开发者在产品列表数据被渲染之前,动态地添加、修改或删除列表中的字段定义和对应的数据。
actionAdminProductsListingFieldsModifier钩子会在PrestaShop后台产品列表加载时触发。它会接收一个包含当前列表字段定义($params['fields'])和产品数据数组($params['list'])的参数。通过在这个钩子的回调函数中操作这些参数,我们可以实现:
为了实现这一功能,我们需要创建一个简单的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
}
}
}
}
代码解释:
步骤3:安装并激活模块
步骤4:验证效果
安装并激活模块后,导航到“目录”->“产品”,您应该能在产品列表的表格中看到新添加的“批发价”列,并显示正确的数据。
通过利用PrestaShop的actionAdminProductsListingFieldsModifier钩子,我们可以专业且安全地在后台产品目录中添加“批发价”列。这种方法不仅解决了数据不显示的问题,还遵循了PrestaShop的开发最佳实践,确保了系统的可维护性和可升级性。希望本教程能帮助您成功定制PrestaShop后台功能。
以上就是在PrestaShop 1.7后台产品目录中添加批发价列的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号