在Prestashop 1.7的管理产品页面中添加新字段的正确方法

sar*_*nos 5 php prestashop prestashop-1.7

我已经为Prestashop 1.7创建了一个模块,以便在“管理产品”页面中添加新字段。由于缺少适当的文档,我想询问添加我的自定义字段(选择)的正确方法。我要保存并更新产品保存。

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

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');
}
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何覆盖Product.php类以拥有$ field以及如何为tpl创建form元素。

我想要创建的表单是这样的

<select name="" id="">
    {foreach from=$items_number item=option}
        <option value="{$option}">
            {$option}
        </option>
    {/foreach}
</select>
Run Code Online (Sandbox Code Playgroud)

很抱歉缺少信息,但是我发现创建模块的新方法非常令人困惑。提前致谢

Ben*_*che 1

要覆盖产品类:

在 override/classes/ 中创建文件“Product.php”

并添加以下内容:

//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
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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