Jump to content

[Module] Ajout de champs personnalisé sur la fiche produit


Recommended Posts

Bonjour à tous, n'ayant pas fait de développement sur prestashop depuis tellement longtemps que je suis quelque peu désorienté ^^

J'ai donc besoin de faire un module pour ajouter un champs de type checkbox pour activer / desactiver une options, j'arrive à l'afficher en back mais pour autant je n'arrive pas à l'enregistrer. Voici mon code :

Fichier modules :

<?php
/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/AFL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop.com/ for more information.
 *
 * @author    PrestaShop SA and Contributors <[email protected]>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

class Yoozly_Productquote extends Module
{

    public function __construct()
    {
        $this->name = 'Yoozly_Productquote';
        $this->tab = 'product';
        $this->version = '1.0.0';
        $this->author = 'Guillaume DELFORGE';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
        'min' => '1.6',
        'max' => _PS_VERSION_
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Customize Admin Product');
        $this->description = $this->l('Allows us to add others fields to product');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall ?');
    }

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

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

    protected function _installSql() {
        $sqlInstall = "ALTER TABLE " . _DB_PREFIX_ . "product "
                . "ADD quote TINYINT(1) UNSIGNED NOT NULL DEFAULT '0'";
 
        $returnSql = Db::getInstance()->execute($sqlInstall);
 
        return $returnSql;
    }

    protected function _unInstallSql() {
        $sqlInstall = "ALTER TABLE " . _DB_PREFIX_ . "product "
                 . "DROP quote";
  
         $returnSql = Db::getInstance()->execute($sqlInstall);
  
         return $returnSql;
    }

    public function hookDisplayAdminProductsExtra($params)
    {
 
    }

    /**
     * Affichage des informations supplémentaires sur la fiche produit
     * @param type $params
     * @return type
     */
    public function hookDisplayAdminProductsMainStepLeftColumnMiddle($params) {
        $product = new Product($params['id_product']);
        $languages = Language::getLanguages(false);
        $this->context->smarty->assign(array(
            'quote' => $product->quote,
            'languages' => $languages,
            'default_language' => $this->context->employee->id_lang,
            )
           );
        return $this->display(__FILE__, 'views/templates/hook/customfield.tpl');
    }

}

Fichier override de la classe product (monModule/override/classes) :

 

<?php

class Product extends ProductCore {
 
    public $quote;
 
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, \Context $context = null) {
        //Définition des nouveaux champs
        self::$definition['fields']['quote'] = [
            'type' => self::TYPE_BOOL,
            'required' => false
        ];
        parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
}

 

En enfin mon template pour l'ajout du champs en BO (monModule/views/templates/hook) :

 

<div class="m-b-1 m-t-1">
    <h2>Mon champ personnalisé</h2>
 
    <fieldset class="form-group">
        <div class="col-md-8">
            <div class="widget-checkbox-inline">
                <div class="checkbox">
                    <label class="form-control-label">
                    <input type="checkbox" id="quote" name="quote" value="{$quote}"/>
                    {l s='Activer la demande de devis' mod='hhproduct'}
                    </label>
                </div>
            </div>
        </div>
    </fieldset>
 
<div class="clearfix"></div>
</div>

 

Donc l'installation ce déroule avec succés, mon champs de type checkbox est bien présent, mais si j'enregistre ma fiche produit, celui ci n'est pas enregistré en base de donnée il reste toujours à 0.

Pouvez vous m'expliquer ce qui me manque ou ce qui ne va pas ?

Merci à tous.

Link to comment
Share on other sites

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