Page 1 of 1
How to pass a variable to another controller's view?
Posted: Tue Mar 13, 2012 9:02 pm
by vaanbooch
Hello,
I'm trying to integrate a facebook like button on every product/product page. In order to specify the image shown on facebook I should pass a meta tag <meta property="og:image" content="current-product-thumb.jpg" /> from controller/product/product.php to the <head> section of common/header.tpl
Is it possible to pass a variable from a controller to another controller's view?
The OpenCart version is 1.5.1.3.
Thanks in advance!
VaanBooch
Re: How to pass a variable to another controller's view?
Posted: Wed Mar 14, 2012 8:02 am
by Jeremy Fisk
it sure is... but i always grab it in the other controller as well
Cheers
jem
Re: How to pass a variable to another controller's view?
Posted: Wed Mar 14, 2012 4:53 pm
by vaanbooch
Jeremy Fisk wrote:it sure is... but i always grab it in the other controller as well
You mean to get the current product's data in common/header.php again? It's possible, of course, but I wonder if there is a more ellegant way to do it ....
Re: How to pass a variable to another controller's view?
Posted: Thu Mar 15, 2012 2:23 am
by Jeremy Fisk
vaanbooch wrote:Jeremy Fisk wrote:it sure is... but i always grab it in the other controller as well
You mean to get the current product's data in common/header.php again? It's possible, of course, but I wonder if there is a more ellegant way to do it ....
Dunno... but what i often do is put in a get request for the product ID and then grab the corrosponding info from the database
Re: How to pass a variable to another controller's view?
Posted: Wed Mar 21, 2012 4:01 pm
by vaanbooch
Thanks Jeremy,
I've made a vqmod and everything went fine. The code is below.
Code: Select all
<modification>
<id>Facebook og:image fix</id>
<version>1.0</version>
<vqmver>1.0.8</vqmver>
<author>www.bewebdesign.be</author>
<file name="catalog/controller/common/header.php">
<operation>
<search position="after"><![CDATA[$this->load->model('catalog/product');]]></search>
<add><![CDATA[
if (isset($this->request->get['product_id'])) {
$product_id = $this->request->get['product_id'];
$product_info = $this->model_catalog_product->getProduct($product_id);
$this->load->model('tool/image');
if ($product_info['image']) {
$this->data['og_image'] = str_replace( ' ', '%20', $this->model_tool_image->resize($product_info['image'],100,100) );
} else {
$this->data['og_image'] = 'FULL_PATH_TO_YOUR_DEFAULT_IMAGE';
}
} else {
$this->data['og_image'] = 'FULL_PATH_TO_YOUR_DEFAULT_IMAGE';
}
]]></add>
</operation>
</file>
</modification>
Add in common/header.tpl <head> area:
Code: Select all
<?php if ($og_image) { ?>
<meta property="og:image" content="<?php echo $og_image; ?>" />
<?php } ?>
If we have an active product, its thumbnail is passed to Facebook. If the page is other than product/product, Facebook will use a default image.
Re: How to pass a variable to another controller's view?
Posted: Wed Mar 21, 2012 7:01 pm
by gifo
vaanbooch, thanks for code

I also had this problem.
Re: How to pass a variable to another controller's view?
Posted: Fri Mar 30, 2012 4:11 pm
by coen1234
I've installed your xml file and the header code as you discriped but i get the following error.
Code: Select all
Catchable fatal error: Argument 1 passed to VQModObject::__construct() must be an instance of DOMNode, null given, called in /home/Mysite/public_html/Mysite/vqmod/vqmod.php on line 580 and defined in /home/Mysite/public_html/Mysite/vqmod/vqmod.php on line 127
Can you please guide me how to do this?
I'm using OC 1.5.2.1
Re: How to pass a variable to another controller's view?
Posted: Fri Mar 30, 2012 9:40 pm
by vaanbooch
There were missing the obligatory <modification>, etc. fields.
I've corrected the code, you can try it now :-)
Re: How to pass a variable to another controller's view?
Posted: Tue Apr 17, 2012 4:52 pm
by vaanbooch
Meanwhile we have made an extended version, which provides:
- Sharing of each Product with it's corresponding Image, Title and Description
- Site Global Image, Title and Description
It can be downloaded here:
http://www.opencart.com/index.php?route ... order=DESC
Enjoy!