Forum

Thread tagged as: Question

Social cards for product sharing

What is the best method to pass product information into the OG cards for facebook and twitter etc? I've started out trying the method in the docs for perch content but not sure if it works the same for the shop:

  # get the post
  $post = perch_shop_product(array(
        'template'      => 'products/product_item.html',
        'filter'        => 'slug',
        'match'         => 'eq',
        'value'         => perch_get('slug'),
        'skip-template' => 'true',
        'return-html'   => 'true',
    ));

  # set up the variables
  $title       = $post['title'];
  $description = $post['0']['description'];
  $fbimage     = $post['0']['imageCarousel'];
  $domain        = 'https://'.$_SERVER["HTTP_HOST"];
  $url           = $domain.$_SERVER["REQUEST_URI"];

  perch_layout('global.head', array (
      'description'    => $description,
      'og_title'       => $title,
      'og_type'        => 'article',
      'sharing_image'  => $domain . $fbimage,
      'url'            => $url
    ));

  perch_layout('shop.header', array(
      'shop' => true //Page indicator for nav
    ));

?>

Currently just getting the following warning: Warning: PDO::quote() expects parameter 1 to be string, array given in

Rob Saunders

Rob Saunders 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you want something more akin to:

  $product = perch_shop_product(perch_get('slug'),[
        'skip-template' => true,
        'return-html'   => true,
    ]);

  # set up the variables
  $title       = $product[0]['title'];
  $description = $product[0]['description'];
  $fbimage     = $product[0]['imageCarousel'];
  $domain      = 'https://'.$_SERVER["HTTP_HOST"];
  $url         = $domain.$_SERVER["REQUEST_URI"];

Perfect thanks!