Jump to content

[SOLVED] prestashop 1.5 custom module


bilalhussain
 Share

Recommended Posts

Hello ,

 

can i access the smarty variable 'reference' on the module tpl file?

 

I want to create a custom modules using this tutorial to connect to an external database and using the current loaded product 'reference' variable search and display product extra specification in a tabular format.

 

anybody have an idea how to achieve this?

 

Thanks,

Bilal

 

http://www.technorep...hop-1-5-module/

Edited by bilalhussain (see edit history)

Share this post


Link to post
Share on other sites

Hi, do you mean $product.reference as in `reference` column in ps_product table ?

As long as you load the proper Product object, you can use it.

By extra specification, are you thinking of something like this http://www.prestasho...uct-extra-tabs/

 

 

Yes I meant the ps_prodcut.reference field since this is the catalog# in our POS desktop application. I am creating part# on prestashop DB and running sync app updating their quantities and some other fields with desktop software. Now I built an application to add extra product field in desktop application which I want to display on the product page. (not under extra tab) using simple table. I am new to prestashop development I would have to learn how to initialize the product object in order to get the reference field so I can make query according to that reference.

Share this post


Link to post
Share on other sites

it will be much better (especially much faster), if the data will be in the same database

 

Yes, I can put the table in prestashop database. but I would need help with simple select query based on the current page ps_product.reference field to pull related data from new table to display it like this..see the pic.

 

post-478363-0-29013800-1372882865_thumb.png

Share this post


Link to post
Share on other sites

so let us know how your database looks like and i will prepare a query for you

 

 

Thanks vekia,

 

here you go.

 

CREATE TABLE `ps_tirespecs` (
 `Catalog` varchar(30) NOT NULL,
 `Sidewall` varchar(50) DEFAULT '',
 `LoadIndex` varchar(50) DEFAULT '',
 `LoadRange` varchar(50) DEFAULT '',
 `SpeedRating` varchar(50) DEFAULT '',
 `UTQGTreadwear` varchar(50) DEFAULT '',
 `UTQGTraction` varchar(50) DEFAULT '',
 `UTQGTemperature` varchar(50) DEFAULT '',
 `MaxSingleLoad` varchar(50) DEFAULT '',
 `MaxSingleAirPressure` varchar(50) DEFAULT '',
 `TreadDepth` varchar(50) DEFAULT '',
 `RimWidthRange` varchar(50) DEFAULT '',
 `MeasuredRimWidth` varchar(50) DEFAULT '',
 `SectionWidth` varchar(50) DEFAULT '',
 `TreadWidth` varchar(50) DEFAULT '',
 `OverallDiameter` varchar(50) DEFAULT '',
 `MileageWarranty` varchar(50) DEFAULT '',
 `RoadHazard` varchar(5) DEFAULT '',
 `date_add` datetime DEFAULT NULL,
 `date_update` datetime DEFAULT NULL,
 PRIMARY KEY (`Catalog`)

 

the above table will have the tire specs information. this table will be being sync with my local POS MySQL database table. We could directly make query to it but as you mentioned it would be easy if the table is in same DB.

 

Now here is how I want it to work. My custom module based on http://www.technoreply.com/how-to-create-a-prestashop-1-5-module/ this tutorial. I used the

function hookDisplayFooterProduct()

to display it on product page. Once the customer click on item to see the detail page. My code will check the product.reference field against the ps_tirespecs.catalog field to see if there is a match and display all the specs inside a table. which I attached it in earlier post.

 

I will be very thankful to you if you can guide me through. :)

 

Regards,

Bilal

Share this post


Link to post
Share on other sites

okay, you said that everything should be based on the reference field, where you've got the reference field in your database (mentioned above) ?

 

reference field inside PS.product table is catalog number of my local database table. ? I hope I am explaining it correctly.

Share this post


Link to post
Share on other sites

okay now it's clear

 

you should run query like:

SELECT * FROM ps_product AS a LEFT JOIN ps_tirespecs AS b on a.reference = b.catalog

 

Thank you for the query. but my question is how would I detect which product is being loaded in the browser? I want to access the current loaded product.reference # so I can match that against the ps_tirespecs.catalog to get the specs. Here is my module .php code.

 

<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this module. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Jevin O. Sewaruth.
* ----------------------------------------------------------------------------
*/
if (!defined('_PS_VERSION_'))
exit;
class ProductSpecs extends Module
{
public function __construct()
{
	$this->name = 'ProductSpecs';
	$this->tab = 'front_office_features';
	$this->version = '1.0';
	$this->author = 'Bilal Hussain copied from Jevin O. Sewaruth';
	parent::__construct();
	$this->displayName = $this->l('ProductSpecs Module');
	$this->description = $this->l('This is just an empty module. You should modify it to make your own.');
	$this->confirmUninstall = $this->l('Are you sure you want to uninstall this module?');
	$this->_checkContent();
	$this->context->smarty->assign('module_name', $this->name);
}
public function install()
{
	if (!parent::install() ||
		!$this->registerHook('displayHeader') ||
		!$this->registerHook('displayLeftColumn') ||
		!$this->registerHook('displayRightColumn') ||
		!$this->registerHook('displayFooter') ||
  !$this->registerHook('displayFooterProduct') ||
		!$this->_createContent())
		return false;
	return true;
}
public function uninstall()
{
	if (!parent::uninstall() ||
		!$this->_deleteContent())
		return false;
	return true;
}

public function hookDisplayHeader()
{
	$this->context->controller->addCSS($this->_path.'css/style.css', 'all');
	$this->context->controller->addJS($this->_path.'js/script.js', 'all');
}

public function hookDisplayLeftColumn()
{
	$this->context->smarty->assign(array(
		'placement' => 'left',
	));
	return $this->display(__FILE__, 'left.tpl');
}

public function hookDisplayRightColumn()
{
	$this->context->smarty->assign(array(
		'placement' => 'right',
	));
	return $this->display(__FILE__, 'right.tpl');
}
/* Bilal added */
public function hookDisplayFooterProduct()
{
	$this->context->smarty->assign(array(
		'placement' => 'footerproduct',
	));
	return $this->display(__FILE__, 'footerproduct.tpl');
}

public function hookDisplayFooter()
{
	$this->context->smarty->assign(array(
		'module_link' => $this->context->link->getModuleLink('productspecs', 'details'),
	));
	return $this->display(__FILE__, 'footer.tpl');
}
public function getContent()
{
	$message = '';
	if (Tools::isSubmit('submit_'.$this->name))
		$message = $this->_saveContent();
	$this->_displayContent($message);
	return $this->display(__FILE__, 'settings.tpl');
}
private function _saveContent()
{
	$message = '';
	if (Configuration::updateValue('MOD_PRODUCTSPECS_NAME', Tools::getValue('MOD_PRODUCTSPECS_NAME')) &&
		Configuration::updateValue('MOD_PRODUCTSPECS_COLOR', Tools::getValue('MOD_PRODUCTSPECS_COLOR')))
		$message = $this->displayConfirmation($this->l('Your settings have been saved'));
	else
		$message = $this->displayError($this->l('There was an error while saving your settings'));
	return $message;
}
private function _displayContent($message)
{
	$this->context->smarty->assign(array(
		'message' => $message,
		'MOD_PRODUCTSPECS_NAME' => Configuration::get('MOD_PRODUCTSPECS_NAME'),
		'MOD_PRODUCTSPECS_COLOR' => Configuration::get('MOD_PRODUCTSPECS_COLOR'),
	));
}
private function _checkContent()
{
	if (!Configuration::get('MOD_PRODUCTSPECS_NAME') &&
		!Configuration::get('MOD_PRODUCTSPECS_COLOR'))
		$this->warning = $this->l('You need to configure this module.');
}
private function _createContent()
{
	if (!Configuration::updateValue('MOD_PRODUCTSPECS_NAME', '') ||
		!Configuration::updateValue('MOD_PRODUCTSPECS_COLOR', ''))
		return false;
	return true;
}
private function _deleteContent()
{
	if (!Configuration::deleteByName('MOD_PRODUCTSPECS_NAME') ||
		!Configuration::deleteByName('MOD_PRODUCTSPECS_COLOR'))
		return false;
	return true;
}

}

?>

 

 

What my understanding is that

1. I need to access the product object to access the reference field then

2. make a query against the table to get the result inside a result variable then

3. on the .tpl file display them inside a tabular format..

 

then the query will be like

 

$reference = $this->smarty->product.referece; /* idk how to access this */
so once I have the current loaded reference in a variable then the query will be like
SELECT * from ps_tirespecs where catalog=$reference

 

 

 

 

What do you think?

 

Bilal

Edited by bilalhussain (see edit history)

Share this post


Link to post
Share on other sites

You can get browsed product id with:

$id_product = Tools::getValue('id_product');

 

then create new product object like this:

$browsed_product = new Product($id_product);

 

and define the reference field:

$reference=$browsed_product->reference;

 

SELECT * FROM ps_product AS a LEFT JOIN ps_tirespecs AS b on a.reference = b.catalog WHERE a.reference = '$reference'

 

and result of this query you can add to the smarty variables array (to template)

Share this post


Link to post
Share on other sites

You can get browsed product id with:

$id_product = Tools::getValue('id_product');

 

then create new product object like this:

$browsed_product = new Product($id_product);

 

and define the reference field:

$reference=$browsed_product->reference;

 

SELECT * FROM ps_product AS a LEFT JOIN ps_tirespecs AS b on a.reference = b.catalog WHERE a.reference = '$reference'

 

and result of this query you can add to the smarty variables array (to template)

 

Thanks for all of your help all along my project. I have got to this far and I think I am very close to success.

 

my module .php file function

 

public function hookDisplayFooterProduct()
{
	$this->context->smarty->assign(array(
		'placement' => 'footerproduct',));

	$id_product = Tools::getValue('id_product');
	$browsed_product = new Product($id_product);
	$reference = $browsed_product->reference;
	echo $reference;	    /*debugging purpose*/

	$sql = "SELECT * FROM ps_tirespecs WHERE catalog = '$reference'";
	if ($result = Db::getInstance()->ExecuteS($sql))
		foreach ($result as $row)
	$smarty->assign('array',$result);
   return $this->display(__FILE__, 'footerproduct.tpl');
}

 

and my module .tpl file code to print those values like this..

 

{$array.Catalog}
{$array.Sidewall}
etc

 

I don't get any output?

 

 

Regards,

Bilal

Edited by bilalhussain (see edit history)

Share this post


Link to post
Share on other sites

url that you pasted redictect me to the some online pharmacy store, what's goin on?

btw. glad that I could help you a bit here:)

 

Thanks for pointing that out. I was just browsing it earlier and it was working fine. I am with web hosting company right now to resolve this issue.

 

Thanks:)

Share this post


Link to post
Share on other sites

Vekia, I appreciate you help throughout my project. I got it working FINALLY. I published this module as 'beta' state. Please have a look at it and I would like your comments on it. I have few things to change but later. :)

 

http://shoptwm.com/e...a-accelera.html

 

 

Regards,

Bilal

 

 

The above link should we working now. It was funny that my dedicated IP got assigned to a different server/website that is why it was redirecting to a pharmacy website.

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