覆盖 AdminProductsController [英] Override AdminProductsController

查看:38
本文介绍了覆盖 AdminProductsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义 presta 商店模块有问题.我的模块向产品添加了新字段,字段名称是promotion,是一个字符串.如果我添加新产品或编辑现有产品没有问题,我会看到我的新字段.但是当我将此字段添加到产品列表时,我看不到他.

I have problem with my custom presta shop module. My module adds new field to product, field name is promotion, is a string. If i add a new product or edit existing i have no problem, i see my new field. But when i have add this field to products list then i don't see this him.

我的模块:

<?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

and AdminProductsController which i override

<?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 的课程,在他的视频上一切正常,但在我这里没有使用相同的代码.

What i do wrong? I have a nemo's course, on his video all works fine, but at me not working this same code.

推荐答案

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

I had the same issue on adminOrdersController, an i solved by passing a calback for the value to print try to edit your override by adding 'filter_key' and a '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 must be popolated by the alias of the table and with the name of the field that you want to show.

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

To know what you have to pass as string in filter_key you should check the executed query that show the products in backoffice.

进入回调函数,您可以在打印值之前对其进行管理或执行任何您想做的事情.

into callback function you can manage or do everything you want with the value before it'll be printed.

为了让 Prestashop 知道产品表中有一个新字段,您还必须覆盖/classes/product.php 中的 Product Core 类

To make Prestashop know that there ia a new fields into product table, you have also to override the Product Core class in /classes/product.php

在此文件中,您必须在第 293 行下方添加此 public $promotion;

in this file you have to add this public $promotion; just below row 293

然后你必须编辑产品表的public static $definition = array(),引入你的新字段的定义,所以你必须把这个放入定义数组

then you have to edit the public static $definition = array() of the product table introducing the definition of your new field, so you have to put into definition array this

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

现在您的字段应该在后台产品列表中可见

now your field should be visible in backoffice product list

这篇关于覆盖 AdminProductsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆