覆盖 AdminProductsController

Override AdminProductsController

我的自定义法式面包店模块有问题。我的模块给product增加了新的字段,字段名是promotion,是一个字符串。如果我添加新产品或编辑现有产品我没有问题,我会看到我的新字段。但是当我将此字段添加到产品列表时,我看不到他。

我的模块:

<?php

if (!defined('_PS_VERSION_'))
    exit;

class OverrideTraining extends Module
{
    private $_html = '';

    public function __construct()
    {
        $this->name = 'overridetraining';
        $this->tab = 'front_office_features';
        $this->version = '1.0';
        $this->author = 'Pawel Cyrklaf';
        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.7.9.9');

        $this->need_instance = 0;
        $this->bootstrap = true;

        $this->displayName = $this->l('Override Training');
        $this->description = $this->l('Task to learn override');
        parent::__construct();
    }

    public function install()
    {
        if (!parent::install() OR
            !$this->alterProductTable() OR
            !$this->registerHook('displayAdminProductsExtra'))
            return false;
        return true;
    }

    public function uninstall()
    {
        if (!parent::uninstall() OR
            !$this->alterProductTable('remove'))
            return false;
        return true;
    }

    /**
     * @param string $method
     * @return bool
     */
    public function alterProductTable($method = 'add')
    {
        if ($method == 'add')
            $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product ADD `promotion` VARCHAR (255) NOT NULL';
        else
            $sql = 'ALTER TABLE ' . _DB_PREFIX_ . 'product DROP COLUMN `promotion`';

        if (!Db::getInstance()->Execute($sql))
            return false;
        return true;
    }

    public function hookDisplayAdminProductsExtra($params)
    {
        $promotion = Db::getInstance()->getValue('SELECT promotion FROM ' . _DB_PREFIX_ . 'product WHERE id_product = ' . (int)Tools::getValue('id_product'));
        $this->context->smarty->assign('promotion', $promotion);
        return $this->display(__FILE__, 'adminProductsExtra.tpl'); 
    }
}

和我覆盖的 AdminProductsController

<?php

class AdminProductsController extends AdminProductsControllerCore
{
    public function __construct()
    {
        parent::__construct();
        $this->fields_list['promotion'] = array(
            'title' => $this->l('Promotion'),
            'align' => 'text-center',
            'class' => 'fixed-width-sm',
            'orderby' => false
        );
    }
}

我做错了什么?我有一个 nemo 的课程,在他的视频上一切正常,但在我身上却没有使用同样的代码。

我在 adminOrdersController 上遇到了同样的问题,我通过为要打印的值传递一个回调来解决尝试通过添加 'filter_key' 和 'calback'

来编辑您的覆盖
public function __construct()
    {
        parent::__construct();

        $this->fields_list['promotion'] = array(
            'title' => $this->l('Promotion'),
            'align' => 'text-center',
            'class' => 'fixed-width-sm',
            'filter_key' => 'a!promotion', // syntax: table_alias!field_name
            'callback' => 'displayPromotion'
        );
    }

    public function displayPromotion($value) {
        return $value ;   
    } 

筛选键必须由 table 的别名和您要显示的字段名称填充。

要了解您必须在 filter_key 中作为字符串传递的内容,您应该检查在后台显示产品的已执行查询。

在回调函数中,您可以在值被打印之前管理或执行您想要的任何操作。

要让 Prestashop 知道产品 table 中有一个新字段,您还必须覆盖 /classes/product 中的产品核心 class。php

在此文件中,您必须在第 293 行

下方添加 public $promotion;

然后您必须编辑产品 table 的 the public static $definition = array() 以引入新字段的定义,因此您必须将此

放入定义数组中
'promotion' => array(
                'type' => self::TYPE_STRING,
                'lang' => true, // or false if don't need to translate this field
                'validate' => 'isCleanHtml',
                'size' => 255
            ),

现在您的领域应该在后台产品列表中可见