Prestashop - 在产品组合上添加其他字段

时间:2021-07-22 09:12:18

标签: prestashop prestashop-1.7

我正在尝试在我的产品组合中添加其他字段,以便为组合提供自定义元标题。 所以我创建了这个模块:

我的模块定义:

class CombinationCustomFields extends Module
{

    public function __construct()
    {
        $this->name = 'combinationcustomfields';
        $this->tab = 'administration';
        $this->author = '';
        $this->version = '1.0';
        $this->need_instance = 0;
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Combination Custom Fields');
        $this->description = $this->l('Add additional fields on combinations');
        $this->ps_versions_compliancy = array('min' => '1.7.1', 'max' => _PS_VERSION_);
    }

    public function install()
    {
        if (!parent::install() || !$this->_installSql()
            || !$this->registerHook('displayAdminProductsCombinationBottom')
            || !$this->registerHook('actionAttributeCombinationSave')
        ) {
            return false;
        }

        return true;
    }

    public function uninstall()
    {
        return parent::uninstall() && $this->_unInstallSql();
    }

    /**
     * Add database fields
     * @return boolean
     */
    protected function _installSql()
    {
        $sqlInstall = "ALTER TABLE " . _DB_PREFIX_ . "product_attribute "
            . "ADD meta_title_fr VARCHAR(255) NULL, "
            . "ADD meta_title_en VARCHAR(255) NULL;";

        $returnSql = Db::getInstance()->execute($sqlInstall);

        return $returnSql;
    }

    /**
     * Remove database fields
     * @return boolean
     */
    protected function _unInstallSql()
    {
        $sqlInstall = "ALTER TABLE " . _DB_PREFIX_ . "product_attribute "
            . "DROP meta_title_fr, "
            . "DROP meta_title_en";

        $returnSql = Db::getInstance()->execute($sqlInstall);

        return $returnSql;
    }

    public function hookDisplayAdminProductsCombinationBottom($params)
    {
        $combination = new Combination($params['id_product_attribute']);
        $this->context->smarty->assign(array(
                'id_product_attribute' => $params['id_product_attribute'],
                'meta_title_fr' => $combination->meta_title_fr,
                'meta_title_en' => $combination->meta_title_en,
            )
        );


        return $this->display(__FILE__, 'views/templates/hook/combinationfields.tpl');
    }


    public function hookActionAttributeCombinationSave($params)
    {
        $combination = new Combination($params['id_product_attribute']);
        $this->context->smarty->assign(array(
                'id_product_attribute' => $params['id_product_attribute'],
                'meta_title_fr' => $combination->meta_title_fr,
                'meta_title_en' => $combination->meta_title_en,
            )
        );

        $combination->meta_title_fr = Tools::getValue('meta_title_fr');
        $combination->meta_title_en = Tools::getValue('meta_title_en');

        $combination->save();

        return $this->display(__FILE__, 'views/templates/hook/combinationfields.tpl');

    }
}

我的管理挂钩 HTML:

<div class="m-b-1 m-t-1">
    <h2>{l s='Combination Custom Fields' mod='combinationcustomfields'}</h2>
    <fieldset class="form-group">
        <div class="col-lg-12 col-xl-4">
            <label class="form-control-label" for="combination_{$id_product_attribute}_attribute_meta_title_fr">{l s='Meta title FR' mod='combinationcustomfields'}</label>
            <input class="form-control" name="combination_{$id_product_attribute}[attribute_meta_title_fr]" id="combination_{$id_product_attribute}_attribute_meta_title_fr" type="text" value="{if $meta_title_fr != ''}{$meta_title_fr}{/if}">
            <br />
        </div>
        <div class="col-lg-12 col-xl-4">
            <label class="form-control-label" for="combination_{$id_product_attribute}_attribute_meta_title_en">{l s='Meta title EN' mod='cmp_combination_customfields'}</label>
            <input class="form-control" name="combination_{$id_product_attribute}[attribute_meta_title_en]" id="combination_{$id_product_attribute}_attribute_meta_title_en" type="text" value="{if $meta_title_en != ''}{$meta_title_en}{/if}">
            <br />
        </div>
    </fieldset>
    <div class="clearfix"></div>
</div>

我对组合类的覆盖:

class Combination extends CombinationCore
{
    public $meta_title_fr;
    public $meta_title_en;
    /**
     * @see ObjectModel::$definition
     */
    public static $definition = [
        'table' => 'product_attribute',
        'primary' => 'id_product_attribute',
        'fields' => [
            'id_product' => ['type' => self::TYPE_INT, 'shop' => 'both', 'validate' => 'isUnsignedId', 'required' => true],
            'location' => ['type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 64],
            'ean13' => ['type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13],
            'isbn' => ['type' => self::TYPE_STRING, 'validate' => 'isIsbn', 'size' => 32],
            'upc' => ['type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12],
            'mpn' => ['type' => self::TYPE_STRING, 'validate' => 'isMpn', 'size' => 40],
            'quantity' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'size' => 10],
            'reference' => ['type' => self::TYPE_STRING, 'size' => 64],
            'supplier_reference' => ['type' => self::TYPE_STRING, 'size' => 64],
            'meta_title_fr' => ['type' => self::TYPE_STRING, 'size' => 64],
            'meta_title_en' => ['type' => self::TYPE_STRING, 'size' => 64],

            /* Shop fields */
            'wholesale_price' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'size' => 27],
            'price' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isNegativePrice', 'size' => 20],
            'ecotax' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'size' => 20],
            'weight' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isFloat'],
            'unit_price_impact' => ['type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isNegativePrice', 'size' => 20],
            'minimal_quantity' => ['type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId', 'required' => true],
            'low_stock_threshold' => ['type' => self::TYPE_INT, 'shop' => true, 'allow_null' => true, 'validate' => 'isInt'],
            'low_stock_alert' => ['type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool'],
            'default_on' => ['type' => self::TYPE_BOOL, 'allow_null' => true, 'shop' => true, 'validate' => 'isBool'],
            'available_date' => ['type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'],
        ],
    ];

}

但是当我保存产品时,字段在请求中:

Fields

但我有一个 400 错误:

Error

知道我的错误吗?

祝您一切顺利,提前感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

您可以使用覆盖控制器覆盖组合类

class Combination extends CombinationCore
{
    public $newVariableName;

    public function __construct($id = null, $id_lang = null, $id_shop = null)
    {
        Combination::$definition['fields']['newVariableName'] = array('type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required' => false);
        parent::__construct($id, $id_lang, $id_shop);
    }
}

您还需要覆盖 product.php

public function updateProductAttribute($id_product_attribute, ..., $newVariableName = null){

public function addCombinationEntity($wholesale_price, ..., $newVariableName = null){

等等...