Jump to content

Add custom field in the AdminProduct page for PrestaShop 1.7.8.10


Recommended Posts

Hi,

 

I know that this subject was posted before, but I can not manage to fix it or, at least, I'm experiencing a different result where the value is properly stored into the database. Let me explain myself:

 

I have a custom module to add a custom field for Products. This is the code of the hooks I'm managing in the main PHP class of my module:

    public function hookDisplayAdminProductsMainStepLeftColumnMiddle($params) 
    {
        $product = new Product($params['id_product']);
        $this->context->smarty->assign(array(
            'siac' => (int)$product->siac,
        ));

        $tpl = $this->context->smarty->createTemplate(
            dirname(__FILE__).'/views/templates/hook/ocgproductfields.tpl'
        );
        return $tpl->fetch();
    }

    public function hookActionAdminProductsControllerSaveBefore($params)
    {
        $productAdapter = $this->get('prestashop.adapter.data_provider.product');
        $product = $productAdapter->getProduct($_REQUEST['form']['id_product']);
    
        $new_siac = isset($_POST['siac']) && $_POST['siac'] == 'on' ? true : false;
        $product->siac = $new_siac;

        $product->save();

        return true;
    }

 

I've included the following code into the Product.php override class:

	public $siac;
	  
	public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null) 
    {
	 
        self::$definition['fields']['siac'] = [
            'type' => self::TYPE_BOOL, 
            'validate' => 'isBool', 
            'required' => false
        ];

		parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
	        
	}

 

And obviously, the database has the siac field and I can see the field in the AdminProduct page and even see the current value (checkbox is checked for true, not checked for false). However, If I try to set the field to false (unchecked), the product is saved properly, and I can see the successful message, but If It set the field to true (checked), the value is properly stored into the database without any problem, but PrestaShop throws an error saying "The field siac is not valid".

 

This is the tpl content:

<h2>{l s='Campos personalizados' mod='ocgproductfields'}</h2>
<div class="row">
    <div class="col-md-3">
        <fieldset class="form-group">
            <label class="form-control-label" for="siac">{l s='SIAC' mod='ocgproductfields'}</label>
            <input type="checkbox" id="siac" name="siac" class="form-control" {if isset($siac) && $siac != '' && $siac == 1}checked="checked"{/if}/>
        </fieldset>
    </div>
</div>
<div class="clearfix"></div>

 

So, what am I doing wrong if everything is working except for that error message? Is it a wrong validate function? Or maybe the declaration of the siac field in the database? By the way, it's a tinyint(1) unsigned (could be) NULL and 0 as default.

 

Does anyone know to fix it? Thank you so much in advance!

 

Edited by Oliver CG
Added tpl content. (see edit history)
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...