Daniel
Daniel

Reputation: 716

Facebook sharing PHP - check if has been shared

My client has a Wordpress website using the WooCommerce e-commerce module and would like the option to be able to share one product in his cart.

So on the cart page I have the following code:

<div class="fb-share-button" data-href="link-to-page-which-will-be-shared" data-width="300" data-type="button"></div>

And in header.php I have this:

<div id="fb-root"></div>
<script>
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

Now it displays a nice little share button - what I would like to know is if the sharing tool return a variable if the user has shared the page - if so how can I access this?

Could I otherwise add something into the JavaScript which would check if the user has shared the page?

After the user has shared the page a discount will be applied to his or her cart and to do this I need to be able to check if the user has shared his or her product.

Thanks in advance.

Upvotes: 0

Views: 2941

Answers (1)

Daniel
Daniel

Reputation: 716

I asked this question over a year ago and figured it out myself using a form. I thought I would share how I did this with everyone.

First off the javascript function for the sharing:

function fshare(e){
    e.preventDefault();
    FB.init({appId: "APP_ID_HERE", status: true, cookie: true});
    var share = {
      method: 'feed',
      link: 'http://domain.com/link-to-page/',
      picture: 'http://domain.com/image/me.jpg',
      name: 'Name of item',
      caption: 'domain.com',
      description: 'Check out my page!'
    };

    FB.ui(share, function(response){
        if (typeof response == "undefined") {
            // do nothing.
        } else {
            // submit facebook sharing form
            document.forms["facebookshared"].submit();
        }
    });
}

Here we submit a form once the item has been shared. Here is an example of an HTML form for this:

<form name="facebookshared" method="POST">
    <input type="hidden" name="shared" value="true" />
</form>

Here is the PHP code that checks if the form has been submitted:

<?php
// if the facebook shared form is posted proceed
if ($_POST['shared']) {
    // perform whatever action you wish to do in between these brackets
}
?>

Upvotes: 4

Related Questions