Jump to content

module Add custom fields


Recommended Posts

hi every body!

 

i'm tring to produce a module to add custom fields to specific product

but i have little problem.

when i had the module to hook : hookDisplayProductPriceBlock

it display it three time, do you know why?

 

here is my module.php

<?php
     
if(!defined('_PS_VERSION_'))
        exit;
class HelloWorld extends Module {
	
	public function __construct()
{
               $this->name = 'helloworld';
               $this->tab = 'front_office_features';
               $this->version = '1.0.0';
               $this->author = 'Muxui';
               $this->need_instance = 0;
               $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.7');
               $this->bootstrap = true;
               parent::__construct();
               $this->displayName = $this->l('Hello World');
               $this->description = $this->l('Affiche Hello World!');
               $this->confirmUninstall = $this->l('Are you crazy? it\'s the best module of the world');
               if (!Configuration::get('HELLOWORLD_NAME'))
                 $this->warning = $this->l('No name provided');
}
	public function getContent()
{
    $output = null;

    if (Tools::isSubmit('submit'.$this->name))
    {
        $my_module_name = strval(Tools::getValue('HELLOWORLD_NAME'));
		$comment = strval(Tools::getValue('HELLOWORLD_NAME1'));
        if (!$my_module_name
          || empty($my_module_name)
          || !Validate::isGenericName($my_module_name))
            $output .= $this->displayError($this->l('Invalid Configuration value'));
        else
        {
            Configuration::updateValue('HELLOWORLD_NAME', $my_module_name);
			Configuration::updateValue('HELLOWORLD_NAME1', $comment);
            $output .= $this->displayConfirmation($this->l('Settings updated'));
        }
    }
	
    return $output.$this->displayForm();
}
	
	public function install() {
         if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);

  return parent::install() &&
	  $this->registerHook('displayProductPriceBlock') &&
    Configuration::updateValue('HELLOWORLD_NAME', 'my friend');
		Configuration::updateValue('HELLOWORLD_NAME1', 'comment');
}
	public function uninstall() {
			if (!parent::uninstall())
					return false;
			return true;
	}
	
	public function displayForm()
{
    // Get default language
    $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

    // Init Fields form array
    $fields_form[0]['form'] = array(
        'legend' => array(
            'title' => $this->l('Settings'),
        ),
        'input' => array(
            array(
                'type' => 'text',
                'label' => $this->l('entrez votre tarif'),
                'name' => 'HELLOWORLD_NAME',
                'size' => 50,
                'required' => true
            ), 
		array(
                'type' => 'text',
                'label' => $this->l('entrez votre marque'),
                'name' => 'HELLOWORLD_NAME1',
                'size' => 60,
                'required' => true
          
        )
        ),
		
          
        'submit' => array(
            'title' => $this->l('Save'),
            'class' => 'btn btn-default pull-right'
        )
    );
	
	

    $helper = new HelperForm();

    // Module, token and currentIndex
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
	$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
	

    // Language
    $helper->default_form_language = $default_lang;
    $helper->allow_employee_form_lang = $default_lang;

    // Title and toolbar
    $helper->title = $this->displayName;
    $helper->show_toolbar = true;        // false -> remove toolbar
    $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
    $helper->submit_action = 'submit'.$this->name;
	
	
    $helper->toolbar_btn = array(
        'save' =>
        array(
            'desc' => $this->l('Save'),
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
            '&token='.Tools::getAdminTokenLite('AdminModules'),
        ),
        'back' => array(
            'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Back to list')
        )
    );

    // Load current value
    $helper->fields_value['HELLOWORLD_NAME'] = Configuration::get('HELLOWORLD_NAME');
	$helper->fields_value['HELLOWORLD_NAME1'] = Configuration::get('HELLOWORLD_NAME1');

    return $helper->generateForm($fields_form);
}
	


public function hookDisplayProductPriceBlock($params)
	{
	  $this->context->smarty->assign(
		  array(
			  'my_module_name' => Configuration::get('HELLOWORLD_NAME'),
		  		'comment' => Configuration::get('HELLOWORLD_NAME1'),
			  'my_module_link' => $this->context->link->getModuleLink('helloworld', 'display')
		  )
	  );
	  return $this->display(__FILE__, 'helloworld.tpl');
	}

public function hookDisplayProductBlock($params)
	{
		
	  return $this->hookDisplayProductPriceBlock($params);	  
		
	}



}

?>


and from my module .tpl


       <style>
div#mymodule_block_home p {
  font-size: 150%;
  font-style:italic;
}
		</style>



<!-- Block mymodule -->

  

  <p>
   
  fuck
           {$my_module_name}
           {$comment}

         

    
    </p>
 

<!-- Block mymodule -->
Link to comment
Share on other sites

Hello,

 

It is displayed in a file product.tpl and product-list.tpl three times with different type

 

Try as in my example:

    public function hookdisplayProductPriceBlock($params)
    {

        if ($params['type'] != 'old_price') {
            return;
        }
     $this->context->smarty->assign(
         array(
             'my_module_name' => Configuration::get('HELLOWORLD_NAME'),
                 'comment' => Configuration::get('HELLOWORLD_NAME1'),
             'my_module_link' => $this->context->link->getModuleLink('helloworld', 'display')
         )
     );
     return $this->display(__FILE__, 'helloworld.tpl');
    } 

hree times with different parameters

Edited by BonPresta (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...