neděle 3. května 2015

Update Facebook Sharer Cache automatically on Page Load

So I have this problem on my one of my clients website. The Facebook cache was not updateed correctly. And no images was loaded. I know that in every 24 hour the cache is updated. But I need this to be done automatically on page load now. This solution is good for WordPress and WooCommerce too. Add this to your functions.php file and the Sharer Cache will be updated on page load.
add_action( 'wp_footer', 'fb_sharer_updater', 5 );

if(!function_exists('fb_sharer_updater')) {
    function fb_sharer_updater() {
        global $post;
            
            echo '<script type="text/javascript">
    jQuery.post(
    "https://graph.facebook.com",
    {
     id: "'.  get_permalink() .'",
     scrape: true
    },
    function(response){
     console.log(response);
    }
   );';
            echo '</script>';
            
        
    }
}
You need to wait approx 5seconds, then the new scrape info is added to FB cache. Open Developer Console and you will see additional information:
Object {url: "http://theblackcat.sk/produkt/plazove-saty-texas-s-5995/", type: "article", title: "Plážové šaty Texas S-5995 - TheBlackCat.sk", locale: Object, image: Array[1]…}
description: "Ľahučké plážové šaty so vzorom, s viazaním na boku. Plážové šaty odporúčame ku všetkým bikinám Texas 2015 ako doplnka."
id: "1532012490185775"
image: Array[1]
locale: Object
site_name: "TheBlackCat.sk"
title: "Plážové šaty Texas S-5995 - TheBlackCat.sk"
type: "article"
updated_time: "2015-05-03T14:23:15+0000"
url: "http://theblackcat.sk/produkt/plazove-saty-texas-s-5995/"
__proto__: Object

sobota 31. ledna 2015

Adding TinyCon library to WooCommerce - Show products in cart count on the favicon

I just came across with a solution for implementing TinyCon with WooCommerce. I use this super-easy, leightweight library to display cart quantity on the favicon.

You need to edit file footer.php in your theme, you find this in most cases:

    /wp-content/themes/xxxxxx/footer.php

What you need to add first is the TinyCon JS library. Download it from [Github][1].

Then you need to call this library and add the cart quantity to display it on the favicon. Add this code before the </body> tag.

My code with explanation:

 <?php global $woocommerce;  //you need this for het the cart quantity
    $my_cart_count = $woocommerce->cart->cart_contents_count; //get the quantity
    ?>
     <script src="<?php bloginfo('stylesheet_directory'); ?>/includes/js/tinycon.min.js" type="text/javascript"></script> //call the TinyCon library
     <script type="text/javascript">
    Tinycon.setOptions({
        width: 7,
        height: 9,
        font: '10px arial',
        colour: '#ffffff',
        background: '#e01c1f',
        fallback: true
    });
     </script>
    <?php if ($my_cart_count > 0) :             //if cart has products in it display bubble by default.
    ?>
    <script type="text/javascript">
   
    Tinycon.setBubble(<?php echo $my_cart_count; ?>);
   
    </script>
    <?php
    endif;
    ?>
    // this part is needed if you use AJAX add to cart in WooCommerce, this will ensure when if you click on AJAX add to cart button the bubble is updated correctly.
    <script type="text/javascript">
     var darab =  <?php echo $my_cart_count; ?>;
     
     jQuery('.add_to_cart_button').bind('click',function() {
       
     darab = darab + 1 ;
     
     Tinycon.setBubble(darab); 
    });
   
    </script>


  [1]: https://github.com/tommoor/tinycon

čtvrtek 15. ledna 2015

WooCommerce + WordPress new line issue with Mandrill

As I stated in previous topic after installing Mandrill my WordPress text/plain emails doesnt contain new line breaks, because they are coded with /n/r code.

There is an option in settings of Mandrill, that you can:
Replace all line feeds ("\n") by <br/>in the message body?

But if you enable this the WooCommerce emails are going to be ugly, because they are already HTML emails, not just plain text emails, which are sended out by WordPress by default.

To solve the issue you need to use the mandrill_playload hook which is just fired before an email sents out.
add_filter('mandrill_payload', 'customFilterName');
 More about this filter: http://blog.mandrill.com/own-your-wordpress-email-with-mandrill.html

What you need to do is select the type of emails with tags which are plain/text emails only.

You need to get the tags from your Mandrill admin.


As you can see, these are the tags in my case.

The wp_WC_Email->send work just fine, because this is the tag for WooCommerce store emails, these are coded with text/html.

What I needed to add is: wp_userpro_mail, wp_template-contact.php and wp_wp_new_user_notification, because these are encoded only in text/plain.

To bypass this and insert correct line breaks tags you just to need to add the following to your functions.php in your theme.

 /*************************************************************************/
 /*WPMandrill playload, add <br> to emails*/
 /*************************************************************************/
 function wd_mandrill_woo_order( $message ) {
      $tags = $message['tags']['automatic'];
   if ( (in_array( 'wp_template-contact.php', $tags )) || (in_array( 'wp_userpro_mail', $tags )) || (in_array( 'wp_wp_new_user_notification', $tags )) ) {
     $message['html'] = nl2br( $message['html'] );
   }
   return $message;
 }
 add_filter( 'mandrill_payload', 'wd_mandrill_woo_order' );

The key functions here is the nl2br which just returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r).

After this little code snippet every system emails from WordPress are correctly displayed :)))

Maybe someone is facing the same issue and I helped him with this :).

WooCommerce emails ending up in SPAM - Enable wpMandrill in WordPress and WooCommerce

So, we have a VPS server with e-mail server installed.

By default e-mails from Wordpress and WooCommerce are send out with wp_mail() function. We have problems with that because our store emails are ended up in SPAM folder.

I have read and troubleshoot couple of hours. Added DKIM and SPF to domain, etc.

But I think this is never gonna get end.

Finally I have found service SMTP server Mandrill, which is developed and maintained by folks from MailChimp :). This thing is amazing, you can track openings, clicks, everything... what was the content of the email. It was delivered or not?

For installing the plugin you just need to download wpMandrill plugin within Wordpress or from WP plugin page.

After install you just need to go to: Settings -> Mandrill 
There is a good guide on this page: https://connect.mailchimp.com/how-to/53

  1. First you are asked to create an API key. Register and visit this page: https://mandrillapp.com/settings
  2. Click on + New API key button
  3. Then setup the plugin.
After installing everything just works out of the box. Our store emails are now correctly received in Gmail INBOX and not in the SPAM folder.

Mandrill is able to send 12 000 emails/month for free, but the pricing above that is also good :).

If you ever face the issue that your WooCommerce store emails are ended up in SPAM, you aren't able to get it work as me install Mandrill, take a seat, and enjoy your work! :)

One issue what I have found and going to cover in other topic is this:
WordPress standard emails (password reset, etc.) are plain-text-ish with /n/r code. They look better when we check "Replace all line feeds ("\n") by
in the message body"
Woocommerce sends nice HTML. They look worse when we check "Replace all line feeds ("\n") by
in the message body"