Override AdminProductsController

问题描述 投票:0回答:1

我的自定义presta商店模块有问题。我的模块向产品添加了新字段,字段名称是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的课程,在他的视频上一切正常,但对我来说,不能使用相同的代码。

prestashop
1个回答
0
投票

我在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 ;   
    } 

过滤器键必须由表的别名以及要显示的字段的名称填充。

要知道您必须在filter_key中作为字符串传递什么,您应该检查在后台显示产品的已执行查询。

进入到回调函数中,您可以管理该值,或在打印该值之前执行所需的所有操作

© www.soinside.com 2019 - 2024. All rights reserved.