
在prestashop 1.7后台的产品目录页面,默认情况下并没有显示产品的批发价(wholesale_price)列。许多开发者在尝试添加此功能时,可能会直观地尝试修改prestashop核心主题或模块的twig模板文件,例如products_table.html.twig和list.html.twig。
例如,像问题中提到的尝试:
<!-- 尝试在 products_table.html.twig 中添加列头 -->
<th scope="col" class="text-center" style="width: 9%">
{{ ps.sortable_column_header("Wholesale price"|trans({}, 'Admin.Catalog.Feature'), 'wholesale_price', orderBy, sortOrder) }}
</th>
<!-- 尝试在 list.html.twig 中添加列数据 -->
<td class="text-center">
<a href="{{ product.url|default('') }}#tab-step2">{{ product.wholesale_price|default('N/A'|trans({}, 'Admin.Global')) }}</a>
</td>这种方法通常会遇到以下问题:
因此,为了实现功能的可扩展性、稳定性和可维护性,我们应该采用PrestaShop提供的Hook(钩子)机制来添加自定义列。
PrestaShop的Hook机制是其核心扩展能力之一。它允许开发者在PrestaShop执行的特定时刻插入自定义代码。对于后台产品列表的修改,actionAdminProductsListingFieldsModifier Hook是最佳选择。
actionAdminProductsListingFieldsModifier Hook 的作用: 当PrestaShop准备渲染后台产品列表时,它会触发此Hook。这个Hook的目的是允许模块开发者修改产品列表的字段定义(即列的标题、是否可排序等)以及列表中的实际数据。
通过这个Hook,我们可以:
为了使用actionAdminProductsListingFieldsModifier Hook,我们需要创建一个PrestaShop自定义模块。
首先,在PrestaShop的modules目录下创建一个新文件夹,例如myproductwholesale。在该文件夹内,创建主模块文件myproductwholesale.php。
modules/
└── myproductwholesale/
└── myproductwholesale.php
└── config.xml (PrestaShop自动生成)<?php
/**
* 2007-2024 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 http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2024 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 MyProductWholesale extends Module
{
public function __construct()
{
$this->name = 'myproductwholesale';
$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',
'max' => _PS_VERSION_,
];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('My Product Wholesale Price Column');
$this->description = $this->l('Adds a wholesale price column to the product catalog in the back office.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall? All data will be lost.');
}
/**
* Module installation
*
* @return bool
*/
public function install()
{
return parent::install() &&
$this->registerHook('actionAdminProductsListingFieldsModifier');
}
/**
* Module uninstallation
*
* @return bool
*/
public function uninstall()
{
return parent::uninstall();
}
/**
* Hook to modify the product listing fields and data in the back office.
*
* @param array $params Contains 'list_fields' and 'list'
* @return void
*/
public function hookActionAdminProductsListingFieldsModifier(array $params)
{
// 确保 $params['list_fields'] 和 $params['list'] 存在且是数组
if (!isset($params['list_fields']) || !isset($params['list']) || !is_array($params['list_fields']) || !is_array($params['list'])) {
return;
}
// 1. 添加新的列定义到 $params['list_fields']
// 'wholesale_price' 是我们自定义的字段名
$params['list_fields']['wholesale_price'] = [
'title' => $this->l('Wholesale price'), // 列标题
'align' => 'text-center', // 对齐方式
'type' => 'price', // 数据类型,PrestaShop会根据此类型进行格式化
'currency' => true, // 是否显示货币符号
'orderby' => true, // 是否可排序
// 'filter' => true, // 如果需要过滤,可以启用
];
// 2. 遍历产品列表,为每个产品填充 'wholesale_price' 数据
foreach ($params['list'] as &$product_data) {
$id_product = (int) $product_data['id_product'];
// 实例化 Product 对象以获取批发价
$product = new Product($id_product, false, (int)Context::getContext()->language->id);
if (Validate::isLoadedObject($product)) {
$product_data['wholesale_price'] = $product->wholesale_price;
} else {
$product_data['wholesale_price'] = 0; // 或者 'N/A'
}
}
}
}
通过遵循PrestaShop的Hook机制,特别是actionAdminProductsListingFieldsModifier,我们可以优雅且专业地在后台产品目录中添加自定义列,如批发价。这种方法不仅解决了直接修改Twig模板无效的问题,还确保了代码的稳定性和可维护性,是PrestaShop二次开发的推荐方式。开发者应充分利用PrestaShop提供的丰富Hook,实现功能的灵活扩展。
以上就是PrestaShop 1.7 后台产品列表添加批发价列教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号