Jump to content

Facebook OG meta image


Recommended Posts

Ok, this is what you would need to do. You have to override the front controller. Make an override for the preProcess class like this.

 

public function preProcess()
{
if (page =="product"){
$product = new Product($_GET['id_product'], false, intval($cookie->id_lang));
if (Validate::isLoadedObject($product))
	self::$smarty->assign('product', $product);

$images = $product->getImages(intval($cookie->id_lang));
foreach ($images AS $k => $image)
	if ($image['cover'])
	{
		$cover = $image;
		$cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
		$cover['id_image_only'] = intval($image['id_image']);
	}
	if (!isset($cover))
	$cover = array('id_image' => Language::getIsoById($cookie->id_lang).'-default', 'legend' => 'No picture', 'title' => 'No picture');
self::$smarty->assign('cover', $cover);
}
}

 

Then in your page put

 

if (page =="product")
<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'large_default')}">
{else}
<meta property="og:image" content="{$logo_url}">
{/if}

Link to comment
Share on other sites

opps, the if statement breaks it, this works, I tested it

public function preProcess()
{
 $product = new Product($_GET['id_product'], false, intval($cookie->id_lang));
   if (Validate::isLoadedObject($product))
    self::$smarty->assign('product', $product);
   $images = $product->getImages(intval($cookie->id_lang));
   foreach ($images AS $k => $image)
    if ($image['cover'])
    {
	    $cover = $image;
	    $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
	    $cover['id_image_only'] = intval($image['id_image']);
    }
    if (!isset($cover))
    $cover = array('id_image' => Language::getIsoById($cookie->id_lang).'-default', 'legend' => 'No picture', 'title' => 'No picture');
   self::$smarty->assign('cover', $cover);
}

Link to comment
Share on other sites

  • 4 months later...
  • 4 weeks later...

The link you posted will not make the product image be available, it will use your logo for the product picture.

 

The one I posted shows you how to override the front controller class and add to the preprocess class so that the product image is retrieved during the load of the header of the page.

Link to comment
Share on other sites

The link you posted will not make the product image be available, it will use your logo for the product picture.

 

It tells you how to change it from the logo to a product image! (You only glanced at it didn't you ;) )

 

The one I posted shows you how to override the front controller class and add to the preprocess class so that the product image is retrieved during the load of the header of the page.

 

Your reply to the forum hadn't actually turned up when I sent my second message. I'm too tired to actually follow the link you posted now. Can you answer my question from my previous comment though? The stuff you originally posted a couple of years ago - which file is it in?

Link to comment
Share on other sites

  • 3 months later...

The link you posted will not make the product image be available, it will use your logo for the product picture.

 

The one I posted shows you how to override the front controller class and add to the preprocess class so that the product image is retrieved during the load of the header of the page.

 

 

Hello Dh42, 

 

this solution posted above is not working for PS 1.5.3.1:(

 

What I have done is replaced the existing preprocess code in frontcontroller.php with the code shown above. And place the og tag code in header.tpl under header hook. 

 

Is there anything that I missed?

 

Im getting server error. 

 

Thanks for any suggestion. 

 

PrestaFanBoy

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

I just checked it out and I see what you mean. I don't have any installations of 1.5.3.1 any where, so I tested this on 1.5.4.1 and it should fix you up. I added this to the initContent of the front controller

If(isset($_GET['id_product'])){
    $product = new Product($_GET['id_product'], false, intval($this->context->language->id));
    if (Validate::isLoadedObject($product))
        self::$smarty->assign('product', $product);
 
    $images = $product->getImages(intval($this->context->language->id));
    foreach ($images AS $k => $image)
        if ($image['cover'])
        {
            $cover = $image;
            $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
            $cover['id_image_only'] = intval($image['id_image']);
        [spam-filter]
        if (!isset($cover))
        $cover = array('id_image' => Language::getIsoById($this->context->language->id).'_default', 'legend' => 'No picture', 'title' => 'No picture');
    self::$smarty->assign('cover', $cover);

Then in the code you put in the theme file I changed image to 

<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'large_default')}">

If you are not using large_default, you will have to change the image from that. 

  • Like 3
Link to comment
Share on other sites

I just checked it out and I see what you mean. I don't have any installations of 1.5.3.1 any where, so I tested this on 1.5.4.1 and it should fix you up. I added this to the initContent of the front controller

If(isset($_GET['id_product'])){
    $product = new Product($_GET['id_product'], false, intval($this->context->language->id));
    if (Validate::isLoadedObject($product))
        self::$smarty->assign('product', $product);
 
    $images = $product->getImages(intval($this->context->language->id));
    foreach ($images AS $k => $image)
        if ($image['cover'])
        {
            $cover = $image;
            $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
            $cover['id_image_only'] = intval($image['id_image']);
        [spam-filter]
        if (!isset($cover))
        $cover = array('id_image' => Language::getIsoById($this->context->language->id).'_default', 'legend' => 'No picture', 'title' => 'No picture');
    self::$smarty->assign('cover', $cover);

Then in the code you put in the theme file I changed image to 

<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'large_default')}">

If you are not using large_default, you will have to change the image from that. 

 

 

Hello Dh42, 

 

Your fix seems to be working. Now im able to see associated images sharing to Facebook. 

 

Thank you very much..:)

 

PrestaFanBoy

Link to comment
Share on other sites

  • 2 months later...

I just checked it out and I see what you mean. I don't have any installations of 1.5.3.1 any where, so I tested this on 1.5.4.1 and it should fix you up. I added this to the initContent of the front controller

If(isset($_GET['id_product'])){
    $product = new Product($_GET['id_product'], false, intval($this->context->language->id));
    if (Validate::isLoadedObject($product))
        self::$smarty->assign('product', $product);
 
    $images = $product->getImages(intval($this->context->language->id));
    foreach ($images AS $k => $image)
        if ($image['cover'])
        {
            $cover = $image;
            $cover['id_image'] = intval($product->id).'-'.$cover['id_image'];
            $cover['id_image_only'] = intval($image['id_image']);
        [spam-filter]
        if (!isset($cover))
        $cover = array('id_image' => Language::getIsoById($this->context->language->id).'_default', 'legend' => 'No picture', 'title' => 'No picture');
    self::$smarty->assign('cover', $cover);

Then in the code you put in the theme file I changed image to 

<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'large_default')}">

If you are not using large_default, you will have to change the image from that. 

Hi,

where can i add this script? please help thank you

Link to comment
Share on other sites

first part at

/classes/controller/FrontController.php

near 413 line

 

second part at

/themes/your_theme/header.tpl

 

 

My changes for header.tpl

{if $page_name == 'product'}
<meta property="og:type" content="product" />
<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'large_default')}">
{/if}
{if $page_name != 'product'} 
<meta property="og:type" content="website" />
<meta property="og:image" content="{$img_ps_dir}logo-1.jpg">
{/if}

 

PS: Thanks a lot for your help!

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

  • 1 year later...

first part at

/classes/controller/FrontController.php

near 413 line

 

second part at

/themes/your_theme/header.tpl

 

 

My changes for header.tpl

{if $page_name == 'product'}
<meta property="og:type" content="product" />
<meta property="og:image" content="{$link->getImageLink($product->link_rewrite, $cover.id_image, 'large_default')}">
{/if}
{if $page_name != 'product'} 
<meta property="og:type" content="website" />
<meta property="og:image" content="{$img_ps_dir}logo-1.jpg">
{/if}

 

PS: Thanks a lot for your help!

 

i would to use different size for facebookshares.its recommended 600x315 size, as default its using large_default images. i will set the size specially for it. is there possible to use as named facebook_default ?

 

Regards

Matt

Link to comment
Share on other sites

×
×
  • Create New...