微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Prestashop - 在产品组合上添加其他字段

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

我的模块定义:

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'],)
        );

        $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,'supplier_reference' => ['type' => self::TYPE_STRING,'Meta_title_fr' => ['type' => self::TYPE_STRING,'Meta_title_en' => ['type' => self::TYPE_STRING,/* Shop fields */
            'wholesale_price' => ['type' => self::TYPE_FLOAT,'shop' => true,'validate' => 'isPrice','size' => 27],'price' => ['type' => self::TYPE_FLOAT,'validate' => 'isNegativePrice','size' => 20],'ecotax' => ['type' => self::TYPE_FLOAT,'weight' => ['type' => self::TYPE_FLOAT,'validate' => 'isFloat'],'unit_price_impact' => ['type' => self::TYPE_FLOAT,'minimal_quantity' => ['type' => self::TYPE_INT,'low_stock_threshold' => ['type' => self::TYPE_INT,'allow_null' => true,'validate' => 'isInt'],'low_stock_alert' => ['type' => self::TYPE_BOOL,'validate' => 'isBool'],'default_on' => ['type' => self::TYPE_BOOL,'available_date' => ['type' => self::TYPE_DATE,'validate' => 'isDateFormat'],],];

}

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

Fields

我有一个 400 错误

Error

知道我的错误吗?

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

解决方法

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

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){

等等...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。