Override AdminProductsController

强颜欢笑 提交于 2021-01-29 14:43:35

问题


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.

My module:

<?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'); 
    }
}

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
        );
    }
}

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


回答1:


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.

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.

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

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

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



来源:https://stackoverflow.com/questions/58209417/override-adminproductscontroller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!