Jump to content

[SOLVED] How to Add extra HTML text beside each Attribute's value ?


gigamax
 Share

Recommended Posts

hello every one . i'm new in prestashop and i dont know how to do this. i'll be so thankful for your advises. 

 

here is the plot : 

i have some simple attributes that show me different size of my product. 

i just want to add extra HTML text beside each value . this extra field will fill with description with deffrenet css style.  for example i have 3 volume of my product :

 

o  25ml bottle

o  50ml bottle

o  100ml bottle

 

and i want to show the customers this type of attributes:

 

 

o  25ml bottle

    include 5% discount !

o  50ml bottle

    include 10% discount !

o  100ml bottle

    include 15% discount and a free 10ml bottle ! (read more)

 

i added an extra description field on "ps_attribute_lang" table but i dont know how to use this in prestashop. i want to enter this text beside value and url section of "Attributes and Values". if there is a way to create new datatype it will be amazing.

 

can you help me friends ? 

thank you

Share this post


Link to post
Share on other sites

Hi.

 

You need to override the Attribute class and add the new column to the $definition array. Then you need to override 'AdminAttributesGroupsController.php' and create the array 'fields_form_override' in 'renderFormAttributes' and add the new column to it.

 

Regards.

Robin.

The CartExpert Team

  • Like 1

Share this post


Link to post
Share on other sites

thanks dear cartExpert for reply . i didn't know the structure of each files. 

i added "desc" field to $difinition . is that correct?

class AttributeCore extends ObjectModel
{
	public $desc;
	
	public static $definition = array(
		'table' => 'attribute',
		'primary' => 'id_attribute',
		'multilang' => true,
		'fields' => array(
			'id_attribute_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'color' => 				array('type' => self::TYPE_STRING, 'validate' => 'isColor'),
			'position' => 			array('type' => self::TYPE_INT, 'validate' => 'isInt'),

			'name' => 				array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
			'desc' => 				array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString', 'size' => 128),
		)
	);

}

but i dont know how to add your advises into 'AdminAttributesGroupsController.php' . can you help me ? 

Share this post


Link to post
Share on other sites

Hi.

 

class AttributeCore extends ObjectModel

 

should be

 

class Attribute extends AttributeCore

 

The file needs to be uploaded to 'override/classes'

 

Don't forget to add the desc as a property to the class (just like id, or name is).

 

With 'AdminAttributesGroupsController.php' you need to overried the 'renderFormAttributes' and create the 'fields_form_override' array and add the new field to it (see how fields_form is set up).

 

$this->fields_form_override = array('type' => 'textarea', 'name' => 'desc', 'label' => $this->l('Description:'), 'lang' => true);

 

Then need to call the parent's function: parent::renderFormAttributes();

 

Regards.

Robin.

The CartExpert Team

  • Like 1

Share this post


Link to post
Share on other sites

well i cancel the over riding process temporary and start to edit core codes just to see the result. 

in classes/attributes.php i add extra_desc field to $definition (red codes): 

public $extra_desc;

	/**
	 * @see ObjectModel::$definition
	 */
	public static $definition = array(
		'table' => 'attribute',
		'primary' => 'id_attribute',
		'multilang' => true,
		'fields' => array(
			'id_attribute_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'color' => 				array('type' => self::TYPE_STRING, 'validate' => 'isColor'),
			'position' => 			array('type' => self::TYPE_INT, 'validate' => 'isInt'),

			'name' => 				array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
			/*mycode - start*/
			'extra_desc' => 		array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 256),
			/*mycode - end*/
		)
	);

in controlers/admin/AdminAttributesGroupsController.php , i add below codes (red codes) to renderFormAttributes function  :

$this->fields_form = array(
			'legend' => array(
				'title' => $this->l('Values'),
				'image' => '../img/admin/asterisk.gif',
			),
			'input' => array(
				array(
					'type' => 'select',
					'label' => $this->l('Attribute type:'),
					'name' => 'id_attribute_group',
					'required' => true,
					'options' => array(
						'query' => $attributes_groups,
						'id' => 'id_attribute_group',
						'name' => 'name'
					),
					'desc' => $this->l('Choose the type of the attribute')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Value:'),
					'name' => 'name',
					'lang' => true,
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}'
				)
				/* mycode - start*/
				,
				array(
					'type' => 'text',
					'label' => 'extra description',
					'extra_desc' => 'extra_desc',
					'lang' => true,
					'size' => 128,
					'desc' => 'show me!!!',
					'hint' => $this->l('Invalid characters:').' <>;=#{}'
				)
				/* mycode - end*/
			)

and ok. the extra_desc field is show up in adding attribute page but when you try to save the attributes, presta gave me an error:

 

 The field extra_desc is required at least in persian.  

 

and of course when i try to edit an attribute , presta gave me php fattal error : 

 

Fatal error: Cannot access empty property in D:\xampp1-8-1\htdocs\atraneh\classes\controller\AdminController.php on line 2382

 

so where is the problem ? i think i must edit the sql query to include my field in select or save. 

Share this post


Link to post
Share on other sites

Hi.

/* mycode - start*/
				,
				array(
					'type' => 'text',
					'label' => 'extra description',
					'extra_desc' => 'extra_desc',
					'lang' => true,
					'size' => 128,
					'desc' => 'show me!!!',
					'hint' => $this->l('Invalid characters:').' <>;=#{}'
				)
				/* mycode - end*/

''extra_desc' => 'extra_desc', is incorrect, should be 'name' => 'extra_desc',

 

Regards.

Robin.

The CartExpert Team

  • Like 1

Share this post


Link to post
Share on other sites

a lot of thanks dear cartExpert !! it's working and it's so good  :)

 

and there is a last question . how can i show my extra field in template ? i know that i must edit product.tpl and <div class="attribute_list"> but i dont know where to call my extra_desc. there is a "$group_attribute" that filled with a varibale "$group" but i dont know where this "$group" filled and initialized. 

Share this post


Link to post
Share on other sites

Hi.

 

$group is initialized in the ProductController.php file within the assignAttributesGroups function, so you will need to override this function.

 

Also, $groups content is populated with the result returned by the 'getAttributesGroups' method of the Product class, therefore this function has to be overriden too. You will need to include the new field in the SQL query of the mentioned function.

 

Regards.

Robin.

The CartExpert Team

  • Like 1

Share this post


Link to post
Share on other sites

  • 5 months later...

Hi Gigamax, CartExpert.

 

Thanks a lot for your help, 

 

I need to do the same thing that Gigamax, but i can not do the last step.

 

I did the first steps, and now i have the info on the data base, and i see text area in the back front. it's working and it's so good 

 

But i can not override the function in ProductController.php in assignAttributesGroups and getAttributesGroups in Product class. Could you please explain to me better this step. I tried, but i can not do this.

 

I put agl.`extra_desc` in the sql, but it doesn`t work. and i do not know where exactly i need to override the product class.

 

A lot of thanks dear friends.

Share this post


Link to post
Share on other sites

  • 2 months later...

Hi.

 

class AttributeCore extends ObjectModel

 

should be

 

class Attribute extends AttributeCore

 

The file needs to be uploaded to 'override/classes'

 

Don't forget to add the desc as a property to the class (just like id, or name is).

 

With 'AdminAttributesGroupsController.php' you need to overried the 'renderFormAttributes' and create the 'fields_form_override' array and add the new field to it (see how fields_form is set up).

 

$this->fields_form_override = array('type' => 'textarea', 'name' => 'desc', 'label' => $this->l('Description:'), 'lang' => true);

 

Then need to call the parent's function: parent::renderFormAttributes();

 

Regards.

 

Robin.

 

The CartExpert Team

 

First of all thank you for your very usefoul tips. I got stucked for hours in overriding renderFormAttributes in ps156 because of this:

http://forge.prestashop.com/browse/PSCFV-11642 (not a bug though)

hope that using: "return AdminController::renderForm();" instead of: "parent::renderForm();" will save someone else's time :)

Share this post


Link to post
Share on other sites

  • 4 months later...
  • 1 year later...

Hi People,

 

This is an alternative for replace textarea for HTML editor,

 

NOTE: in prestashop 1.6 I have to add the column desc to the ps_attribute_lang table

 

 

CREATE TABLE `ps_attribute_lang` (

`id_attribute` int(10) unsigned NOT NULL,

`id_lang` int(10) unsigned NOT NULL,

`name` varchar(128) NOT NULL,

`desc` longtext NOT NULL,

PRIMARY KEY (`id_attribute`,`id_lang`),

KEY `id_lang` (`id_lang`,`name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

Forn declaration:

this:

array(

'type' => 'text',

'label' => 'extra description',

'name' => 'desc',

'lang' => true,

'size' => 128,

'desc' => 'show me!!!',

'hint' => $this->l('Invalid characters:').' <>;=#{}'

)

to:

array(

'type' => 'textarea',

'label' => $this->l('Description:'),

'name' => 'desc',

'lang' => true,

'cols' => 60,

'rows' => 20,

'class' => 'rte',

'autoload_rte' => true,

'hint' => $this->l('Invalid characters:').' <>;=#{}',

),

 

In file Atribute.php rewrite, self::TYPE_HTML is very important

 

this:

'desc' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 256),

 

to:

'desc' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml', 'size' => 4000),

 

Happy Coding!!!!

Share this post


Link to post
Share on other sites

Hello,

I did this, and it works almost perfectly.

I need one more tips :

I tried to set a description of the attribute value just under the short description of my product.
So I want to show the correct description when a value is selected in my input select.

For the moment I have all the list with this code :

            <div id="attributes">
                    <div class="clearfix"></div>
                    {foreach from=$groups key=id_attribute_group item=group}
                        {if $group.attributes|@count}
                                    {assign var="groupName" value="group_$id_attribute_group"}
                                    <div class="attribute_list">
                                        <ul>
                                            {foreach from=$group.attributes2 key=id_attribute item=group_attribute}
                                                    <li>{$group_attribute|escape:'html':'UTF-8'}</li>
                                            {/foreach}
                                        </ul>
                                    </div>
                        {/if}    
                    {/foreach}
            </div>

Can you help me to achieve this please ?

Thanks very much for any help or advice !

Share this post


Link to post
Share on other sites

Hi Skumbabs

 

Try this to show the value:

 

{$group.desc[$id_attribute]} 

 

NOTE: .desc is the name of your custom attribute

<ul>
    {foreach from=$group.attributes key=id_attribute item=group_attribute}
    <li> {$group.desc[$id_attribute]} </li>
    {/foreach}
</ul>

Is very important add this line to the ProductController.php -> line 445 aprox 

 

$groups[$row['id_attribute_group']]['desc'][$row['id_attribute']] = $row['desc'];

 

$row['desc'] is the row in the database in the table ps_attribute_lang

 

Best Coding ;-)

Share this post


Link to post
Share on other sites

Hi Skumbabs

 

Try this to show the value:

 

{$group.desc[$id_attribute]} 

 

NOTE: .desc is the name of your custom attribute

<ul>
    {foreach from=$group.attributes key=id_attribute item=group_attribute}
    <li> {$group.desc[$id_attribute]} </li>
    {/foreach}
</ul>

Is very important add this line to the ProductController.php -> line 445 aprox 

 

$groups[$row['id_attribute_group']]['desc'][$row['id_attribute']] = $row['desc'];

 

$row['desc'] is the row in the database in the table ps_attribute_lang

 

Best Coding ;-)

 

When using your code I got exactly the same result.

 

Maybe I didnt explain myself really good because it set <ul> and <li> in my code.

 

I need only one information by attribute the desc i added in value.

 

In example I have a select which is "Color" (Attribute), in the select I have 2 values : Red and Blue.

For the value red I added a desc which is : "Red like beautiful flames,..."

 

I would like behind the short_description when I choose "Red" -> "Red like beautiful flames,..." shows.

 

To compare my needs to something existing, it's the same as the price changing when the combination change.

 

 

I hope, I'm clearer now :/

And thanks for your help !

 

 

Share this post


Link to post
Share on other sites

  • 3 weeks later...
  • 9 months later...
EDIT: I SOLVE MY PROBLEM ALONE ;)
 
Hello,
 
I'm also adding a description field for attribute values, but I block. I post below my two Override files if anyone can help me.
 
/override/classes/Attribute.php :
<?php

class Attribute extends AttributeCore {
    public $description;

    public static $definition = array(
        'table' => 'attribute',
        'primary' => 'id_attribute',
        'multilang' => true,
        'fields' => array(
            'id_attribute_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'color' =>                array('type' => self::TYPE_STRING, 'validate' => 'isColor'),
            'position' =>            array('type' => self::TYPE_INT, 'validate' => 'isInt'),

            /* Lang fields */
            'name' =>                array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),

            /* Override fields */
            'description' =>                array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
        )
    );
}

/override/controllers/AdminAttributesGroupsController.php :

<?php

class AdminAttributesGroupsController extends AdminAttributesGroupsControllerCore
{
    public function renderFormAttributes()
    {
        $attributes_groups = AttributeGroup::getAttributesGroups($this->context->language->id);

        $this->table = 'attribute';
        $this->identifier = 'id_attribute';

        $this->show_form_cancel_button = true;
        $this->fields_form_override = array(
            'legend' => array(
                'title' => $this->l('Values'),
                'icon' => 'icon-info-sign'
            ),
            'input' => array(
                array(
                    'type' => 'select',
                    'label' => $this->l('Attribute group'),
                    'name' => 'id_attribute_group',
                    'required' => true,
                    'options' => array(
                        'query' => $attributes_groups,
                        'id' => 'id_attribute_group',
                        'name' => 'name'
                    ),
                    'hint' => $this->l('Choose the attribute group for this value.')
                ),
                array(
                    'type' => 'text',
                    'label' => $this->l('Value'),
                    'name' => 'name',
                    'lang' => true,
                    'required' => true,
                    'hint' => $this->l('Invalid characters:').' <>;=#{}'
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Description'),
                    'name' => 'description',
                    'lang' => true,
                    'required' => false,
                    'hint' => $this->l('Invalid characters:').' <>;=#{}'
                )
            )
        );
        return AdminController::renderForm();
    }
}

Thank you in advance, and good evening at all.

 

EDIT: I SOLVE MY PROBLEM ALONE ;)

Edited by jonathanabout (see edit history)

Share this post


Link to post
Share on other sites

  • 8 months later...

Is there any way how to use this to display <img> within the attributes drop down list? Im running 1.6.1.13 and i like to add colored aquare to each color, like on the picture. I tried some advices i find on a website, but two days of tryhard with no success :)

Is there anyone with 1.6 who displays HTML within attribute drop down list? :)

 

Thank you in advance!

 

Hugo

color_time.png

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More