Jump to content

[Résolu] Ajouter un champ de description des valeurs de caractéristique


Recommended Posts

Bonjour,

 

Je cherche à ajouter un nouveau champ de description pour les valeurs de caractéristiques de mes produits.

 

J'ai réussi l'ajout en backoffice mais impossible d'afficher mon nouveau champ en front office.

 

Voici ce que j'ai fait pour l'instant :

 

1/ Création d'un nouveau champ 'value_description' dans la table 'feature_value_lang' (en m'inspirant du champ existant 'value')

 

2/ Override de la classe FeatureValue.php avec ce code :

<?php

class FeatureValue extends FeatureValueCore
{
	public $value_description;
	
	public static $definition = array(
		'table' => 'feature_value',
		'primary' => 'id_feature_value',
		'multilang' => true,
		'fields' => array(
			'id_feature' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'custom' => 	array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),

			// Lang fields
			'value' => 		array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 255),
			'value_description' => 		array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString', 'size' => 255),
		),
	);
	
}

3/ Override du controller AdminFeaturesController.php avec ce code :

<?php

class AdminFeaturesController extends AdminFeaturesControllerCore
{	

	public function renderView()
	{
		if (($id = Tools::getValue('id_feature')))
		{
			$this->setTypeValue();
			$this->list_id = 'feature_value';
			$this->lang = true;

			// Action for list
			$this->addRowAction('edit');
			$this->addRowAction('delete');

			if (!Validate::isLoadedObject($obj = new Feature((int)$id)))
			{
				$this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
				return;
			}

			$this->feature_name = $obj->name;
			$this->toolbar_title = $this->feature_name[$this->context->employee->id_lang];
			$this->fields_list = array(
				'id_feature_value' => array(
					'title' => $this->l('ID'),
					'align' => 'center',
					'class' => 'fixed-width-xs'
				),
				'value' => array(
					'title' => $this->l('Value')
				),
				'value_description' => array(
					'title' => $this->l('Description')
				)
			);

			$this->_where = sprintf('AND `id_feature` = %d', (int)$id);
			self::$currentIndex = self::$currentIndex.'&id_feature='.(int)$id.'&viewfeature';
			$this->processFilter();
			return AdminController::renderList();
		}
	}
	
	public function initFormFeatureValue()
	{
		$this->setTypeValue();

		$this->fields_form[0]['form'] = array(
			'legend' => array(
				'title' => $this->l('Feature value'),
				'icon' => 'icon-info-sign'
			),
			'input' => array(
				array(
					'type' => 'select',
					'label' => $this->l('Feature'),
					'name' => 'id_feature',
					'options' => array(
						'query' => Feature::getFeatures($this->context->language->id),
						'id' => 'id_feature',
						'name' => 'name'
					),
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Value'),
					'name' => 'value',
					'lang' => true,
					'size' => 33,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Description'),
					'name' => 'value_description',
					'lang' => true,
					'autoload_rte' => true,
					'size' => 33,
					'hint' => $this->l('Invalid characters:').'',
				),
			),
			'submit' => array(
				'title' => $this->l('Save'),
			),
			'buttons' => array(
				'save-and-stay' => array(
					'title' => $this->l('Save then add another value'),
					'name' => 'submitAdd'.$this->table.'AndStay',
					'type' => 'submit',
					'class' => 'btn btn-default pull-right',
					'icon' => 'process-icon-save'
				)
			)
		);

		$this->fields_value['id_feature'] = (int)Tools::getValue('id_feature');

		// Create Object FeatureValue
		$feature_value = new FeatureValue(Tools::getValue('id_feature_value'));

		$this->tpl_vars = array(
			'feature_value' => $feature_value,
		);

		$this->getlanguages();
		$helper = new HelperForm();
		$helper->show_cancel_button = true;

		$back = Tools::safeOutput(Tools::getValue('back', ''));
		if (empty($back))
			$back = self::$currentIndex.'&token='.$this->token;
		if (!Validate::isCleanHtml($back))
			die(Tools::displayError());

		$helper->back_url = $back;
		$helper->currentIndex = self::$currentIndex;
		$helper->token = $this->token;
		$helper->table = $this->table;
		$helper->identifier = $this->identifier;
		$helper->override_folder = 'feature_value/';
		$helper->id = $feature_value->id;
		$helper->toolbar_scroll = false;
		$helper->tpl_vars = $this->tpl_vars;
		$helper->languages = $this->_languages;
		$helper->default_form_language = $this->default_form_language;
		$helper->allow_employee_form_lang = $this->allow_employee_form_lang;
		$helper->fields_value = $this->getFieldsValue($feature_value);
		$helper->toolbar_btn = $this->toolbar_btn;
		$helper->title = $this->l('Add a new feature value');
		$this->content .= $helper->generateForm($this->fields_form);
	}
	
	
	
}

4/ Pour afficher le contenu du champ sur la fiche produit je me suis inspirée de l'affichage du champ 'value' en ajoutant  {$feature.value_description|escape:'html':'UTF-8'} dans le product.tpl de mon thème... Mais rien n'y fait, le champ ne s'affiche pas...

 

 

Sauriez-vous ce que j'ai oublié de faire ou que j'ai mal fait ?

 

Merci d'avance pour les pistes ou solutions que vous pourrez m'apporter !

 

 

Edited by Christinelle (see edit history)
Link to comment
Share on other sites

J'ai trouvé !

 

Il manquait un override de la classe Product.php !

 

Donc après l'étape 3 de mon premier post il faut faire :

 

4/ Override de la classe Product.php :

 

Il s'agit d'overrider la fonction getFrontFeaturesStatic de la façon suivante :

public static function getFrontFeaturesStatic($id_lang, $id_product)
		{
			if (!Feature::isFeatureActive())
				return array();
			if (!array_key_exists($id_product.'-'.$id_lang, self::$_frontFeaturesCache))
			{
				self::$_frontFeaturesCache[$id_product.'-'.$id_lang] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
					SELECT name, value, value_description, pf.id_feature   /* j'ai ajouté ma 'value_description' juste après 'value' */
					FROM '._DB_PREFIX_.'feature_product pf
					LEFT JOIN '._DB_PREFIX_.'feature_lang fl ON (fl.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')
					LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = pf.id_feature_value AND fvl.id_lang = '.(int)$id_lang.')
					LEFT JOIN '._DB_PREFIX_.'feature f ON (f.id_feature = pf.id_feature AND fl.id_lang = '.(int)$id_lang.')
					'.Shop::addSqlAssociation('feature', 'f').'
					WHERE pf.id_product = '.(int)$id_product.'
					ORDER BY f.position ASC'
				);
			}
			return self::$_frontFeaturesCache[$id_product.'-'.$id_lang];
		}

5/ Et enfin pour afficher le contenu en front office il faut bien utiiser {$feature.value_description|escape:'html':'UTF-8'} dans le fichier de template.

 

Et voilà !

Soulagée d'y être enfin arrivée ! Et comme une grande en plus  :-) !

  • Like 1
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...