Prestashop免费的产品自定义字段模块

实现产品自定义字段功能的模块都是收费的,这是唯一的免费可用的基本框架,需要自行开发

项目地址:https://github.com/PululuK/democustomfields17/tree/main

下面是使用它需要修改的地方

1.引入bundle错误导致不出现表单

定位到文件:\modules\democustomfields17\views\PrestaShop\democustomfields17\admin\product\customfields.html.twig,由于默认/app/config/configyaml中的参数“form_themes”默认导入了该文件,直接移除即可

{% use "PrestaShopBundle:Admin/TwigTemplateForm:bootstrap_4_horizontal_layout.html.twig" %}

或者

{% form_theme form '@PrestaShop/Admin/TwigTemplateForm/bootstrap_4_horizontal_layout.html.twig' %}

2.表单数据构建

打开文件: \modules\democustomfields17\src\Form\Product\Hooks\HookDisplayAdminProductsExtraFieldsBuilder.php

修改

$adminFormBuilder
                ->add('demo_url', TextType::class, [
                    'label' => $module->l('Demo URL'),
                    'attr' => [
                        'class' => 'my-custom-class',
                        'data-hex'=> 'true'
                    ]
                ])
                ->add('demo_text', TranslatableType::class, [  //多语言字段
                    'label' => $module->l('Try the demo'),
                    'type' => TextType::class,
                    'locales' => $module->getLocales()  
                ])                
                ->add('demo_switch', SwitchType::class, [
                    'label' => $module->l('Enable?')
                ]);

修改Entity,打开\modules\democustomfields17\src\Model\ProductCustomFields.php

    /** @var int ID */
    public $id;

    /** @var int product ID */
    public $id_product;

    /** @var string  */
    public $demo_url;

    /** @var string  */
    public $demo_text;

    /** @var bool  */
    public $demo_switch;

    /** @var string Object creation date */
    public $date_add;

    /** @var string Object last modification date */
    public $date_upd;


    public static $definition = [
        'table' => 'democustomfields17',
        'primary' => 'id_democustomfields17',
        'multilang' => true,
        'multilang_shop' => true,
        'fields' => [
            'id_product' => [
                'type' => self::TYPE_INT,
                'validate' => 'isUnsignedId'
            ],
            'demo_url' => [
                'type' => self::TYPE_HTML,
                'validate' => 'isCleanHtml'
            ],
            'demo_text' => [
                'type' => self::TYPE_HTML,
                'lang' => true,  #多语言字段需要打开此项
                'shop' => true,
                'validate' => 'isCleanHtml'
            ],
            'demo_switch' => [
                'type' => self::TYPE_BOOL
            ],
            'date_add' => [
                'type' => self::TYPE_DATE,
                'validate' => 'isDate'
            ],
            'date_upd' => [
                'type' => self::TYPE_DATE,
                'validate' => 'isDate'
            ],
        ],
    ];

3.构建安装SQL脚本

修改文件\modules\democustomfields17\sql\install.php,注意:多语言字段存储在表 democustomfields17_lang

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'democustomfields17` (
    `id_democustomfields17` int(11) NOT NULL AUTO_INCREMENT,
    `id_product` int(11) unsigned NOT NULL,
    `demo_switch` tinyint(1) unsigned NOT NULL DEFAULT "0",
    `demo_url` text DEFAULT NULL,
    `date_add` datetime NOT NULL,
    `date_upd` datetime NOT NULL,
    PRIMARY KEY  (`id_democustomfields17`),
    KEY `id_product` (`id_product`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'democustomfields17_lang` (
    `id_democustomfields17` int(11) unsigned NOT NULL,
    `id_shop` INT(11) UNSIGNED NOT NULL DEFAULT "1",
    `id_lang` int(11) unsigned NOT NULL ,
    `demo_text` text DEFAULT NULL,
    PRIMARY KEY  (`id_democustomfields17`, `id_shop`, `id_lang`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

4.处理数据

文件:\modules\democustomfields17\src\Form\Product\FormDataHandlerInterface.php

        $idProduct = (int) $data['id_product'];
        $productCustomFields = ProductCustomFieldsFactory::create($idProduct);
        $productCustomFields->id_product = $idProduct;
        $productCustomFields->demo_switch = (bool) $data['demo_switch'];
        $productCustomFields->demo_url = $data['demo_url'];
        $productCustomFields->demo_text = $data['demo_text'];

        return [
            'id' => $productCustomFields->id,
            'id_product' => $productCustomFields->id_product,
            'demo_switch' => (bool) $productCustomFields->demo_switch,
            'demo_url' => $productCustomFields->demo_url,
            'demo_text' => $productCustomFields->demo_text,
        ];

最后想说prestashop的问题真的很多,要通关并不容易,这种架构让问题非常棘手,抛开系统本身我用了很多第三方商业模块都有各种奇奇怪怪的问题,非常复杂且需要时间去解决,新手慎用,还是别踩这个坑了。

Post Comment