WordPress WooCommerce to CRM Integration

The aim was to build a WordPress plugin to collect user data at the WooCommerce checkout and send this data to a selected CRM system. This was my first attempt at a WordPress plugin development and also involved how WooCommerce integrates with WordPress. A great learning experience.

The plugin was developed for a specific project and is not something that can be installed and used without modification.

Overview

Create a plugin template from various examples, and the WordPress documentation.
Add an admin Settings section to store the the required UID and URL for the CRM system.
Processing function to collect the Order information required, and send it to the CRM.

 

/*

Plugin Name: Gracode WooCommerce CRM Integration

Plugin URI: https://gracode.com/

Description: Sends checkout data to selected CRM

Version: 0.0.1

Author: grahamcheetham

Author URI: https://gracode.com

Requires at least: 4.0

Tested up to: 4.2

License: GPLv2 or later

License URI: http://www.gnu.org/licenses/gpl-2.0.html

Text Domain: gracode-woocommerce-crm-integration

Domain Path: /languages/

*/

if ( ! defined( 'ABSPATH' ) ) {
   exit; // Exit if accessed directly
}

/**
 * Check if WooCommerce is active
 **/

if ( in_array( 'woocommerce/woocommerce.php', 
   apply_filters( 'active_plugins', 
   get_option( 'active_plugins' ) ) ) ) {

   /**
    * Localisation
    **/

   /**
    * WC_CRM_Integration class
    **/

   if ( ! class_exists( 'WC_CRM_Integration' ) ) {
      class WC_CRM_Integration {
         public function __construct() {
            // Hooks
            // Init settings
            $this->settings = array(
               array(
                  'name'     => __( 'CRM Integration', 'gracode-wc-crm-integration' ),
                  'type'     => 'title',
                  'desc'  => 'Allow Checkout data to be sent to selected CRM systems.',
                  'id'   => 'wc_gci_options'
               ),
               array(
                  'name'        => __( 'Service UID', 'gracode-wc-crm-integration' ),
                  'desc_tip'     => __( 'CRM Service UID - User Account ID', 'gracode-wc-crm-integration' ),
                  'id'      => 'wc_gci_service_uid',
                  'type'        => 'text',
                  'css'      => 'min-width:300px;',
                  'default'  => 'test',
               ),
               array(
                  'name'        => __( 'Service URL', 'gracode-wc-crm-integration' ),
                  'desc_tip'     => __( 'CRM Service URL - Web address to POST selected form data', 'gracode-wc-crm-integration' ),
                  'id'      => 'wc_gci_service_post_url',
                  'type'        => 'text',
                  'css'      => 'min-width:400px;',
                  'default'  => 'test url',
               ),
               array( 'type' => 'sectionend', 'id' => 'wc_gci_options' ),
            );

            // Default options
            add_option( 'wc_gci_service_uid', 'dummy uid' );
            add_option( 'wc_gci_service_post_url', 'dummy url' );

            // Admin
            // display settings

            add_action( 'woocommerce_settings_payment_gateways_options_after', array( $this, 'admin_settings' ), 20 );

            // save settings

            add_action( 'woocommerce_update_options_checkout', array( $this, 'save_admin_settings' ) );

            // processed order - send to CRM

            add_action( 'woocommerce_checkout_order_processed', array($this, 'checkout_order_processed' ) );

            }

           /*-----------------------------------------------------------------------------------*/

         /* Class Functions */

         /*-----------------------------------------------------------------------------------*/

         function admin_settings() {
                woocommerce_admin_fields( $this->settings );
         }

         function save_admin_settings() {
                woocommerce_update_options( $this->settings );
         }

         function checkout_order_processed($order_id, $checkout)
         {

            // get saved settings for destination CRM

            $setting_uid = get_option( 'wc_gci_service_uid', 'dummy uid' );
            $setting_url = get_option( 'wc_gci_service_post_url', 'dummy url' );

            // get order info
            $order = new WC_Order($order_id);
            $order_product = '';
            $order_items = $order->get_items();

            // get product(s)
            foreach ( $order_items as $item ) {
               $order_product .= $item['name'] . ',';
            }

            $order_product = rtrim($order_product, ',');

            // build post to crm

            $response = wp_remote_post( $setting_url, array(
               'method' => 'POST',
               'timeout' => 45,
               'redirection' => 5,
               'httpversion' => '1.0',
               'blocking' => true,
               'headers' => array(),
               'user-agent' =>  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36',
               'body' => array(
                  'firstname' => $order->billing_first_name,
                  'lastname' => $order->billing_last_name,
                  'email' => $order->billing_email,
                  'home_phone' => $order->billing_phone,
                  'company' => $order->billing_company,

                  // mystery data here

                  'uid' => $setting_uid,
                  'contact_id' => '',
                  'redirect' => 'http://if-needed.com',
                  'tags' => '',
                            'sequence' => '',
                            'owner_' => '1',
                            'afft_' => '',
                            'aff_' => '',
                            'sess_' => '',
                            'ref_' => '',
                            'own_' => '',
                            'oprid' => ''
                        ),

                        'cookies' => array()
                    )
                );

                if ( is_wp_error( $response ) ) {
                    $error_message = $response->get_error_message();
                } else {
                    $error_message = "okay";
                }

            }

      }

      $WC_CRM_Integration = new WC_CRM_Integration();

   }

}

Details

Line 37 begins by checking if WooCommerce is loaded.
Line 49 starts the definition of the WC_CRM_Integration class
The __construct function defines the settings title and parameters, from line 54.
Default options are created and written, lines 81 & 82.
Followed by a series of add_action() functions to display the settings, save the settings, and where to call the our function to process the the checkout data during processing.
Note: “woocommerce_checkout_order_processed” – I found it difficult to find a good explanation of the hooks available in WooCommerce and where it would be best to inject our function. So with a bit of trial and error and some diagnostic triggers I found found this works well. Function is called post checkout but before sending the user to the external payment page. In the later function the required order parameters can be recalled for our purpose.

The real action happens in checkout_order_processed($order_id, $checkout).
Recall the settings required, lines 118, 119.
Load the order and gather the names from the item(s), lines 122 – 129.
Line 135 sends the collected data by posting it the CRM web site for processing. This process will be CRM specific, and t
here were some tricks applied here to get it working for our case.

 

As this is my first plugin there were some challenges to sort through and there is no doubt I probably broke some rules along the way. Some of the documentation I found could be more helpful.

Let me know your experience in the comments.