如何动态设置facebook元标记?

I have an opencart site, and I'm trying to configure facebook share options of my products.

Since everything is loaded as a separate module, I can't set the facebook meta tags like this (header.tpl):

<meta property="og:description" content="<?php echo $description; ?>" />

Because the $description doesn't exist while the header module is loading.It is created in the controller of product module. So I tried to change the content value dynamically (product.tpl):

$("meta[property='og:description']").attr('content','<?php echo $description; ?>');

And it worked, I can see that the value is changed (in the page source), then I debugged my page but I couldn't get the value.. I think I know the reason, I have to set the value before the page load but I'm not sure how can I do that.. do you have any idea ?

You could use the Document class to add those facebook tags (as many as you want). Just add two extra methods setFacebookDescription and getFacebookDescription, so you have to add the followings:

<?php
class Document {
    private $facebook_description;

    public function getFacebookDescription() {
        return $this->facebook_description;
    }

    public function setFacebookDescription($facebook_description) {
        $this->facebook_description = $facebook_description;
    }
}

On each controller, you will find at the end of each method, a call which loads the header of Opencart, something like this $data['header'] = $this->load->controller('common/header'); (example). Note that it might differ from yours, it depends on Opencart version.

Now, in header.php controller you add:

<?php
class ControllerCommonHeader extends Controller {

    public function index() {
        $data['facebook_description'] = $this->document->getFacebookDescription();
    }
}

this will get the facebook_description variable and pass it to the view header.tpl. Next, add the facebook tags between your <head> tags in header.tpl file:

<!DOCTYPE html>
<head>
    <?php if ($facebook_description != '') { ?><meta property="og:description" content="<?php echo $facebook_description; ?>" /><?php } ?>
</head>

Finally, you can set facebook_description in each controller, by calling $this->document->setFacebookDescription('my description');.

Example: in product.php controller you add

<?php
class ControllerProductProduct extends Controller {

    public function index() {

        // code...
        $this->document->setTitle($product_info['meta_title']);
        $this->document->setDescription($product_info['meta_description']);

        $this->document->setFacebookDescription($product_info['meta_description']);

        // the rest of the code...
    }
}

here you set the $product_info['meta_description'] as facebook description tag, however you could also use $product_info['name'] or other variable.

Final note: you can change all the Opencart system classes with the vqmod.