Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxrice/6a59f496cc8a2dfcff44 to your computer and use it in GitHub Desktop.
Save maxrice/6a59f496cc8a2dfcff44 to your computer and use it in GitHub Desktop.
WooCommerce Sage ERP Connector Customizations plugin
<?php
/**
* Plugin Name: WooCommerce Sage ERP Connector Customizations
* Plugin URI: https://github.com/skyverge/woocommerce-sage-erp-connector
* Description: This is a sample customization plugin for the WooCommerce Sage ERP Connector extension.
* Author: SkyVerge
* Author URI: http://www.skyverge.com
* Version: 1.0.0
* Text Domain: wc-sage-erp-connector-customizations
* Domain Path: /languages/
*
* Copyright: (c) 2013-2015 SkyVerge, Inc. ([email protected])
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package WC-Sage-ERP-Connector-Customizations
* @author SkyVerge
* @category Custom
* @copyright Copyright (c) 2013-2015, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* The WC_Sage_ERP_Connector_Customizations global object
* @name $wc_sage_erp_connector_customizations
* @global WC_Sage_ERP_Connector_Customizations $GLOBALS['wc_sage_erp_connector_customizations']
*/
$GLOBALS['wc_sage_erp_connector_customizations'] = new WC_Sage_ERP_Connector_Customizations();
/**
* # WooCommerce Sage ERP Connector Customizations Plugin Class
*
* ## Plugin Overview
*
* This plugin customizes the WooCommerce Sage ERP Connector extension by making use of the
* filters provided inside the connector extension to modify what fields are imported for customers and orders.
*
*/
class WC_Sage_ERP_Connector_Customizations {
/** plugin version number */
const VERSION = '1.0.0';
/** plugin text domain */
const TEXT_DOMAIN = 'wc-sage-erp-connector-customizations';
/**
* Adds the required filters
*
* @since 1.0.0
*/
public function __construct() {
// modify customer creation/update
add_filter( 'wc_sage_erp_connector_customer', array( $this, 'modify_customer' ), 10, 2 );
// modify sales order creation
add_filter( 'wc_sage_erp_connector_sales_order', array( $this, 'modify_sales_order' ), 10, 2 );
}
/**
* Modifies the customer data used for creation/updates
*
* @since 1.0.0
* @param object $customer the customer stdClass object that is passed to the Sage API
* @param object $order the WC_Order object
* @return object
*/
public function modify_customer( $customer, $order ) {
// handle foreign addresses for countries without two-letter state/county codes, to keep Sage happy
if ( ! in_array( $order->billing_country, array( 'US', 'CA' ) ) )
$order = $this->format_non_us_addresses( $order );
// normalize fields via strtolower + ucwords
$customer->CustomerName = ucwords( strtolower( $order->billing_company ) );
$customer->AddressLine2 = ucwords( strtolower( $order->billing_address_1 ) );
$customer->AddressLine3 = ucwords( strtolower( $order->billing_address_2 ) );
$customer->City = ucwords( strtolower( $order->billing_city ) );
$customer->State = $order->billing_state;
$customer->ZipCode = $order->billing_postcode;
$customer->CountryCode = ( 'US' === $order->billing_country ) ? 'USA' : '';
$customer->EmailAddress = $order->billing_email;
$customer->TelephoneNo = $order->billing_phone;
// format US phone numbers
if ( 'US' === $customer->CountryCode ) {
// remove non-digits
$customer->TelephoneNo = preg_replace( '[\D]', '', $customer->TelephoneNo );
// format to NNN-NNN-NNNN
$customer->TelephoneNo = preg_replace( '/([0-9]{3})([0-9]{3})([0-9]{4})/', '$1-$2-$3', $customer->TelephoneNo );
}
// Terms Code - Sage will set this to 00 if not provided
$customer->TermsCode = '01';
// Tax Schedule is required
$customer->TaxSchedule = 'NONTAX';
return $customer;
}
/**
* Modifies the sales order data used for updates
*
* @since 1.0.0
* @param object $sales_order the sales order stdClass object that is passed to the Sage API
* @param WC_Order $order the WC_Order object
* @return object
*/
public function modify_sales_order( $sales_order, $order ) {
// handle foreign addresses for countries without two-letter state/county codes, to keep Sage happy
if ( ! in_array( $order->shipping_country, array( 'US', 'CA' ) ) )
$order = $this->format_non_us_addresses( $order );
// normalize fields via strtolower + ucwords
$sales_order->ShipToName = ucwords( strtolower( $order->shipping_company ) );
$sales_order->ShipToAddress2 = ucwords( strtolower( $order->shipping_address_1 ) );
$sales_order->ShipToAddress3 = ucwords( strtolower( $order->shipping_address_2 ) );
$sales_order->ShipToCity = ucwords( strtolower( $order->shipping_city ) );
$sales_order->ShipToState = $order->shipping_state;
$sales_order->ShipToZipCode = $order->shipping_postcode;
$sales_order->ShipToCountryCode = ( 'US' === $order->shipping_country ) ? 'USA' : '';
// set additional info for the order
$sales_order->DepositAmt = $order->get_total();
$sales_order->DiscountAmt = $order->get_total_discount();
$sales_order->FreightAmt = $order->get_total_shipping();
$sales_order->OrderDate = date( 'Y-m-d', strtotime( $order->order_date ) );
//$sales_order->PaymentType = 'CHECK';
//$sales_order->CheckNumber = 'PAID';
// free-form field for adding additional information to the sales order
// $sales_order->SalesOrderComment
// terms code is set to '01' which means PAID
$sales_order->TermsCode = '01';
// WarehouseCode : 200 (defaults from MAS product record)
// OrderType defaults to 'S' (sale)
$sales_order->ConfirmTo = $order->billing_first_name . ' ' . $order->billing_last_name;
return $sales_order;
}
/**
* Formats non-US addresses to a valid Sage format
*
* @since 1.0
* @param object $order the order object
* @return object
*/
private function format_non_us_addresses( $order ) {
$order->billing_address_3 = '';
$order->shipping_address_3 = '';
if ( ! in_array( $order->billing_country, array( 'US', 'BR', 'CA' ) ) ) {
// join the state to the city
$order->billing_city = substr( $order->billing_city . ', ' . $order->billing_state, 0, 20 );
$order->shipping_city = substr( $order->shipping_city . ', ' . $order->shipping_state, 0, 20 );
// set the address 3 line to the full state
$order->billing_address_3 = $order->billing_state;
$order->shipping_address_3 = $order->shipping_state;
// set the state to the country code so it's 2 characters
$order->billing_state = $order->billing_country;
$order->shipping_state = $order->shipping_country;
}
return $order;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment