Jump to content

Product informations extraction


EricGardien

Recommended Posts

[RESOLVED]

 

 

Hello,

 

I try in vain to recover data from a product.

I wish I could get the name, description, and price (Tax included) of a product and the name of the shop, in a module without altering Prestashop files.

 

Apart from a few variables as img_prod_dir that I can use, the others remain inaccessible.

 

A big thank you to those who can help me.

 

Eric

Link to comment
Share on other sites

Hello,

 

Yes , of a specific product.

When on the page product.php?id_product=6 for instance, I would like to grab all the informations of the product number 6.

 

Thank you for your reply ;-)

Eric

When you say grab, what do you mean? display?

 

All information related to the product are in the object Product. this object get instanciated as part of the page preparation and is available thru the Smarty variable $product. Any time in your template you can access public member of the object by {$product->member} you can also access any public method the same way {$product->method (...) }.

 

Hope this clarifies and covers your need.

 

Best,

(d)oekia

Link to comment
Share on other sites

Elaboration : ;)

 

First of all, I am making a module which could work for all prestashopers.

 

The module should send the product informations via a form to another website.

For that I need to grab the product name, short description, price, currency, image links, url to the product page, and the identifier of the shop.

 

I am trying to get it from the page itself with a javascript getElement... but I don't find it really good enough.

I would prefer to get the data from the database of the shop or from a controller if possible.

 

I don't know if my explanation is clear ?

 

Thank you for your answers.

Eric

Link to comment
Share on other sites

I am trying to get it from the page itself with a javascript getElement... but I don't find it really good enough.

 

DOM extraction can only grad information that exists on a page. That is certainly a dead-end for your scenario.

Have you take a look to the webservices?

Link to comment
Share on other sites

Webservices ... no not yet.

Is the $product var global ?

If I try to use {$product->name} it returns no result

$product exists in Smarty. I suspect you try to fetch it thru javascript. Again javascript will be of no help here since the $product does not exists in the page. You could modify your template to jasonize the $product as a javascript object but this way you only get members. Method are lost.

To rephrase that in asp terminology, $product is a server side object.

Link to comment
Share on other sites

First you will need to know where the module is hooked to.

 

There are different variables set to $smarty at various stages. Simple example:

 

Product page:

  • Hook header will only have id_product from request.
  • Hook footer will have $product with almost everything you need.

Best to work out what you need first, and where you need it and then consider to get it from $smarty or write simple queries to extract only what you need.

 

Michael

Link to comment
Share on other sites

Hello Michael,

 

Thank you for this information.

The module is hooked in the ProductAction block, near the 'add to cart' button.

I included the productController.php in my php, and found almost all variables that I need.

It was not so easy because I haven't found any documentation on which variables are available and their names.

 

The only problem remaining is to get the product image link.

I get the path but no the 2 numbers identifying the image.

 

Any idea ?

 

Thank you

Eric

Link to comment
Share on other sites

in PHP

$cover = Product::getCover($product_item->id);
$product_item->id_image= ''.$product_item->id.'-'.(isset($cover['id_image'])?$cover['id_image']:0);

 

with $product_item a valid instance of Product

 

Then in the template

 

<a href="{$product->getLink()|escape:'htmlall':'UTF-8'}"><img src="{$link->getImageLink($product->link_rewrite, $product->id_image, 'small')}" /></a>

Link to comment
Share on other sites

Please try to be clear

Value remain 0 mean absolutly nothing

If you have a VALID Product object, its id is set hence 0 is not even an answer you get.

 

 	 $product_item = new Product((int)$an_id_i_have_in_my_hand);
      if (Validate::isLoadedObject($product_item))
      {
          $cover = Product::getCover($product_tem->id);
          $product_item->id_image= ''.$product_item->id.'-'.(isset($cover['id_image'])?$cover['id_image']:0);
	  }
	  else throw new Exception('Bugger! My coding logic is wrong.');

	  global $smarty;
	  $smarty->assign('product',$product_item);

 

Change config.inc.php to displays php and sql errors

Add a "does not permit any coding mistake" trapper code in config.inc.php

set_error_handler(create_function('','{ error_reporting(0); restore_error_handler(); while (ob_get_level()) ob_end_flush(); echo \'<pre>\'.print_r(debug_backtrace(),true).\'</pre>\'; exit(0); }'));

 

remove the _ character before the filename within your override/classes/*

 

Sorry I'm willing to help but this does not mean cut/paste recipe without you to at least understand what you are doing.

Link to comment
Share on other sites

<p> </p>

<div>Ok, sorry for the miss understanding.</div>

<div> </div>

<div>I am trying so many thing to find the solution that sometimes I'm lost ;-)</p></div>

<div> </div>

<div>Aim of the project:</div>

<div>Add a button in the block ProductActions (under the 'Add to cart' button) which sends information to another website.</div>

<div> </div>

<div>I need to send:</div>

<div>* language of the user,</div>

<div>* product name,</div>

<div>* link to the product (eg <a href="http://prestashop.cieldesiles.com/prestashop/product.php?id_product=7">product.php?id_product=7</a>),</div>

<div>* link to the image product,</div>

<div>* product price,</div>

<div>* currency</div>

<div>It seems easy no ?</div>

<div>But I m lost in the logic of prestashop.</div>

<div>The only solution that I found is to get those values in javascript in the file tpl which generate an innerHTML with the link that I need.</div>

<div>But for the image it's not working.</div>

<div>So I though to start from the beginning and to get all the values in the php file and send them with a smarty array in my tpl file.</div>

<div><br />

<div>

<div>public function hookProductActions($params)</div>

<div>{</div>

<div>include( '../../config/config.inc.php' );</div>

<div> </div>

<div>global $smarty;</div>

<div> </div>

<div>$pn = $product-&gt;name;</div>

<div>$pl = $base_dir.'product.php?id_product='.$product-&gt;id;</div>

<div>$lg = $lang_iso;</div>

<div>$pd = $product-&gt;description_short;</div>

<div>$cover = Product::getCover($product_item-&gt;id);</div>

<div>$product_item-&gt;id_image= ''.$id_product.'-'.(isset($cover['id_image'])?$cover['id_image']:0);</div>

<div>$il = $link-&gt;getImageLink($product-&gt;link_rewrite, $product_item-&gt;id_image, 'small'); </div>

<div>$pp = $productPriceWithoutEcoTax+$ecotax_tax_inc;</div>

<div>$pc = $currency-&gt;sign;</div>

<div> </div>

<div>$smarty-&gt;assign(array(</div>

<div>'aid'=&gt;$aid,</div>

<div>'pn'=&gt;$pn,</div>

<div>'pl'=&gt;$pl,</div>

<div>'lg'=&gt;$lg,</div>

<div>'pd'=&gt;$pd,</div>

<div>'il'=&gt;$il,</div>

<div>'pp'=&gt;$pp,</div>

<div>'pc'=&gt;$pc,</div>

<div>));</div>

<div> </div>

<div>}</div>

<div> </div>

</div>

</div>

<div>Thank you very much for your help</div>

 

Link to comment
Share on other sites

Could edit your post and replace all occurence of > (the greater than sign) by ampersand gt semi-colon.

This is a long lasting forum bug

 

But you have not even try what I did posted last have you?

Pitty because that way you will have spotted your coding errors.

Link to comment
Share on other sites

yes I tried everything.

I got the path to the file but with values 0/0 for the image.

I suspect that $cover['id_image'] returns 0.

 

Maybe have a look at the result that I want.

 

http://prestashop.cieldesiles.com/prestashop/product.php?id_product=1

 

This is generated on the tpl page with a javascript.

The script extract the path to the image within the div tag image-block. Not so nice, isn't ?

Link to comment
Share on other sites

You suspect !!! That is driving me crazy.

 

There is a typo

$cover = Product::getCover($product_tem->id);

should be

$cover = Product::getCover($product_item->id);

Was fixed but the forum seems buggy nowadays correction get lost

 

I explained you how to have immediate information from your code of such problem then no need to guess

 

Why do you refuse to use the proper method to develop something?

You are wasting your time and mine.

Link to comment
Share on other sites

<p>I don't refuse. I spend too much time on this already, and I try to get it done asap.</p>

<p>I really don't need to waste time. Not mine, not yours.</p>

<p> </p>

<p>But ... I'm new in Prestashop, and even in php.</p>

<p>It could be so simple with few example.</p>

<p>My question is no so complicated, I think.</p>

<p> I paste your code and I get this as result.</p>

<p> </p>

<p><img src="http://prestashop.cieldesiles.com/prestashop/img/p/-small.jpg"></p>

<p> </p>

<p>So the information is clearly not passed to the tpl.</p>

<p> </p>

<p>In php</p>

<div>

</div>

<div>

<div>public function hookProductActions($params)</div>

<div>{<span class="Apple-tab-span" style="white-space:pre"> </span></div>

<div>global $smarty;</div>

<div> </div>

<div>$cover = Product::getCover($product_item->id);</div>

<div>$product_item->id_image= ''.$id_product.'-'.(isset($cover['id_image'])?$cover['id_image']:0);</div>

<div>return ($this->display(__FILE__, 'donaki.tpl'));</div>

<div>}</div>

<div> </div>

</div>

<div>

</div>

<div> </div>

<div>in tpl</div>

<div>

<div>

</div>

<div><img src="{$link->getImageLink($product->link_rewrite, $product->id_image, 'small')}" /></a></div>

<div>

</div>

<p> </p>

<p> </p>

<div>if I add</div>

<div>$smarty->assign('product_item',product_item$);<span class="Apple-tab-span" style="white-space: pre; "> </span></div>

<div>in php, it returns a 500 err</div>

<div> </div>

</div>

 

Link to comment
Share on other sites

It could be so simple with few example.

Wrong Prestashop is indeed a script but around 100'000 lines of Object Oriented PHP

 

You want to code but don't have the basis

You want to code but you want it done before you have think of it

]example:

$smarty->assign('product_item',product_item$);

1 _________________________________________^

2 _______________^^^^^^^^^^^^

 

There is no magic, no arcane, it is a language. I'm not such super-powerful/genius than you think developpers are. We just manage to take the time to get knowlege. Even do I write code since ages, I never try quick fast and furious coding at 4AM in the morning returning from a party.

 

Re-read slowly first part of post#16 (correct the product_tem in product_item - I can stop edit the forum it refuses to modify this entry). Does it look close to the line you mention? That is what makes me having such a call -

Link to comment
Share on other sites

Thank you professor.

Your are right I made a syntax mistake placing the $ wrongly.

I am coding for so many years (probably more than you) that I don't need that destroying kind of speech.

But ok, you helped me a little.

Here below the right code, necessary to get it achieved.

Your code couldn't work without the getvalue ... product_item a valid instance of product :-)

Of course you said it ... now that I found it, i understand what you meant.

public function hookProductActions($params)

{<span class="Apple-tab-span" style="white-space:pre"> </span>

global $smarty;

$cover = Product::getCover(intval(Tools::getValue('id_product')));

$product_item->id_image=(isset($cover['id_image'])?$cover['id_image']:0);

$smarty->assign('product_item',$product_item);

return ($this->display(__FILE__, 'mytemplate.tpl'));

}

 

I hope this post will help other newbies with ps.

PS: I don't code at 4am after a party, I come back later.

PS2: It is so nice from you willing to help, continue it staying calm, it's better for everybody.

PS3: this code work too (even if I don't know why before the isset is ...$product_item->id ... necessary)

public function hookProductActions($params)

{

global $smarty;

$product_item->id = intval(Tools::getValue('id_product'));

$cover = Product::getCover($product_item->id);

$product_item->id_image=''.$product_item->id.'-'.(isset($cover['id_image'])?$cover['id_image']:0);

$smarty->assign('product_item',$product_item);

return ($this->display(__FILE__, 'mytemplate.tpl'));

}

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...