Jump to content

code for insert extra description


ilario

Recommended Posts

hi

 

I have a new table "description_price_category" (example) with two fields
id_category and description_price

in page Product (product.tpl) I would insert below the price the description_price linked to the category (id_category) Product

 

thank

ilario

 

post-757615-0-01570600-1428068250_thumb.jpg

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

If you make it its own table, you'll have to make some class for it, with all functions to init, read and write the data from/to the table. See also other files in /classes/ directory for examples.

 

I'll give some small, but important parts of your class. N.B. This is NOT complete, look at other classes what is exactly needed!

 

Something like:

create file /classes/DescriptionPriceCategory.php:

 

class DescriptionPriceCategory extends ObjectModel
{
 
/** @var string Description Price */
public $Description_price;
...
 
And some code for definition of table etc. see other classes as example...
 
public static $definition = array(
  'table' => 'descprice',   // your new table name, without prefix
  'primary' => 'id_category',
  'multilang' => false ,     // true if you want to support multi languages
  'multilang_shop' => false,  // true if you want to support the multi shop option of PrestaShop
  'fields' => array(
  /* Classic fields */
  'description_price' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
}
 
Further all functions to construct, init, add, update, delete etc.
 

Then write some function to get the description_price, given a specific category_id:

     static public function getDescriptionPriceOfCategory($id_category)

 

Then, override controllers/front/ProductController.php and add to the function:

 
public function initContent()
 
something like this to the smarty assign:
 
$this->context->smarty->assign(array(
    'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
    'customizationFields' => $customization_fields,
    'accessories' => $accessories,
    'return_link' => $return_link,
    'product' => $this->product,
    'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
    'description_price_category' => DescriptionPriceCategory::getDescriptionPriceOfCategory($this->category->id);
 
Then, finally, in product.tpl, you can use {$description_price_category} wherever you want.
 
 
 
Long story. Best is also to actually put it in a module, so you can disable/enable it easily and add it to another shop, or it might be easier when upgrading. But that would give even more work.
 
 
 
Maybe it's easier to override Category class directly and add a new field description_price in in ps_category, or if you want more languages, in table ps_category_lang and add the definition here.
 
An example how an extra description is added (but then with category class and product-list.tpl, not product.tpl), you can see here
 
 
Hope this helps,
pascal.
  • Like 1
Link to comment
Share on other sites

 

If you make it its own table, you'll have to make some class for it, with all functions to init, read and write the data from/to the table. See also other files in /classes/ directory for examples.

 

I'll give some small, but important parts of your class. N.B. This is NOT complete, look at other classes what is exactly needed!

 

Something like:

create file /classes/DescriptionPriceCategory.php:

 

class DescriptionPriceCategory extends ObjectModel
{
 
/** @var string Description Price */
public $Description_price;
...
 
And some code for definition of table etc. see other classes as example...
 
public static $definition = array(
  'table' => 'descprice',   // your new table name, without prefix
  'primary' => 'id_category',
  'multilang' => false ,     // true if you want to support multi languages
  'multilang_shop' => false,  // true if you want to support the multi shop option of PrestaShop
  'fields' => array(
  /* Classic fields */
  'description_price' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
}
 
Further all functions to construct, init, add, update, delete etc.
 

Then write some function to get the description_price, given a specific category_id:

     static public function getDescriptionPriceOfCategory($id_category)

 

Then, override controllers/front/ProductController.php and add to the function:

 
public function initContent()
 
something like this to the smarty assign:
 
$this->context->smarty->assign(array(
    'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
    'customizationFields' => $customization_fields,
    'accessories' => $accessories,
    'return_link' => $return_link,
    'product' => $this->product,
    'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
    'description_price_category' => DescriptionPriceCategory::getDescriptionPriceOfCategory($this->category->id);
 
Then, finally, in product.tpl, you can use {$description_price_category} wherever you want.
 
 
 
Long story. Best is also to actually put it in a module, so you can disable/enable it easily and add it to another shop, or it might be easier when upgrading. But that would give even more work.
 
 
 
Maybe it's easier to override Category class directly and add a new field description_price in in ps_category, or if you want more languages, in table ps_category_lang and add the definition here.
 
An example how an extra description is added (but then with category class and product-list.tpl, not product.tpl), you can see here
 
 
Hope this helps,
pascal.

 

hello
thanks very much  for the code, I followed your steps but I must have done something wrong because it does not work (blank page), do not know well php
 
I made the code
 
<?php

class DescriptionPriceCategory extends ObjectModel
{
 
/** @var string Description Price */
public $Description_price;
  
public static $definition = array(
  'table' => 'product_description_price',   // your new table name, without prefix
  'primary' => 'id_category',
  'multilang' => false ,     // true if you want to support multi languages
  'multilang_shop' => false,  // true if you want to support the multi shop option of PrestaShop
  'fields' => array(
  /* Classic fields */
  'description_price' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
    
};

static public function getDescriptionPriceOfCategory($id_category)
   
   {
		return Db::getInstance()->executeS('
			SELECT `description_price`
			FROM `'._DB_PREFIX_.'ps_product_description_price`
			WHERE `id_category_default` = '.(int)$id_category);
	}
	
}

and add in productcontroller.php

$this->context->smarty->assign(array(
				'stock_management' => Configuration::get('PS_STOCK_MANAGEMENT'),
				'customizationFields' => $customization_fields,
				'accessories' => $accessories,
				'return_link' => $return_link,
				'product' => $this->product,
				'product_manufacturer' => new Manufacturer((int)$this->product->id_manufacturer, $this->context->language->id),
				'description_price_category' => DescriptionPriceCategory::getDescriptionPriceOfCategory($this->category->id),
				'token' => Tools::getToken(false),
.............
			

and in product.tpl

.....
   {/if}
   <p>test Description  {$description_price_category}</p>
</div> <!-- end prices -->

thank 

 

ilario

Link to comment
Share on other sites

Hi ilario,

 

As I mentioned at the beginning of my first post, this is NOT the complete code, you have to add lots of other code like construct, add, update, delete object  functions etc.

 

The white page is because you have errors in the code. You can turn on debug mode to see the error, instead of just a white page.

(N.B. In the controller, only add the red line in the already existing smarty assign code). It's probaby complaining that nor construct is defined or so.

 

If you don't know php that well, I suggest you first play with some smaller changes, not directly creating a new class, until you have some more feeling for the PrestaShop environment. As I mentioned, a lot of additional code is needed to make the class work and be connected to your table etc.

 

If you really need this, I suggest to look at the alternative solution, by not creating a whole new table, but just adding a field to the ps_category table where you define the description_price. There I added a link to a step by step example how to add a description to the category class and how to use it.

 

(I quote again from my previous post):

 

Maybe it's easier to override Category class directly and add a new field description_price in in ps_category, or if you want more languages, in table ps_category_lang and add the definition here.

 
An example how an extra description is added (but then with category class and product-list.tpl, not product.tpl), you can see here
 

 

(See my first post for the link)

 

If you really need it in a separate table, look at the other classes how they are defined in detail and from there build your own.

 

 

Success!

 

pascal.

Link to comment
Share on other sites

Hi ilario,

 

As I mentioned at the beginning of my first post, this is NOT the complete code, you have to add lots of other code like construct, add, update, delete object  functions etc.

 

The white page is because you have errors in the code. You can turn on debug mode to see the error, instead of just a white page.

(N.B. In the controller, only add the red line in the already existing smarty assign code). It's probaby complaining that nor construct is defined or so.

 

If you don't know php that well, I suggest you first play with some smaller changes, not directly creating a new class, until you have some more feeling for the PrestaShop environment. As I mentioned, a lot of additional code is needed to make the class work and be connected to your table etc.

 

If you really need this, I suggest to look at the alternative solution, by not creating a whole new table, but just adding a field to the ps_category table where you define the description_price. There I added a link to a step by step example how to add a description to the category class and how to use it.

 

(I quote again from my previous post):

 

(See my first post for the link)

 

If you really need it in a separate table, look at the other classes how they are defined in detail and from there build your own.

 

 

Success!

 

pascal.

thanks you very much
 
the error is
Fatal error: Class 'DescriptionPriceCategory' not found in /home2/dbsantam/public_html/shop/controllers/front/ProductController.php on line 266
 
in class I have to do only a select, do not want to do anything else, the description of the insert directly into the database, they are so few
 
I could give a hand for the class please
Link to comment
Share on other sites

Hi ilario,

 

Not sure where you're going with this, but anyway, the error code suggests the ProductController file doesn't know of the class yet. (I'm a little surprised/concerned though, as it seems that you change directly the ProductController core file, instead of overriding it... I recommend to override the ProductController, and THERE add your code referencing the new class, NOT in the core Product Controller file)

 

Anyway, you probably have to tell it something like:

 

require_once(_PS_MODULE_DIR_ . 'path/to/your/class/DescriptionPriceCategory.php'); 
 
(assuming you make some module around it. Otherwise, change the _PS_MODULE_DIR_ to something else appropriate to find the Class)
 
add that (with the path corrected!) line at the beginning of the ProductController.php file, just after the <?php code (Line 1)
 
 
My 2 cents,
pascal.
Link to comment
Share on other sites

The getDescription functions could be something like:

 

 

/**
* Get DecriptionPrice data for a given id_category and id_lang
*
* @param integer $id_lang Language id
* @param integer $id_category Category id
* @return array with DescriptionPrice data
* @static
*/
public static function getDescriptionPriceOfCategory($id_lang, $id_category)
{
    return Db::getInstance()->getRow('
            SELECT *
            FROM `'._DB_PREFIX_.'descriptionpricecategory` dpc
            LEFT JOIN `'._DB_PREFIX_.'descriptionpricecategory_lang` dpcl
                ON ( dpc.`id_category` = dpcl.`id_category` AND dpcl.`id_lang` = '.(int)$id_lang.')
            WHERE dpc.`id_category` = '.(int)$id_category
    );
}
 
 
/**
* Get all description prices for a given language
*
* @param integer $id_lang Language id
* @return array Multiple arrays with description price's data
* @static
*/
public static function getDescriptionPrices($id_lang)
{
    return Db::getInstance()->executeS('
            SELECT DISTINCT dpc.*, dpcl.*
            FROM `'._DB_PREFIX_.'descriptionpricecategory` dpc
            LEFT JOIN `'._DB_PREFIX_.'descriptionpricecategory_lang` dpcl
                    ON (dpc.`id_category` = dpcl.`id_category` 
                    AND dpcl.`id_lang` = '.(int)$id_lang.')'
    );
}
 
(Didn't test, sorry for any typing errors...)
--------------------------
or, if no languages:
 
/**
* Get Decription Price data for a given id_category
*
* @param integer $id_category Category id
* @return array with Description Price data
* @static
*/
public static function getDescriptionPriceOfCategory($id_category)
{
    return Db::getInstance()->getRow('
            SELECT *
            FROM `'._DB_PREFIX_.'descriptionpricecategory` dpc
            WHERE dpc.`id_category` = '.(int)$id_category
    );
}
 
 
/**
* Get all description prices
*
* @return array Multiple arrays with description price's data
* @static
*/
public static function getDescriptionPrices()
{
    return Db::getInstance()->executeS('
            SELECT DISTINCT *
            FROM `'._DB_PREFIX_.'descriptionpricecategory`'
    );
}
 
 
I think something like this.
pascal
Link to comment
Share on other sites

thanks Pascal
I made a mix of your codes,

no longer a separate table, but added a field "description_price" in category_lang and with the class

<?php

class DescriptionPriceCategory extends ObjectModel
{
 
/** @var string Description Price */
public $Description_price;
  
public static $definition = array(
  'table' => 'category_lang',   // your new table name, without prefix
  'primary' => 'id_category',
  'multilang' => true ,     // true if you want to support multi languages
  'multilang_shop' => false,  // true if you want to support the multi shop option of PrestaShop
  'fields' => array(
  /* Classic fields */
  'description_price' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
    ),
	);
 
 public static function getDescriptionPriceOfCategory($id_category, $id_lang)
  {
    return Db::getInstance()->getRow('
            SELECT `description_price`
            FROM `'._DB_PREFIX_.'category_lang`
            WHERE `id_category` = '.(int)$id_category. ' and `id_lang` = '.(int)$id_lang
    );   
  }

}
 

then I did override the productcontroller.php

<?php
 
require_once(_PS_ROOT_DIR_ . '/classes/DescriptionPriceCategory.php'); 

class ProductController extends ProductControllerCore
{

	 
	public function initContent()
	{
		parent::initContent();
			 
	
	$this->context->smarty->assign(array(
	
	'description_price_category' => DescriptionPriceCategory::getDescriptionPriceOfCategory($this->category->id, $this->context->language->id)
	));
	
	}			
}

and finally in product.tpl I added


...........

<div id="description_price_category" class="align_justify" itemprop="description">
     <p>{$description_price_category['description_price'] }</p>
</div>
							<!-- end prices -->

thanks again for everything
Now I study your code in the addition description_long category, and insert description_price

 

I owe you a beer

 

Ilario

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