在 Prestashop 1.7 的管理产品页面中添加新字段的正确方法 [英] The right way to add a new field in admin product page in Prestashop 1.7

查看:34
本文介绍了在 Prestashop 1.7 的管理产品页面中添加新字段的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Prestashop 1.7 创建了一个模块,以便在管理产品页面中添加一个新字段.由于缺乏适当的文档,我想问一下添加自定义字段的正确方法,一个选择.我想在产品保存时得到保存和更新.

I've created a module for Prestashop 1.7 in order to add a new field in the Admin Product page. Due to the lack of proper documentation, I want to ask the proper way to add my custom field, a select. I want to get saved and updated on product save.

我使用下面的代码添加一个包含表单的 tpl

I use the code below to add a tpl containing a form

public function hookDisplayAdminProductsExtra($params) {
    $this->smarty;

    $available_items = $this->getAvailableBadges();
    $id_product  = Tools::getValue('id_product');

    $this->context->smarty->assign('items_number', $available_items);
    return $this->display(__FILE__, '/views/templates/admin/admin_products.tpl');
}

问题是我不知道如何覆盖 Product.php 类以获得我的 $field 以及如何为 tpl 创建表单元素.

The problem is that I don't know how to make to override the Product.php Class in order to have my $field and how to create the form element for the tpl.

我想创建的表单就像那样

The form I want to create I something like that

<select name="" id="">
    {foreach from=$items_number item=option}
        <option value="{$option}">
            {$option}
        </option>
    {/foreach}
</select>

很抱歉缺乏信息,但我发现创建模块的新方法非常混乱.提前致谢

Sorry for the lack of information, but I find the new way of creating modules very confusing. Thanks in advance

推荐答案

要覆盖 Product 类:

To override the Product class :

在 override/classes/中创建一个文件Product.php"

Create a file "Product.php" in override/classes/

并添加以下内容:

//override the class
class Product extends ProductCore {
    //add your attribute
    public $field_name;

    //override the construct function
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, \Context $context = null) {

        //add your custom field to the array $definitions['fields']
        self::$definition['fields']['field_name'] = [
            'type' => self::TYPE_STRING,
            'lang' => true, //if you have multiple languages on your site
            'required' => false,
            'size' => 255
        ];
    }
}

然后,在您的数据库中添加一个新条目,如果 lang 设置为 false,则在 ps_product 中添加field_name";如果 lang 设置为 true,则在 ps_product_lang 中添加.

Then, add a new entry in your database, "field_name" in ps_product if lang is set to false or in ps_product_lang if lang is set to true.

您有不同的类型:TYPE_STRING"、TYPE_HTML"、TYPE_BOOL"、TYPE_INT"……我不知道详尽的列表.根据您选择的类型,您必须在数据库中创建正确的列类型(VARCHAR"、TEXT"....)

You have different types : "TYPE_STRING", "TYPE_HTML", "TYPE_BOOL", "TYPE_INT", ... I don't know the exhaustive list. Depending on the type you choose you have to create the right column type in database ("VARCHAR", "TEXT", ....)

可以在论坛中找到一些很好的文档(stackoverflow,https://www.prestashop.com/forums/) 以及像这样的博客:https://www.h-hennes.fr/博客/

Some good documentation can be found in forum (stackoverflow, https://www.prestashop.com/forums/) but also on blog like this one : https://www.h-hennes.fr/blog/

这篇关于在 Prestashop 1.7 的管理产品页面中添加新字段的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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