Jump to content

Ajout champ personnalisé déclinaison


Recommended Posts

Bonjour à tous,

Je suis en train d'essayer d'ajouter des champs personnalisé sur mes déclinaisons pour obtenir des meta title réécris à la main.

Mon module:

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('Ajouter des champs supplémentaires aux combinaisons');
        $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();
    }

    /**
     * Modifications sql du module
     * @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;
    }

    /**
     * Suppression des modification sql du module
     * @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');

    }
}

 

Mon html pour l'ajout des champs au BO (views/templates/hook/combinationfields.tpl):

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

 

Mon fichier d'override de Combination:

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

}

 

 

Lorsque je sauve le produit, les champs sont bien présents dans la requête:

image.png.57c73da1d400ca772480b14b298fdd50.png

Mais j'ai une erreur 400:

image.png.32b1bcbf1e41db2b08f2b0808d6c687b.png
 

Auriez-vous une idée ?

 

Merci à tous pour votre aide :) 

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...