Plugin Directory

Changeset 2489733


Ignore:
Timestamp:
03/08/2021 04:49:47 PM (5 years ago)
Author:
Easify
Message:

Version 4.26

Location:
easify-server-woocommerce/trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • easify-server-woocommerce/trunk/easify.php

    r2473139 r2489733  
    55 * Plugin URI: http://www.easify.co.uk/wordpress/
    66 * Description: Connects Easify Business Management, EPOS (Electronic Point of Sale) and invoicing software to your WooCommerce enabled WordPress website. Allowing you to keep your online and offline shop's orders and stock control synchronised.
    7  * Version: 4.25
     7 * Version: 4.26
    88 * License: GPL2
    99 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    1111 * Author URI: http://www.easify.co.uk/
    1212 * Requires at least: 4.0
    13  * Tested up to: 5.6
     13 * Tested up to: 5.7
    1414 * WC tested up to: 5.0
     15 *
     16 * @package easify-woocommerce-connector
     17 * @link    https://www.easify.co.uk
    1518 */
    1619
     
    2124 * as published by the Free Software Foundation; either version 2
    2225 * of the License, or (at your option) any later version.
    23  * 
     26 *
    2427 * This program is distributed in the hope that it will be useful,
    2528 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2629 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2730 * GNU General Public License for more details.
    28  * 
     31 *
    2932 * You should have received a copy of the GNU General Public License
    3033 * along with this program; if not, write to the Free Software
     
    3235 */
    3336
    34 // Exit if accessed directly
    35 if (!defined('ABSPATH')) {
    36     exit;
     37// Exit if accessed directly.
     38if ( ! defined( 'ABSPATH' ) ) {
     39    exit;
    3740}
    3841
    3942// Logging can be enabled either in the Easify Plugin Options (via the database)
    4043// or in the easify-generic-constants.php file.
    41 if (!defined('EASIFY_LOGGING_BY_DB_FLAG')) {
    42     if (!empty(get_option('easify_options_logging')))
    43     {
    44         define('EASIFY_LOGGING_BY_DB_FLAG', get_option('easify_options_logging')['easify_logging_enabled']);
    45     }   
    46     else
    47     {
    48         define('EASIFY_LOGGING_BY_DB_FLAG', false);     
    49     }
     44if ( ! defined( 'EASIFY_LOGGING_BY_DB_FLAG' ) ) {
     45    if ( ! empty( get_option( 'easify_options_logging' ) ) ) {
     46        define( 'EASIFY_LOGGING_BY_DB_FLAG', get_option( 'easify_options_logging' )['easify_logging_enabled'] );
     47    } else {
     48        define( 'EASIFY_LOGGING_BY_DB_FLAG', false );
     49    }
    5050}
    5151
    52 if (!defined('PLUGIN_ROOT_PATH')) {
    53     define('PLUGIN_ROOT_PATH', __FILE__);
     52if ( ! defined( 'PLUGIN_ROOT_PATH' ) ) {
     53    define( 'PLUGIN_ROOT_PATH', __FILE__ );
    5454}
    5555
    56 // Includes
    57 require_once( 'includes/class-easify-generic-logging.php' );
    58 require_once( 'includes/class-easify-wc-activation.php' );
    59 require_once( 'includes/class-easify-wc-plugin.php' );
    60 require_once( 'includes/class-easify-wc-plugin-settings-page.php' );
    61 require_once( 'includes/easify-generic-constants.php' );
     56// Includes.
     57require_once 'includes/class-easify-generic-logging.php';
     58require_once 'includes/class-easify-wc-activation.php';
     59require_once 'includes/class-easify-wc-plugin.php';
     60require_once 'includes/class-easify-wc-plugin-settings-page.php';
     61require_once 'includes/easify-generic-constants.php';
    6262
    63 // Wire up plugin activation class
     63// Wire up plugin activation class.
    6464$ewci = new Easify_WC_Activation();
    6565
    66 // Initialise the plugin for action
     66// Initialise the plugin for action.
    6767$ewcc = new Easify_WC_Plugin();
    6868
    69 // Initialise settings if logged in user is admin
    70 if (is_admin()) {
    71     $ewcsp = new Easify_WC__Plugin_Settings_Page();
     69// Initialise settings if logged in user is admin.
     70if ( is_admin() ) {
     71    $ewcsp = new Easify_WC__Plugin_Settings_Page();
    7272}
    7373?>
  • easify-server-woocommerce/trunk/includes/class-easify-generic-easify-order-model.php

    r1632274 r2489733  
    66 * as published by the Free Software Foundation; either version 2
    77 * of the License, or (at your option) any later version.
    8  * 
     8 *
    99 * This program is distributed in the hope that it will be useful,
    1010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1111 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1212 * GNU General Public License for more details.
    13  * 
     13 *
    1414 * You should have received a copy of the GNU General Public License
    1515 * along with this program; if not, write to the Free Software
     
    1919/**
    2020 * Easify_Generic_Easify_Order_Model class.
    21  * 
    22  * The Easify_Generic_Easify_Order_Model encapsulates the data required to 
     21 *
     22 * The Easify_Generic_Easify_Order_Model encapsulates the data required to
    2323 * make up an Easify Order. You would create an instance of this class,
    24  * populate it with the details of the order you want to send to your Easify 
     24 * populate it with the details of the order you want to send to your Easify
    2525 * Server, and pass it to an instance of the Easify_Generic_Easify_Cloud_Api
    2626 * class which will take care of routing the order to the Easify Server
    27  * associated with the credentials supplied to the 
     27 * associated with the credentials supplied to the
    2828 * Easify_Generic_Easify_Cloud_Api class.
    2929 *
     30 * NOTE: You need to make sure the names of the properties match the property
     31 * names in the Easify Model, as they are mapped by name within the Easify
     32 * Cloud API Server. If you "tidy up" the names according to WordPress
     33 * coding standards e.g. rename $StatusId to $status_id the code will
     34 * break.
     35 *
    3036 * @class       Easify_Generic_Easify_Order_Model
    31  * @version     4.0
     37 * @version     4.26
    3238 * @package     easify-woocommerce-connector
    3339 * @author      Easify
    3440 */
    3541class Easify_Generic_Easify_Order_Model{
    36     // Composed classes
    37     public $Customer; // Easify_Order_Customer
    38     public $OrderDetails; // Array of Easify_Order_Order_Details
    39     public $Payments; // Array of Easify_Order_Payments
    40    
     42    // Composed classes.
     43    public $Customer; // Easify_Order_Customer.
     44    public $OrderDetails; // Array of Easify_Order_Order_Details.
     45    public $Payments; // Array of Easify_Order_Payments.
     46
    4147    // Order class variables
    4248    public $ExtOrderNo;
     
    4854    public $Invoiced;
    4955    public $DateInvoiced;
    50     public $Comments; // Required field
     56    public $Comments; // Required field.
    5157    public $Notes;
    5258    public $DateOrdered;
     
    6571    public $OrderType;
    6672    public $PaymentTermsId;
    67    
    68    
    69     public function __construct() {
    70         $this->Customer = new Easify_Order_Customer();
    71         $this->OrderDetails = array();
    72         $this->Payments = array();               
    73     }
     73
     74    /**
     75     * The total value of the order excluding tax. If null, net total
     76     * will be calculated in Easify Cloud API from the sum of the
     77     * order line items.
     78     *
     79     * @var float $NetTotal - Added in plugin V4.26.
     80     */
     81    public $NetTotal;
     82
     83    /**
     84     * The total tax value of the order. If null, tax total
     85     * will be calculated in Easify Cloud API from the the
     86     * order line items.
     87     *
     88     * @var float $TaxTotal - Added in plugin V4.26.
     89     */
     90    public $TaxTotal;
     91
     92    /**
     93     * The gross (net + tax) value of the order. If null, Gross total
     94     * will be calculated in Easify Cloud API from the the
     95     * order line items.
     96     *
     97     * @var float $GrossTotal - Added in plugin V4.26.
     98     */
     99    public $GrossTotal;
     100
     101    /**
     102     * The gross fixed discount to be applied to the order.
     103     *
     104     * @var float $DiscountFixedGross - Added in plugin V4.26.
     105     */
     106    public $DiscountFixedGross;
     107
     108    /**
     109     * The gross percentage discount to be applied to the order.
     110     *
     111     * @var float $DiscountPercentageGross - Added in plugin V4.26.
     112     */
     113    public $DiscountPercentageGross;
     114
     115    public function __construct() {
     116        $this->Customer     = new Easify_Order_Customer();
     117        $this->OrderDetails = array();
     118        $this->Payments     = array();
     119    }
    74120}
    75121
    76     class Easify_Order_Customer{
    77         public $ExtCustomerId;
    78         public $CompanyName; // Customer must have CompanyName, FirstName or Surname
    79         public $Title;
    80         public $FirstName; // Customer must have CompanyName, FirstName or Surname
    81         public $Surname; // Customer must have CompanyName, FirstName or Surname
    82         public $JobTitle;
    83         public $Address1;
    84         public $Address2;
    85         public $Address3;
    86         public $Town;
    87         public $County;
    88         public $Postcode;
    89         public $Country;
    90         public $HomeTel;
    91         public $Email;
    92         public $DeliveryFirstName;
    93         public $DeliverySurname;
    94         public $DeliveryCompanyName;
    95         public $DeliveryAddress1;
    96         public $DeliveryAddress2;
    97         public $DeliveryAddress3;
    98         public $DeliveryTown;
    99         public $DeliveryCounty;
    100         public $DeliveryPostcode;
    101         public $DeliveryCountry;
    102         public $DeliveryTel;
    103         public $DeliveryEmail;
    104         public $SubscribeToNewsletter;
    105         public $TradeAccount;
    106         public $CreditLimit;
    107         public $CustomerTypeId;
    108         public $PaymentTermsId;
    109         public $RelationshipId;   
    110     }
    111    
    112     class Easify_Order_Order_Details{
    113         public $Sku; // Required field
    114         public $Qty;
    115         public $Price;
    116         public $Comments;
    117         public $TaxRate;
    118         public $TaxId;
    119         public $Spare;
    120         public $ExtParentId;
    121         public $ExtOrderDetailsId;
    122         public $ExtOrderNo;
    123         public $AutoAllocateStock;       
    124     }
    125    
    126     class Easify_Order_Payments{
    127         public $PaymentDate;
    128         public $PaymentAccountId;
    129         public $TransactionRef;
    130         public $PaymentMethodId;
    131         public $PaymentTypeId;
    132         public $ExtOrderNo;
    133         public $Comments;
    134         public $Amount;       
    135     }
    136    
    137    
    138    
     122class Easify_Order_Customer{
     123    public $ExtCustomerId;
     124    public $CompanyName; // Customer must have CompanyName, FirstName or Surname.
     125    public $Title;
     126    public $FirstName; // Customer must have CompanyName, FirstName or Surname.
     127    public $Surname; // Customer must have CompanyName, FirstName or Surname.
     128    public $JobTitle;
     129    public $Address1;
     130    public $Address2;
     131    public $Address3;
     132    public $Town;
     133    public $County;
     134    public $Postcode;
     135    public $Country;
     136    public $HomeTel;
     137    public $Email;
     138    public $DeliveryFirstName;
     139    public $DeliverySurname;
     140    public $DeliveryCompanyName;
     141    public $DeliveryAddress1;
     142    public $DeliveryAddress2;
     143    public $DeliveryAddress3;
     144    public $DeliveryTown;
     145    public $DeliveryCounty;
     146    public $DeliveryPostcode;
     147    public $DeliveryCountry;
     148    public $DeliveryTel;
     149    public $DeliveryEmail;
     150    public $SubscribeToNewsletter;
     151    public $TradeAccount;
     152    public $CreditLimit;
     153    public $CustomerTypeId;
     154    public $PaymentTermsId;
     155    public $RelationshipId;
     156}
     157
     158class Easify_Order_Order_Details{
     159    public $Sku; // Required field.
     160    public $Qty;
     161    public $Price;
     162    public $Comments;
     163    public $TaxRate;
     164    public $TaxId;
     165    public $Spare;
     166    public $ExtParentId;
     167    public $ExtOrderDetailsId;
     168    public $ExtOrderNo;
     169    public $AutoAllocateStock;
     170}
     171
     172class Easify_Order_Payments{
     173    public $PaymentDate;
     174    public $PaymentAccountId;
     175    public $TransactionRef;
     176    public $PaymentMethodId;
     177    public $PaymentTypeId;
     178    public $ExtOrderNo;
     179    public $Comments;
     180    public $Amount;
     181}
     182
    139183?>
  • easify-server-woocommerce/trunk/includes/class-easify-generic-logging.php

    r1632274 r2489733  
    66 * as published by the Free Software Foundation; either version 2
    77 * of the License, or (at your option) any later version.
    8  * 
     8 *
    99 * This program is distributed in the hope that it will be useful,
    1010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1111 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1212 * GNU General Public License for more details.
    13  * 
     13 *
    1414 * You should have received a copy of the GNU General Public License
    1515 * along with this program; if not, write to the Free Software
     
    1919/**
    2020 * Contains logging functionality for use with Easify Plugin Code
    21  * 
     21 *
    2222 * This is a static class and can be used anywhere that it is in scope without
    2323 * needing to be instantiated.
    24  * 
     24 *
    2525 * @class       Easify_Logging
    26  * @version     4.0
     26 * @version     4.26
    2727 * @package     easify-woocommerce-connector
    28  * @author      Easify 
     28 * @author      Easify
    2929 */
    3030class Easify_Logging {
    3131
    32     /**
    33      * A static logging method that you can use anywhere that includes this file
    34      * without having to instantiate the class.
    35      *
    36      * Usage: Easify_Logging::Log("Hello, world!");
    37      *
    38      * @param type $text - the text to be logged. If $text is an array it is
    39      * rendered with print_r()
    40      * @return type void
    41      */
    42     public static function Log($text) {
    43        
    44         // Can't guarantee EASIFY_LOGGING_BY_DB_FLAG has been set handle case
    45         // where not defined.
    46         $database_debug_flag = false;       
    47         if (defined('EASIFY_LOGGING_BY_DB_FLAG')) {
    48             $database_debug_flag = EASIFY_LOGGING_BY_DB_FLAG;
    49         }
    50                
    51         if (!$database_debug_flag && !EASIFY_LOGGING) {
    52             return;
    53         }
     32    /**
     33     * A static logging method that you can use anywhere that includes this file
     34     * without having to instantiate the class.
     35     *
     36     * Usage: Easify_Logging::Log("Hello, world!");
     37     *
     38     * @param object|string $text - the text to be logged. If $text is an array it is
     39     * rendered with print_r().
     40     */
     41    public static function Log( $text ) {
     42        // Can't guarantee EASIFY_LOGGING_BY_DB_FLAG has been set handle case
     43        // where not defined.
     44        $database_debug_flag = false;
     45        if ( defined( 'EASIFY_LOGGING_BY_DB_FLAG' ) ) {
     46            $database_debug_flag = EASIFY_LOGGING_BY_DB_FLAG;
     47        }
    5448
    55         // write to log file in the following format: 17-12-2012 10:15:10:000000 - $text \n
    56         $LogFile = fopen(dirname(dirname(__FILE__)) . '/logs/easify_log.txt', 'a');
     49        if ( ! $database_debug_flag && ! EASIFY_LOGGING ) {
     50            return;
     51        }
    5752
    58         if (is_array($text)) {
    59             $text = print_r($text, true);
    60         }
     53        // write to log file in the following format: 17-12-2012 10:15:10:000000 - $text \n.
     54        $LogFile = fopen( dirname( dirname( __FILE__ ) ) . '/logs/easify_log.txt', 'a' );
    6155
    62         fwrite($LogFile, date('d-m-y H:i:s') . substr((string) microtime(), 1, 6) . ' - ' . $text . "\n");
    63         fclose($LogFile);
    64     }
     56        if ( is_array( $text ) ) {
     57            $text  = print_r( $text, true );
     58            $text .= ' was array';
     59        }
     60
     61        fwrite( $LogFile, date( 'd-m-y H:i:s' ) . substr( (string) microtime(), 1, 6 ) . ' - ' . $text . "\n" );
     62        fclose( $LogFile );
     63    }
     64
     65    /**
     66     * A static logging method that you can use anywhere that includes this file
     67     * without having to instantiate the class.
     68     *
     69     * Usage: Easify_Logging::Log($myArray);
     70     *
     71     * @param object|string $text - always rendered with print_r().
     72     */
     73    public static function Log_r( $text ) {
     74        // Can't guarantee EASIFY_LOGGING_BY_DB_FLAG has been set handle case
     75        // where not defined.
     76        $database_debug_flag = false;
     77        if ( defined( 'EASIFY_LOGGING_BY_DB_FLAG' ) ) {
     78            $database_debug_flag = EASIFY_LOGGING_BY_DB_FLAG;
     79        }
     80
     81        if ( ! $database_debug_flag && ! EASIFY_LOGGING ) {
     82            return;
     83        }
     84
     85        // write to log file in the following format: 17-12-2012 10:15:10:000000 - $text \n
     86        $LogFile = fopen( dirname( dirname( __FILE__ ) ) . '/logs/easify_log.txt', 'a' );
     87
     88        $text = print_r( $text, true );
     89
     90        fwrite( $LogFile, date( 'd-m-y H:i:s' ) . substr( (string) microtime(), 1, 6 ) . ' - ' . $text . "\n" );
     91        fclose( $LogFile );
     92    }
    6593}
    6694
    67 ?>
     95
  • easify-server-woocommerce/trunk/includes/class-easify-wc-send-order-to-easify.php

    r2419894 r2489733  
    1717 */
    1818
    19 require_once ( 'class-easify-generic-easify-cloud-api.php' );
    20 require_once ( 'class-easify-generic-easify-server.php' );
    21 require_once ( 'class-easify-generic-easify-order-model.php' );
    22 require_once ( 'class-easify-wc-easify-options.php' );
    23 require_once ( 'class-easify-wc-woocommerce-order.php' );
     19require_once 'class-easify-generic-easify-cloud-api.php';
     20require_once 'class-easify-generic-easify-server.php';
     21require_once 'class-easify-generic-easify-order-model.php';
     22require_once 'class-easify-wc-easify-options.php';
     23require_once 'class-easify-wc-woocommerce-order.php';
     24require_once 'class-easify-wc-coupon-splitter.php';
    2425
    2526/**
    2627 * Sends a WooCommerce order to an Easify Server
    27  * 
     28 *
    2829 * This class gets a WooCommerce order, packages it into an Easify Order Model
    29  * object, and sends it to the Easify Cloud API Server so that the order can 
     30 * object, and sends it to the Easify Cloud API Server so that the order can
    3031 * be queued for delivery to the relevant Easify Server.
    31  * 
     32 *
    3233 * @class       Easify_WC_Send_Order_To_Easify
    33  * @version     4.23
     34 * @version     4.26
    3435 * @package     easify-woocommerce-connector
    35  * @author      Easify 
     36 * @author      Easify
    3637 */
    3738class Easify_WC_Send_Order_To_Easify {
    3839
    39     private $easify_username;
    40     private $easify_password;
    41     private $easify_order_model;
    42     private $woocommerce_order;
    43     private $easify_options;
    44 
    45     /**
    46      * Constructor
    47      *
    48      * Initialises various classes that are used to get an order from WooCommerce,
    49      * and populate it into an Easify Order Model so that it can be sent to the
    50      * Easify Cloud API to be queued for sending to the destination Easify
    51      * Server.
    52      *
    53      * The Easify Order Model is just a class that represents an Easify order.
    54      * It has no functionality, it is just a way of representing and storing the
    55      * order data in a format that can be send to an Easify Server via the Easify
    56      * Cloud API Server.
    57      *
    58      * @param integer $order_no - The WooCommerce order number of the order.
    59      * @param string $username - The username of the Easify WooCommerce plugin subscription
    60      * @param string $password - The password of the Easify WooCommerce plugin subscription
    61      */
    62     function __construct($order_no, $username, $password) {
    63         $this->easify_username = $username;
    64         $this->easify_password = $password;
    65 
    66         // Instantiate a repository so we can easily get at WooCommerce order parameters
    67         $this->woocommerce_order = new Easify_WC_WooCommerce_Order($order_no);
    68 
    69         // Instantiate an Easify Order Model, we will populate this with the order
    70         // information and send it to the Easify Cloud API which will queue the order
    71         // to be sent to the relevant Easify Server.
    72         $this->easify_order_model = new Easify_Generic_Easify_Order_Model();
    73 
    74         // Create an Easify options class for easy access to Easify Options
    75         $this->easify_options = new Easify_WC_Easify_Options();
    76     }
    77 
    78     public function process() {
    79         // compile all required information and send it to the Easify Cloud API
    80         // to be forwarded to the relevant Easify Server.
    81         try {
    82             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify Order no:" . $this->woocommerce_order->order_no);
    83 
    84             // Copy WooCommerce order information to Easify Order Model                       
    85             $this->do_order();
    86 
    87             // Copy WooCommerce customer details to Easify Order Model
    88             $this->do_customer();
    89 
    90             // Copy WooCommerce order details to Easify Order Model
    91             $this->do_order_details();           
    92            
    93             // Add shipping to Easify Order Model
    94             $this->do_shipping();
    95 
    96             // Copy WooCommerce coupons of present to Easify Order Model
    97             $this->do_order_coupons();
    98            
    99             // Add payment record to Easify Order Model
    100             $this->do_payment();
    101 
    102             // Send Easify Order Model to the Easify Server
    103             if (!$this->send_order_to_easify_server()) {
    104                 SendEmail("Order (" . $this->woocommerce_order->order_no . ") failed to submit to Easify.");
    105             }
    106         } catch (Exception $ex) {
    107             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify Exception: " . $ex->getMessage());
    108             throw $ex;
    109         }
    110     }
    111 
    112     /**
    113      * Gets the WooCommerce Order and puts it into the Easify Order Model ready
    114      * for it to be sent to the Easify Cloud API for delivery to the Easify Server
    115      */
    116     private function do_order() {
    117         // Populate Easify Order model with order information
    118         $this->easify_order_model->ExtOrderNo = $this->woocommerce_order->order_no;
    119         $this->easify_order_model->ExtCustomerId = $this->woocommerce_order->customer_id;
    120         $this->easify_order_model->DatePlaced = $this->get_formatted_date();
    121         $this->easify_order_model->StatusId = $this->easify_options->get_easify_order_status_id();
    122 
    123         // Determine whether paid. If payment method was not enabled in WordPress
    124         // options, we don't want to set paid = true here. For example, if customer
    125         // is paying by COD they won't have yet paid for their order and we want
    126         // that reflected in the Easify order.       
    127         if ($this->easify_options->is_payment_method_enabled($this->woocommerce_order->payment_method)) {
    128             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify.do_order() - payment method " .
    129                     $this->woocommerce_order->payment_method . " enabled, marking order as paid.");
    130            
    131             $this->easify_order_model->Paid = 'true'; // Set as string otherwise true in PHP becomes 1 and the WebAPI mapper in CloudAPI thinks it's an int
    132             $this->easify_order_model->DatePaid = $this->get_formatted_date();
    133         } else {
    134             // If customer is paying later (COD, BACS, Cheque) no payment record will be
    135             // raised in Easify but we still need to record how they intend to pay.
    136             // Record payment method in internal notes to let Easify know how the
    137             // customer intends to pay for the order...
    138             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify.do_order() - payment method " .
    139                     $this->woocommerce_order->payment_method . " NOT enabled, NOT marking order as paid.");
    140            
    141             $this->easify_order_model->Paid = 'false';
    142             $this->easify_order_model->DatePaid = NULL;
    143             $this->easify_order_model->Notes = "Payment to follow - Payment method: " . $this->woocommerce_order->payment_method . ". ";
    144         }
    145 
    146         $this->easify_order_model->CustomerRef = "";
    147         $this->easify_order_model->Invoiced = 'true';
    148         $this->easify_order_model->DateInvoiced = $this->get_formatted_date();
    149         $this->easify_order_model->Comments = $this->easify_options->get_easify_order_comment() . " " . $this->woocommerce_order->order_no;
    150 
    151         if (!empty($this->woocommerce_order->order->customer_note)) {
    152             $this->easify_order_model->Notes .= "\r\n\r\nCustomer Notes: " . $this->woocommerce_order->order->customer_note;
    153         }
    154 
    155         $this->easify_order_model->DateOrdered = $this->get_formatted_date();
    156         $this->easify_order_model->DueDate = $this->get_formatted_date();
    157         $this->easify_order_model->DueTime = $this->get_formatted_date();
    158         $this->easify_order_model->Scheduled = 'false';
    159         $this->easify_order_model->Duration = 0;
    160         $this->easify_order_model->Priority = 0;
    161         $this->easify_order_model->Recurring = 'false';
    162         $this->easify_order_model->RecurTimePeriod = 0;
    163         $this->easify_order_model->DueDate2 = $this->get_formatted_date();
    164         $this->easify_order_model->DueTime2 = $this->get_formatted_date();
    165         $this->easify_order_model->DueDuration2 = 0;
    166         $this->easify_order_model->OrderType = $this->easify_options->get_easify_order_type_id();
    167         $this->easify_order_model->PaymentTermsId = $this->easify_options->get_easify_payment_terms_id();
    168 
    169         // Append footer to internal notes if present...
    170         if (!empty($this->easify_order_model->Notes)) {
    171             $this->easify_order_model->Notes .= "\r\n\r\n";
    172             $this->easify_order_model->Notes .= " - Comment auto generated by Easify WooCommerce plugin - " . date('d M Y \a\t H:i', time());
    173             $this->easify_order_model->Notes .= "\r\n______________________________";
    174         }
    175     }
    176 
    177     /**
    178      * Gets the WooCommerce Customer for the order and puts it into the Easify
    179      * Order Model ready for it to be sent to the Easify Cloud API for delivery
    180      * to the Easify Server
    181      */
    182     private function do_customer() {
    183         // Populate customer in Easify Model with customer details from WooCommerce order
    184         $this->easify_order_model->Customer->ExtCustomerId = $this->woocommerce_order->customer_id;
    185         $this->easify_order_model->Customer->CompanyName = $this->woocommerce_order->order_post_meta['_billing_company'][0];
    186         $this->easify_order_model->Customer->Title = "";
    187         $this->easify_order_model->Customer->FirstName = $this->woocommerce_order->order_post_meta['_billing_first_name'][0];
    188         $this->easify_order_model->Customer->Surname = $this->woocommerce_order->order_post_meta['_billing_last_name'][0];
    189         $this->easify_order_model->Customer->JobTitle = "";
    190         $this->easify_order_model->Customer->Address1 = $this->woocommerce_order->order_post_meta['_billing_address_1'][0];
    191         $this->easify_order_model->Customer->Address2 = $this->woocommerce_order->order_post_meta['_billing_address_2'][0];
    192         $this->easify_order_model->Customer->Address3 = "";
    193         $this->easify_order_model->Customer->Town = $this->woocommerce_order->order_post_meta['_billing_city'][0];
    194         $this->easify_order_model->Customer->County = $this->woocommerce_order->order_post_meta['_billing_postcode'][0];
    195         $this->easify_order_model->Customer->Country = $this->woocommerce_order->order_post_meta['_billing_country'][0];
    196         $this->easify_order_model->Customer->HomeTel = $this->woocommerce_order->order_post_meta['_billing_phone'][0];
    197         $this->easify_order_model->Customer->Email = $this->woocommerce_order->order_post_meta['_billing_email'][0];
    198         $this->easify_order_model->Customer->DeliveryFirstName = $this->woocommerce_order->order_post_meta['_shipping_first_name'][0];
    199         $this->easify_order_model->Customer->DeliverySurname = $this->woocommerce_order->order_post_meta['_shipping_last_name'][0];
    200         $this->easify_order_model->Customer->DeliveryCompanyName = $this->woocommerce_order->order_post_meta['_shipping_company'][0];
    201         $this->easify_order_model->Customer->DeliveryAddress1 = $this->woocommerce_order->order_post_meta['_shipping_address_1'][0];
    202         $this->easify_order_model->Customer->DeliveryAddress2 = $this->woocommerce_order->order_post_meta['_shipping_address_2'][0];
    203         $this->easify_order_model->Customer->DeliveryAddress3 = "";
    204         $this->easify_order_model->Customer->DeliveryTown = $this->woocommerce_order->order_post_meta['_shipping_city'][0];
    205         $this->easify_order_model->Customer->DeliveryCounty = $this->woocommerce_order->order_post_meta['_shipping_state'][0];
    206         $this->easify_order_model->Customer->DeliveryPostcode = $this->woocommerce_order->order_post_meta['_shipping_postcode'][0];
    207         $this->easify_order_model->Customer->DeliveryCountry = $this->woocommerce_order->order_post_meta['_shipping_country'][0];
    208         $this->easify_order_model->Customer->DeliveryTel = "";
    209         $this->easify_order_model->Customer->DeliveryEmail = "";
    210         $this->easify_order_model->Customer->SubscribeToNewsletter = "";
    211 
    212         $this->easify_order_model->Customer->CustomerTypeId = $this->easify_options->get_easify_customer_type_id();
    213         $this->easify_order_model->Customer->RelationshipId = $this->easify_options->get_easify_customer_relationship_id();       
    214        
    215         // Typically we don't want to overwrite the following values as they will have been set in Easify.
    216         // pass nulls so the values are ignored by the Easify Cloud API
    217         $this->easify_order_model->Customer->TradeAccount = null; // Set to null - don't want to overwrite an existing trade status
    218         $this->easify_order_model->Customer->CreditLimit = null; // Set to null - don't want to overwrite an existing credit limit       
    219         $this->easify_order_model->Customer->PaymentTermsId = null;  // Set to null - don't want to overwrite an existing payment terms setting
    220        
    221 
    222     }
    223 
    224     /**
    225      * Iterates each product in the WooCommerce order and adds it to the Easify
    226      * Order Model prior to it being queued for delivery to the destination
    227      * Easify Server via the Easify Cloud API.
    228      */
    229     private function do_order_details() {
    230         // Iterate each product in the order...
    231         foreach ($this->woocommerce_order->order_details as $woocommerce_product) {
    232             // Create a new Easify Order Details object
    233             $easify_order_detail = new Easify_Order_Order_Details();
    234 
    235             // Copy WooCommerce order detail (product) to Easify Order Model order details   
    236            
    237             $variationId = $woocommerce_product['variation_id'];
    238             Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.do_order_details() $variationId: ' . $variationId);         
    239            
    240             $easify_order_detail->Sku = $this->get_easify_sku_by_woocommerce_product_id($woocommerce_product['product_id'], $variationId);                 
    241                      
    242             $easify_order_detail->Qty = $woocommerce_product['qty'];
    243             $easify_order_detail->Price = round($woocommerce_product['line_subtotal'] / ($woocommerce_product['qty'] == 0 ? 1 : $woocommerce_product['qty']), 4);
    244             $easify_order_detail->Comments = '';
    245            
    246             /* TODO: Determine tax class based on WooCommerce settings i.e. Tax Class based on delivery address, or billing address,
    247              * and lookup appropriate Easify tax id and rate to use. E.g. if GB country code for tax class then
    248              * just use usual tax rate. Else if other country code or * then lookup relevant tax code...  */
    249             $easify_order_detail->TaxId = $this->easify_options->get_easify_tax_id_by_code($woocommerce_product['tax_class']);
    250             $easify_order_detail->TaxRate = $this->easify_options->get_easify_tax_rate_by_code($woocommerce_product['tax_class']);
    251             $easify_order_detail->Spare = '';
    252             $easify_order_detail->ExtParentId = 0;
    253             $easify_order_detail->ExtOrderDetailsId = $woocommerce_product['product_id'];
    254             $easify_order_detail->ExtOrderNo = $this->woocommerce_order->order_no;
    255             $easify_order_detail->AutoAllocateStock = 'true';
    256 
    257             // Add the order detail to the Easify order model
    258             array_push($this->easify_order_model->OrderDetails, $easify_order_detail);
    259         }
    260     }
    261 
    262    
    263    
    264      /**
    265      * Iterates each coupon in the WooCommerce order and adds it to the Easify
    266      * Order Model prior to it being queued for delivery to the destination
    267      * Easify Server via the Easify Cloud API.
    268      */
    269     private function do_order_coupons() {
    270         // Iterate each product in the order...
    271         foreach ($this->woocommerce_order->coupons as $woocommerce_coupon) {
    272             // Create a new Easify Order Details object
    273             $easify_order_detail = new Easify_Order_Order_Details();
    274 
    275             // Copy WooCommerce order detail (product) to Easify Order Model order details   
    276             $easify_order_detail->Sku =  $this->easify_options->get_easify_discount_sku(); //$this->get_easify_sku_by_woocommerce_product_id($woocommerce_product['product_id']);
    277             $easify_order_detail->Qty = 1;
    278             $easify_order_detail->Price = $woocommerce_coupon->coupon_value * -1; // Negative value for discount...
    279             $easify_order_detail->Comments = 'Coupon code: ' . $woocommerce_coupon->coupon_code;
    280             $easify_order_detail->TaxId = $this->easify_options->get_easify_default_tax_id();
    281             $easify_order_detail->TaxRate = $this->easify_options->get_easify_default_tax_rate();
    282             $easify_order_detail->Spare = '';
    283             $easify_order_detail->ExtParentId = 0;
    284             $easify_order_detail->ExtOrderNo = $this->woocommerce_order->order_no;
    285             $easify_order_detail->AutoAllocateStock = 'false';
    286 
    287             // Add the order detail to the Easify order model
    288             array_push($this->easify_order_model->OrderDetails, $easify_order_detail);
    289         }
    290     } 
    291    
    292    
    293     /**
    294      * Pass in a WooCommerce product id and this function will return the
    295      * corresponding Easify product SKU.
    296      *
    297      * @global database $wpdb
    298      * @param integer $woocommerce_product_id
    299      * @return integer
    300      */
    301     private function get_easify_sku_by_woocommerce_product_id($woocommerce_product_id, $variationId) {
    302         global $wpdb;
    303        
    304         Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.get_easify_sku_by_woocommerce_product_id() ' .
    305                 ' $woocommerce_product_id: ' . $woocommerce_product_id .
    306                 ' $variationId: ' . $variationId);
    307        
    308         if ($variationId != '0')
    309         {
    310             // Have variation - try to get sku
    311             $sku = $wpdb->get_var($wpdb->prepare(
     40    private $easify_username;
     41    private $easify_password;
     42    private $easify_order_model;
     43    private $woocommerce_order;
     44    private $easify_options;
     45
     46    /**
     47     * Constructor
     48     *
     49     * Initialises various classes that are used to get an order from WooCommerce,
     50     * and populate it into an Easify Order Model so that it can be sent to the
     51     * Easify Cloud API to be queued for sending to the destination Easify
     52     * Server.
     53     *
     54     * The Easify Order Model is just a class that represents an Easify order.
     55     * It has no functionality, it is just a way of representing and storing the
     56     * order data in a format that can be send to an Easify Server via the Easify
     57     * Cloud API Server.
     58     *
     59     * @param int    $order_no - The WooCommerce order number of the order.
     60     * @param string $username - The username of the Easify WooCommerce plugin subscription.
     61     * @param string $password - The password of the Easify WooCommerce plugin subscription.
     62     */
     63    public function __construct( int $order_no, string $username, string $password ) {
     64        $this->easify_username = $username;
     65        $this->easify_password = $password;
     66
     67        // Instantiate a repository so we can easily get at WooCommerce order parameters.
     68        $this->woocommerce_order = new Easify_WC_WooCommerce_Order( $order_no );
     69
     70        // Instantiate an Easify Order Model, we will populate this with the order
     71        // information and send it to the Easify Cloud API which will queue the order
     72        // to be sent to the relevant Easify Server.
     73        $this->easify_order_model = new Easify_Generic_Easify_Order_Model();
     74
     75        // Create an Easify options class for easy access to Easify Options.
     76        $this->easify_options = new Easify_WC_Easify_Options();
     77    }
     78
     79    /**
     80     * Compiles all required information and send it to the Easify Cloud API
     81     * to be forwarded to the relevant Easify Server.
     82     *
     83     * @throws Exception - Throws exception on error and logs it.
     84     */
     85    public function process() {
     86        try {
     87            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify Order no:' . $this->woocommerce_order->order_no );
     88
     89            // Copy WooCommerce order information to Easify Order Model.
     90            $this->do_order();
     91
     92            // Copy WooCommerce customer details to Easify Order Model.
     93            $this->do_customer();
     94
     95            // Copy WooCommerce order details to Easify Order Model.
     96            $this->do_order_details();
     97
     98            // Copy WooCommerce coupons of present to Easify Order Model. Do this before
     99            // adding shipping because coupons are calculated excluding shipping.
     100            $this->do_order_coupons();
     101
     102            // Add shipping to Easify Order Model.
     103            $this->do_shipping();
     104
     105            // Add payment record to Easify Order Model.
     106            $this->do_payment();
     107
     108            // Send Easify Order Model to the Easify Server.
     109            if ( ! $this->send_order_to_easify_server() ) {
     110                SendEmail( 'Order (' . $this->woocommerce_order->order_no . ') failed to submit to Easify.' );
     111            }
     112        } catch ( Exception $ex ) {
     113            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify Exception: ' . $ex->getMessage() );
     114            throw $ex;
     115        }
     116    }
     117
     118    /**
     119     * Gets the WooCommerce Order and puts it into the Easify Order Model ready
     120     * for it to be sent to the Easify Cloud API for delivery to the Easify Server
     121     */
     122    private function do_order() {
     123        // Populate Easify Order model with order information.
     124        $this->easify_order_model->ExtOrderNo    = $this->woocommerce_order->order_no;
     125        $this->easify_order_model->ExtCustomerId = $this->woocommerce_order->customer_id;
     126        $this->easify_order_model->DatePlaced    = $this->get_formatted_date();
     127        $this->easify_order_model->StatusId      = $this->easify_options->get_easify_order_status_id();
     128
     129        $this->easify_order_model->GrossTotal = $this->woocommerce_order->order->get_total();
     130        $this->easify_order_model->TaxTotal   = $this->woocommerce_order->order->get_total_tax();
     131        $this->easify_order_model->NetTotal   = $this->easify_order_model->GrossTotal - $this->easify_order_model->TaxTotal;
     132
     133        // Determine whether paid. If payment method was not enabled in WordPress
     134        // options, we don't want to set paid = true here. For example, if customer
     135        // is paying by COD they won't have yet paid for their order and we want
     136        // that reflected in the Easify order.
     137        if ( $this->easify_options->is_payment_method_enabled( $this->woocommerce_order->payment_method ) ) {
     138            Easify_Logging::Log(
     139                'Easify_WC_Send_Order_To_Easify.do_order() - payment method ' .
     140                $this->woocommerce_order->payment_method . ' enabled, marking order as paid.'
     141            );
     142
     143            $this->easify_order_model->Paid     = 'true'; // Set as string otherwise true in PHP becomes 1 and the WebAPI mapper in CloudAPI thinks it's an int.
     144            $this->easify_order_model->DatePaid = $this->get_formatted_date();
     145        } else {
     146            // If customer is paying later (COD, BACS, Cheque) no payment record will be
     147            // raised in Easify but we still need to record how they intend to pay.
     148            // Record payment method in internal notes to let Easify know how the
     149            // customer intends to pay for the order.
     150            Easify_Logging::Log(
     151                'Easify_WC_Send_Order_To_Easify.do_order() - payment method ' .
     152                $this->woocommerce_order->payment_method . ' NOT enabled, NOT marking order as paid.'
     153            );
     154
     155            $this->easify_order_model->Paid     = 'false';
     156            $this->easify_order_model->DatePaid = null;
     157            $this->easify_order_model->Notes    = 'Payment to follow - Payment method: ' .
     158                                                    $this->woocommerce_order->payment_method .
     159                                                    '. ';
     160        }
     161
     162        $this->easify_order_model->CustomerRef  = '';
     163        $this->easify_order_model->Invoiced     = 'true';
     164        $this->easify_order_model->DateInvoiced = $this->get_formatted_date();
     165        $this->easify_order_model->Comments     = $this->easify_options->get_easify_order_comment() . ' ' . $this->woocommerce_order->order_no;
     166
     167        if ( ! empty( $this->woocommerce_order->order->customer_note ) ) {
     168            $this->easify_order_model->Notes .= "\r\n\r\nCustomer Notes: " . $this->woocommerce_order->order->customer_note;
     169        }
     170
     171        $this->easify_order_model->DateOrdered     = $this->get_formatted_date();
     172        $this->easify_order_model->DueDate         = $this->get_formatted_date();
     173        $this->easify_order_model->DueTime         = $this->get_formatted_date();
     174        $this->easify_order_model->Scheduled       = 'false';
     175        $this->easify_order_model->Duration        = 0;
     176        $this->easify_order_model->Priority        = 0;
     177        $this->easify_order_model->Recurring       = 'false';
     178        $this->easify_order_model->RecurTimePeriod = 0;
     179        $this->easify_order_model->DueDate2        = $this->get_formatted_date();
     180        $this->easify_order_model->DueTime2        = $this->get_formatted_date();
     181        $this->easify_order_model->DueDuration2    = 0;
     182        $this->easify_order_model->OrderType       = $this->easify_options->get_easify_order_type_id();
     183        $this->easify_order_model->PaymentTermsId  = $this->easify_options->get_easify_payment_terms_id();
     184
     185        // Append footer to internal notes if present.
     186        if ( ! empty( $this->easify_order_model->Notes ) ) {
     187            $this->easify_order_model->Notes .= "\r\n\r\n";
     188            $this->easify_order_model->Notes .= ' - Comment auto generated by Easify WooCommerce plugin - ' . date( 'd M Y \a\t H:i', time() );
     189            $this->easify_order_model->Notes .= "\r\n______________________________";
     190        }
     191    }
     192
     193    /**
     194     * Gets the WooCommerce Customer for the order and puts it into the Easify
     195     * Order Model ready for it to be sent to the Easify Cloud API for delivery.
     196     * to the Easify Server
     197     */
     198    private function do_customer() {
     199        // Populate customer in Easify Model with customer details from WooCommerce order.
     200        $this->easify_order_model->Customer->ExtCustomerId       = $this->woocommerce_order->customer_id;
     201        $this->easify_order_model->Customer->CompanyName         = $this->woocommerce_order->order_post_meta['_billing_company'][0];
     202        $this->easify_order_model->Customer->Title               = '';
     203        $this->easify_order_model->Customer->FirstName           = $this->woocommerce_order->order_post_meta['_billing_first_name'][0];
     204        $this->easify_order_model->Customer->Surname             = $this->woocommerce_order->order_post_meta['_billing_last_name'][0];
     205        $this->easify_order_model->Customer->JobTitle            = '';
     206        $this->easify_order_model->Customer->Address1            = $this->woocommerce_order->order_post_meta['_billing_address_1'][0];
     207        $this->easify_order_model->Customer->Address2            = $this->woocommerce_order->order_post_meta['_billing_address_2'][0];
     208        $this->easify_order_model->Customer->Address3            = '';
     209        $this->easify_order_model->Customer->Town                = $this->woocommerce_order->order_post_meta['_billing_city'][0];
     210        $this->easify_order_model->Customer->County              = $this->woocommerce_order->order_post_meta['_billing_postcode'][0];
     211        $this->easify_order_model->Customer->Country             = $this->woocommerce_order->order_post_meta['_billing_country'][0];
     212        $this->easify_order_model->Customer->HomeTel             = $this->woocommerce_order->order_post_meta['_billing_phone'][0];
     213        $this->easify_order_model->Customer->Email               = $this->woocommerce_order->order_post_meta['_billing_email'][0];
     214        $this->easify_order_model->Customer->DeliveryFirstName   = $this->woocommerce_order->order_post_meta['_shipping_first_name'][0];
     215        $this->easify_order_model->Customer->DeliverySurname     = $this->woocommerce_order->order_post_meta['_shipping_last_name'][0];
     216        $this->easify_order_model->Customer->DeliveryCompanyName = $this->woocommerce_order->order_post_meta['_shipping_company'][0];
     217        $this->easify_order_model->Customer->DeliveryAddress1    = $this->woocommerce_order->order_post_meta['_shipping_address_1'][0];
     218        $this->easify_order_model->Customer->DeliveryAddress2    = $this->woocommerce_order->order_post_meta['_shipping_address_2'][0];
     219        $this->easify_order_model->Customer->DeliveryAddress3    = '';
     220        $this->easify_order_model->Customer->DeliveryTown        = $this->woocommerce_order->order_post_meta['_shipping_city'][0];
     221        $this->easify_order_model->Customer->DeliveryCounty      = $this->woocommerce_order->order_post_meta['_shipping_state'][0];
     222        $this->easify_order_model->Customer->DeliveryPostcode    = $this->woocommerce_order->order_post_meta['_shipping_postcode'][0];
     223        $this->easify_order_model->Customer->DeliveryCountry     = $this->woocommerce_order->order_post_meta['_shipping_country'][0];
     224        $this->easify_order_model->Customer->DeliveryTel         = '';
     225        $this->easify_order_model->Customer->DeliveryEmail       = '';
     226
     227        $this->easify_order_model->Customer->SubscribeToNewsletter = '';
     228
     229        $this->easify_order_model->Customer->CustomerTypeId = $this->easify_options->get_easify_customer_type_id();
     230        $this->easify_order_model->Customer->RelationshipId = $this->easify_options->get_easify_customer_relationship_id();
     231
     232        // Typically we don't want to overwrite the following values as they will have been set in Easify.
     233        // pass nulls so the values are ignored by the Easify Cloud API.
     234        $this->easify_order_model->Customer->TradeAccount   = null; // Set to null - don't want to overwrite an existing trade status.
     235        $this->easify_order_model->Customer->CreditLimit    = null; // Set to null - don't want to overwrite an existing credit limit.
     236        $this->easify_order_model->Customer->PaymentTermsId = null;  // Set to null - don't want to overwrite an existing payment terms setting.
     237    }
     238
     239    /**
     240     * Iterates each product in the WooCommerce order and adds it to the Easify
     241     * Order Model prior to it being queued for delivery to the destination.
     242     * Easify Server via the Easify Cloud API.
     243     */
     244    private function do_order_details() {
     245        // Iterate each product in the order.
     246        foreach ( $this->woocommerce_order->order_details as $woocommerce_product ) {
     247            // Create a new Easify Order Details object.
     248            $easify_order_detail = new Easify_Order_Order_Details();
     249
     250            // Copy WooCommerce order detail (product) to Easify Order Model order details.
     251
     252            $variation_id = $woocommerce_product['variation_id'];
     253            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify.do_order_details() $variation_id: ' . $variation_id );
     254
     255            $easify_order_detail->Sku = $this->get_easify_sku_by_woocommerce_product_id( $woocommerce_product['product_id'], $variation_id );
     256
     257            $easify_order_detail->Qty      = $woocommerce_product['qty'];
     258            $easify_order_detail->Price    = round( $woocommerce_product['line_subtotal'] / ( $woocommerce_product['qty'] == 0 ? 1 : $woocommerce_product['qty'] ), 4 );
     259            $easify_order_detail->Comments = '';
     260
     261            /*
     262             * TODO: Determine tax class based on WooCommerce settings i.e. Tax Class based on delivery address, or billing address,
     263             * and lookup appropriate Easify tax id and rate to use. E.g. if GB country code for tax class the
     264             * just use usual tax rate. Else if other country code or * then lookup relevant tax code.
     265             */
     266            $easify_order_detail->TaxId             = $this->easify_options->get_easify_tax_id_by_code( $woocommerce_product['tax_class'] );
     267            $easify_order_detail->TaxRate           = $this->easify_options->get_easify_tax_rate_by_code( $woocommerce_product['tax_class'] );
     268            $easify_order_detail->Spare             = '';
     269            $easify_order_detail->ExtParentId       = 0;
     270            $easify_order_detail->ExtOrderDetailsId = $woocommerce_product['product_id'];
     271            $easify_order_detail->ExtOrderNo        = $this->woocommerce_order->order_no;
     272            $easify_order_detail->AutoAllocateStock = 'true';
     273
     274            // Add the order detail to the Easify order model.
     275            array_push( $this->easify_order_model->OrderDetails, $easify_order_detail );
     276        }
     277    }
     278
     279    /**
     280     * We use the WooCommerceCouponSplitter() to split the WooCommerce coupon into
     281     * a separate coupon for each tax code that is present on the order.
     282     * Iterates each split coupon and adds it to the Easify Order Model prior to
     283     * it being queued for delivery to the destination Easify Server via the
     284     * Easify Cloud API.
     285     *
     286     * NOTE: We process the coupons before the shipping so that the discount
     287     * doesn't get applied to the shipping.
     288     */
     289    private function do_order_coupons() {
     290        $coupon_splitter = new Easify_WC_Coupon_Splitter();
     291        $split_coupons   = $coupon_splitter->split_coupons(
     292            $this->easify_order_model->OrderDetails,
     293            $this->woocommerce_order->coupons
     294        );
     295
     296        foreach ( $split_coupons as $split_coupon ) {
     297            // Create a new Easify Order Details object.
     298            $easify_order_detail = new Easify_Order_Order_Details();
     299
     300            // Copy WooCommerce order detail (product) to Easify Order Model order details.
     301            $easify_order_detail->Sku   = $this->easify_options->get_easify_discount_sku();
     302            $easify_order_detail->Qty   = 1;
     303            $easify_order_detail->Price = $split_coupon->amount * -1; // Negative value for discount.
     304
     305            if ( count( $split_coupons ) === 1 ) {
     306                $easify_order_detail->Comments = 'Coupon code: ' . $split_coupon->code;
     307            } else {
     308                $easify_order_detail->Comments = 'Coupon code: ' . $split_coupon->code . ' (@' . $split_coupon->tax_rate . '% Tax Rate)';
     309            }
     310
     311            $easify_order_detail->TaxId             = $split_coupon->tax_id;
     312            $easify_order_detail->TaxRate           = $split_coupon->tax_rate;
     313            $easify_order_detail->Spare             = '';
     314            $easify_order_detail->ExtParentId       = 0;
     315            $easify_order_detail->ExtOrderNo        = $this->woocommerce_order->order_no;
     316            $easify_order_detail->AutoAllocateStock = 'false';
     317
     318            // Add the order detail to the Easify order model.
     319            array_push( $this->easify_order_model->OrderDetails, $easify_order_detail );
     320        }
     321    }
     322
     323    /**
     324     * Pass in a WooCommerce product id and this function will return the
     325     * corresponding Easify product SKU.
     326     *
     327     * @param int          $woocommerce_product_id woocommerce product id.
     328     * @param $variation_id
     329     *
     330     * @return int
     331     * @global database $wpdb
     332     */
     333    private function get_easify_sku_by_woocommerce_product_id( $woocommerce_product_id, $variation_id ): int {
     334        global $wpdb;
     335
     336        Easify_Logging::Log(
     337            'Easify_WC_Send_Order_To_Easify.get_easify_sku_by_woocommerce_product_id() ' .
     338            ' $woocommerce_product_id: ' . $woocommerce_product_id .
     339            ' $variationId: ' . $variation_id
     340        );
     341
     342        if ( $variation_id != '0' ) {
     343            // Have variation - try to get sku.
     344            $sku = $wpdb->get_var(
     345                $wpdb->prepare(
     346                "SELECT meta_value FROM " . $wpdb->postmeta .
     347                    " WHERE meta_key = '_sku' AND post_id = '%s' LIMIT 1", $variation_id
     348                )
     349            );
     350
     351            if ( $sku == '' ) {
     352                Easify_Logging::Log(
     353                    'Easify_WC_Send_Order_To_Easify.get_easify_sku_by_woocommerce_product_id() ' .
     354                    ' variation not found, must be first variation which does not have SKU. Using productId instead.'
     355                );
     356
     357                // First variation doesn't have a SKU, instead use product Id.
     358                $sku = $wpdb->get_var(
     359                    $wpdb->prepare(
    312360                    "SELECT meta_value FROM " . $wpdb->postmeta .
    313                     " WHERE meta_key = '_sku' AND post_id = '%s' LIMIT 1", $variationId));
    314            
    315             if ($sku == '')
    316             {
    317                 Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.get_easify_sku_by_woocommerce_product_id() ' .
    318                 ' variation not found, must be first variation which does not have SKU. Using productId instead.');
    319        
    320                 // First variation doesn't have a SKU, instead use product Id           
    321                 $sku = $wpdb->get_var($wpdb->prepare(
    322                     "SELECT meta_value FROM " . $wpdb->postmeta .
    323                     " WHERE meta_key = '_sku' AND post_id = '%s' LIMIT 1", $woocommerce_product_id));             
    324             }
    325                        
    326         }
    327         else
    328         {
    329             // Not a variation
    330             $sku = $wpdb->get_var($wpdb->prepare(
    331                     "SELECT meta_value FROM " . $wpdb->postmeta .
    332                     " WHERE meta_key = '_sku' AND post_id = '%s' LIMIT 1", $woocommerce_product_id));
    333         }
    334        
    335         return $sku;
    336     }
    337 
    338    
    339     /**
    340      * Gets the shipping methods from the WooCommerce order and creates an
    341      * Easify order detail for each shipping method and adds it to the Easify
    342      * Order Model prior to it being sent to the Easify Cloud API to be queued
    343      * for delivery to the destination Easify Server.   
    344      */
    345     private function do_shipping() {
    346         // Iterate each shipping method in the WooCommerce order...
    347         foreach ($this->woocommerce_order->shipping_methods as $woocommerce_shipping) {
    348             // If shipping has been expanded to include different instances, extract shipping method
    349             $wocommerce_shipping_method = $woocommerce_shipping['method_id'];
    350            
    351             Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.do_shipping() $wocommerce_shipping_method: ' . $wocommerce_shipping_method);
    352            
    353             if (strpos($wocommerce_shipping_method, ":") !== false) {
    354                 $wocommerce_shipping_method = explode(":", $wocommerce_shipping_method)[0];
    355             }
    356 
    357             // Get the Easify SKU that is mapped to the WooCommerce shipping method
    358             $easify_sku = $this->easify_options->get_easify_shipping_method_sku_by_name($wocommerce_shipping_method);
    359 
    360             // if $easify_sku == -1 means that no shipping method has been mapped in Easify plugin settings
    361             if ($easify_sku > -1) {
    362 
    363                 // Create a new Easify order detail to represent the shipping   
    364                 $easify_order_detail = new Easify_Order_Order_Details();
    365 
    366                 $easify_order_detail->Sku = $easify_sku;
    367                 $easify_order_detail->Qty = 1;
    368                 $easify_order_detail->Price = $woocommerce_shipping['cost'];
    369                 $easify_order_detail->Comments = $woocommerce_shipping['name'];
    370                 $easify_order_detail->TaxRate = $this->easify_options->get_easify_default_tax_rate();
    371                 $easify_order_detail->TaxId = $this->easify_options->get_easify_default_tax_id();
    372                 $easify_order_detail->Spare = '';
    373                 $easify_order_detail->ExtParentId = 0;
     361                    " WHERE meta_key = '_sku' AND post_id = '%s' LIMIT 1", $woocommerce_product_id
     362                    )
     363                );
     364            }
     365        } else {
     366            // Not a variation.
     367            $sku = $wpdb->get_var(
     368                $wpdb->prepare(
     369                    "SELECT meta_value FROM " . $wpdb->postmeta .
     370                    " WHERE meta_key = '_sku' AND post_id = '%s' LIMIT 1",
     371                    $woocommerce_product_id
     372                )
     373            );
     374        }
     375
     376        return $sku;
     377    }
     378
     379
     380    /**
     381     * Gets the shipping methods from the WooCommerce order and creates an
     382     * Easify order detail for each shipping method and adds it to the Easify
     383     * Order Model prior to it being sent to the Easify Cloud API to be queued
     384     * for delivery to the destination Easify Server.
     385     */
     386    private function do_shipping() {
     387        // Iterate each shipping method in the WooCommerce order.
     388        foreach ( $this->woocommerce_order->shipping_methods as $woocommerce_shipping ) {
     389            // If shipping has been expanded to include different instances, extract shipping method.
     390            $woocommerce_shipping_method = $woocommerce_shipping['method_id'];
     391
     392            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify.do_shipping() $woocommerce_shipping_method: ' . $woocommerce_shipping_method );
     393
     394            if ( strpos( $woocommerce_shipping_method, ':' ) !== false ) {
     395                $woocommerce_shipping_method = explode( ':', $woocommerce_shipping_method )[0];
     396            }
     397
     398            // Get the Easify SKU that is mapped to the WooCommerce shipping method.
     399            $easify_sku = $this->easify_options->get_easify_shipping_method_sku_by_name( $woocommerce_shipping_method );
     400
     401            // if $easify_sku == -1 means that no shipping method has been mapped in Easify plugin settings.
     402            if ( $easify_sku > -1 ) {
     403                // Create a new Easify order detail to represent the shipping.
     404                $easify_order_detail = new Easify_Order_Order_Details();
     405
     406                $easify_order_detail->Sku               = $easify_sku;
     407                $easify_order_detail->Qty               = 1;
     408                $easify_order_detail->Price             = $woocommerce_shipping['cost'];
     409                $easify_order_detail->Comments          = $woocommerce_shipping['name'];
     410                $easify_order_detail->TaxRate           = $this->easify_options->get_easify_default_tax_rate();
     411                $easify_order_detail->TaxId             = $this->easify_options->get_easify_default_tax_id();
     412                $easify_order_detail->Spare             = '';
     413                $easify_order_detail->ExtParentId       = 0;
    374414                $easify_order_detail->ExtOrderDetailsId = 0;
    375                 $easify_order_detail->ExtOrderNo = $this->woocommerce_order->order_no;
    376                 $easify_order_detail->AutoAllocateStock = 'true';
    377 
    378                 // Add the order detail to the Easify order model
    379                 array_push($this->easify_order_model->OrderDetails, $easify_order_detail);
    380             }
    381         }
    382     }
    383 
    384     private function do_payment() {
    385         Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.do_payment() Payment Method: ' . $this->woocommerce_order->payment_method);
    386            
    387         // Get the payment mapping details from the Easify options...
    388         $payment_mapping = $this->easify_options->get_payment_mapping_by_payment_method_name($this->woocommerce_order->payment_method);
    389        
    390         if ($payment_mapping == NULL) {
    391             // Use default payment mapping if no matching mapping found - i.e.
    392             // if WooCommerce has a payment method that we don't support.
    393             Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.do_payment() - unknown payment method, using default.');               
    394                
    395             $payment_mapping = $this->easify_options->get_payment_mapping_by_payment_method_name('default');
    396                        
    397             // If this payment method has not been enabled in Easify Options, do nothing   
    398             if (!$this->easify_options->is_payment_method_enabled('default')) {
    399                 Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.do_payment() - default payment method not enabled, ignoring.');               
    400                 return;
    401             }         
    402         }
    403         else {
    404             // We have a payment mapping use it and make sure it is enabled...
    405             // If this payment method has not been enabled in Easify Options, do nothing   
    406             if (!$this->easify_options->is_payment_method_enabled($this->woocommerce_order->payment_method)) {
    407                 Easify_Logging::Log('Easify_WC_Send_Order_To_Easify.do_payment() - payment method not enabled, ignoring.');
    408                 return;
    409             }           
    410         }
    411 
    412         $easify_order_payment = new Easify_Order_Payments();
     415                $easify_order_detail->ExtOrderNo        = $this->woocommerce_order->order_no;
     416                $easify_order_detail->AutoAllocateStock = 'true';
     417
     418                // Add the order detail to the Easify order model.
     419                array_push( $this->easify_order_model->OrderDetails, $easify_order_detail );
     420            }
     421        }
     422    }
     423
     424    private function do_payment() {
     425        Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify.do_payment() Payment Method: ' . $this->woocommerce_order->payment_method );
     426
     427        // Get the payment mapping details from the Easify options...
     428        $payment_mapping = $this->easify_options->get_payment_mapping_by_payment_method_name( $this->woocommerce_order->payment_method );
     429
     430        if ( $payment_mapping === null ) {
     431            // Use default payment mapping if no matching mapping found - i.e.
     432            // if WooCommerce has a payment method that we don't support.
     433            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify.do_payment() - unknown payment method, using default.' );
     434
     435            $payment_mapping = $this->easify_options->get_payment_mapping_by_payment_method_name( 'default' );
     436
     437            // If this payment method has not been enabled in Easify Options, do nothing.
     438            if ( ! $this->easify_options->is_payment_method_enabled( 'default' ) ) {
     439                Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify.do_payment() - default payment method not enabled, ignoring.' );
     440                return;
     441            }
     442        } else {
     443            // We have a payment mapping use it and make sure it is enabled.
     444            // If this payment method has not been enabled in Easify Options, do nothing.
     445            if ( ! $this->easify_options->is_payment_method_enabled( $this->woocommerce_order->payment_method ) ) {
     446                Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify.do_payment() - payment method not enabled, ignoring.' );
     447                return;
     448            }
     449        }
     450
     451        $easify_order_payment = new Easify_Order_Payments();
    413452
    414453        $easify_order_payment->Amount = $this->woocommerce_order->order_post_meta['_order_total'][0];
     
    421460        $easify_order_payment->TransactionRef = !empty($this->woocommerce_order->order_post_meta['_transaction_id'][0]) ? $this->woocommerce_order->order_post_meta['_transaction_id'][0] : "";
    422461
    423         // Add the payment record to the Easify order model. Note we could add multiple
    424         // payment records to the Easify Model if we wanted to i.e. The PayPal
    425         // amount and the PayPal trasnaction fee. WooCommerce doesn't give us the
    426         // PayPal transaction fee though.
    427         array_push($this->easify_order_model->Payments, $easify_order_payment);
    428     }
    429 
    430     /**
    431     * When the WooCommerce order has been populated into the Easify Order Model,
    432     * this function passes the assembled Easify Order Model to the
    433     * Easify_Generic_Easify_Cloud_Api for it to be queued on the Easify Cloud API
    434     * to be sent to the destination Easify Server.
    435      *
    436      * @return boolean Returns true on success else false
    437     */
    438     private function send_order_to_easify_server() {
    439         try {
    440             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify::send_order_to_easify_server() easify_order_model:" . print_r($this->easify_order_model, true));
    441 
    442             // Here we send the model containing the order to the Easify Cloud API
    443             // for delivery to the Easify Server...
    444             $easify_cloud_api = new Easify_Generic_Easify_Cloud_Api(EASIFY_CLOUD_API_URI, $this->easify_username, $this->easify_password);
    445             $easify_cloud_api->send_order_to_easify_server($this->easify_order_model);
    446 
    447             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify::send_order_to_easify_server() - End");
    448             return true;
    449         } catch (Exception $ex) {
    450             Easify_Logging::Log("Easify_WC_Send_Order_To_Easify::send_order_to_easify_server() Exception: " . $ex->getMessage());
    451             return false;
    452         }
    453     }
    454 
    455     /**
    456      * Helper function to format the current date to be compatible with the
    457     * Easify Order Model.
    458      *
    459      * @return date
    460     */
    461     function get_formatted_date() {
    462         return date('Y-m-d\TH:i:s', time());
    463     }
     462        // Add the payment record to the Easify order model. Note we could add multiple
     463        // payment records to the Easify Model if we wanted to i.e. The PayPal
     464        // amount and the PayPal trasnaction fee. WooCommerce doesn't give us the
     465        // PayPal transaction fee though.
     466        array_push( $this->easify_order_model->Payments, $easify_order_payment );
     467    }
     468
     469    /**
     470    * When the WooCommerce order has been populated into the Easify Order Model,
     471    * this function passes the assembled Easify Order Model to the
     472    * Easify_Generic_Easify_Cloud_Api for it to be queued on the Easify Cloud API
     473    * to be sent to the destination Easify Server.
     474     *
     475     * @return bool Returns true on success else false
     476    */
     477    private function send_order_to_easify_server(): bool {
     478        try {
     479            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify::send_order_to_easify_server() easify_order_model:' . print_r( $this->easify_order_model, true ) );
     480
     481            // Here we send the model containing the order to the Easify Cloud API
     482            // for delivery to the Easify Server...
     483            $easify_cloud_api = new Easify_Generic_Easify_Cloud_Api( EASIFY_CLOUD_API_URI, $this->easify_username, $this->easify_password );
     484            $easify_cloud_api->send_order_to_easify_server( $this->easify_order_model );
     485
     486            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify::send_order_to_easify_server() - End' );
     487            return true;
     488        } catch ( Exception $ex ) {
     489            Easify_Logging::Log( 'Easify_WC_Send_Order_To_Easify::send_order_to_easify_server() Exception: ' . $ex->getMessage() );
     490            return false;
     491        }
     492    }
     493
     494    /**
     495     * Helper function to format the current date to be compatible with the
     496    * Easify Order Model.
     497     *
     498     * @return false|string
     499    */
     500    public function get_formatted_date() {
     501        return date( 'Y-m-d\TH:i:s', time() );
     502    }
    464503
    465504}
  • easify-server-woocommerce/trunk/includes/class-easify-wc-shop.php

    r2420694 r2489733  
    2828 *
    2929 * @class       Easify_Generic_Shop
    30  * @version     4.24
     30 * @version     4.26
    3131 * @package     easify-woocommerce-connector
    3232 * @author      Easify
     
    216216            /* @var $Product ProductDetails */   
    217217           
    218            
    219             // Determine whether ignore product uploads
    220             if ($this->easify_options->get_easify_dont_upload_products())
    221             {
    222                 Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Easify plugin settings dictate ignore product uploads. Not uploading.');               
    223                 return;           
    224             }
    225            
    226             if ($this->easify_options->get_easify_ignore_product_updates())
    227             {
    228                 Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Easify plugin settings dictate ignore product updates. Not updating.');               
    229                 return;           
    230             }           
    231            
    232             Easify_Logging::Log('Easify_WC_Shop.UpdateProduct()');
    233             // get product
    234             if (empty($this->easify_server)) {
    235                 Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Easify Server is NULL");
    236             }
    237  
     218            /* Moved stock level and price changing code to start of method so that
     219             * we can still allow stock level and price changes even if product
     220             * updtes are disabled in config. */
     221           
    238222            $Product = $this->easify_server->GetProductFromEasify($EasifySku);
    239            
    240             if ($Product->Published == FALSE) {
    241                 Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Not published, deleting product and not updating.');
    242                 $this->DeleteProduct($EasifySku);
    243                 return;
    244             }
    245 
    246              if ($Product->Discontinued == 'true') {
    247                 Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Discontinued, deleting product and not updating.');
    248                 $this->DeleteProduct($EasifySku);
    249                 return;
    250             }           
    251            
    252             // calculate price from retail margin and cost price
    253             $Price = round(($Product->CostPrice / (100 - $Product->RetailMargin) * 100), 4);
    254 
    255            
    256             // Determine whether ignore product price changes
    257             if (!$this->easify_options->get_easify_dont_update_product_prices())
    258             {
    259                 // catch reserved delivery SKUs and update delivery prices
    260                 if ($this->UpdateDeliveryPrice($Product->SKU, $Price))
    261                 {
    262                     Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Product was delivery SKU, updated price and nothing more to do.");
    263                      return;               
    264                 }               
    265             }
    266        
    267             // sanitise weight value
    268             $Product->Weight = (isset($Product->Weight) && is_numeric($Product->Weight) ? $Product->Weight : 0);
    269 
    270             if (!$this->easify_options->get_dont_overwrite_woocommerce_product_categories())
    271             {
    272                 Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating product categories...");
    273              
    274                 // get Easify product categories
    275                 $EasifyCategories = $this->easify_server->GetEasifyProductCategories();
    276 
    277                 // get Easify category description by the Easify category id
    278                 $CategoryDescription = $this->easify_server->GetEasifyCategoryDescriptionFromEasifyCategoryId($EasifyCategories, $Product->CategoryId);
    279 
    280                 // get Easify product sub categories by Easify category id
    281                 $EasifySubCategories = $this->easify_server->GetEasifyProductSubCategoriesByCategory($Product->CategoryId);
    282 
    283                 // get Easify sub category description by Easify sub category id
    284                 $SubCategoryDescription = $this->easify_server->GetEasifyCategoryDescriptionFromEasifyCategoryId($EasifySubCategories, $Product->SubcategoryId);
    285 
    286                 // insert new category if needed and return WooCommerce category id
    287                 $CategoryId = $this->InsertCategoryIntoWooCommerce($CategoryDescription, $CategoryDescription);
    288 
    289                 // insert new sub category if needed and return WooCommerce sub category id
    290                 $SubCategoryId = $this->InsertSubCategoryIntoWooCommerce($SubCategoryDescription, $SubCategoryDescription, $CategoryId);               
    291             }
    292            
    293             // get WooCommerce product id from Easify SKU
     223                                 
     224            // Get WooCommerce product id from Easify SKU
    294225            $ProductId = $this->GetWooCommerceProductIdFromEasifySKU($Product->SKU);
    295 
    296             // create a WooCommerce stub for the new product
    297             $ProductStub = array(
    298                 'ID' => $ProductId,
    299                 'post_title' => $Product->Description,
    300                 'post_content' => '',
    301                 'post_status' => 'publish',
    302                 'post_type' => 'product'
    303             );
    304            
    305             // insert product record and get WooCommerce product id
    306             $ProductId = wp_update_post($ProductStub);
    307 
    308             if (!$this->easify_options->get_dont_overwrite_woocommerce_product_categories())
    309             {
    310                 Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Linking new product categories...");
     226                       
     227            // STOCK LEVELS         
     228            $updateStockLevels = !$this->easify_options->get_easify_dont_update_product_stock_levels();
     229           
     230            if ($updateStockLevels)
     231            {           
     232                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating of stock levels enabled.");
    311233               
    312                 // link subcategory to product
    313                 wp_set_post_terms($ProductId, array($SubCategoryId), "product_cat");           
    314             }
    315 
    316             // get WooCommerce tax class from Easify tax id
    317             $TaxClass = $this->GetWooCommerceTaxIdByEasifyTaxId($Product->TaxId);
    318 
    319             // Easify_Logging::Log("UpdateProduct.TaxClass: " . $TaxClass);
    320 
    321             /*
    322               flesh out product record meta data
    323              */
    324 
    325             if (!$this->easify_options->get_easify_dont_update_product_prices())
    326             {
    327                 // pricing
    328                 update_post_meta($ProductId, '_sku', $Product->SKU);
    329                 update_post_meta($ProductId, '_price', $Price);
    330                 update_post_meta($ProductId, '_regular_price', $Price);
    331                 update_post_meta($ProductId, '_sale_price', $Price);
    332                 update_post_meta($ProductId, '_sale_price_dates_from    ', '');
    333                 update_post_meta($ProductId, '_sale_price_dates_to', '');
    334                 update_post_meta($ProductId, '_tax_status', 'taxable');
    335                 update_post_meta($ProductId, '_tax_class', strtolower($TaxClass));
    336             }
    337            
    338            
    339             if (!$this->easify_options->get_easify_dont_update_product_stock_levels())
    340             {           
    341234                // handling stock - we get free stock minus allocated stock
    342235                $stockLevel = $Product->StockLevel - $this->easify_server->get_allocation_count_by_easify_sku($Product->SKU);
     
    365258                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating stock level.");                     
    366259                update_post_meta($ProductId, '_stock', $stockLevel);
    367             }
    368            
     260            } else{
     261                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating of stock levels disabled.");
     262            }           
     263           
     264           
     265            // PRICES
     266            $updateProductPrice = !($this->easify_options->get_easify_dont_update_product_prices());
     267           
     268            if ($updateProductPrice){
     269                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating of product price enabled.");
     270               
     271                // calculate price from retail margin and cost price
     272                $Price = round(($Product->CostPrice / (100 - $Product->RetailMargin) * 100), 4);
     273
     274                $TaxClass = $this->GetWooCommerceTaxIdByEasifyTaxId($Product->TaxId);
     275
     276                // pricing
     277                update_post_meta($ProductId, '_sku', $Product->SKU);
     278                update_post_meta($ProductId, '_price', $Price);
     279                update_post_meta($ProductId, '_regular_price', $Price);
     280                update_post_meta($ProductId, '_sale_price', $Price);
     281                update_post_meta($ProductId, '_sale_price_dates_from    ', '');
     282                update_post_meta($ProductId, '_sale_price_dates_to', '');
     283                update_post_meta($ProductId, '_tax_status', 'taxable');
     284                update_post_meta($ProductId, '_tax_class', strtolower($TaxClass));                               
     285            }
     286            else{
     287               Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating of product price disabled.");
     288            }
     289           
     290           
     291            // Determine whether ignore product uploads
     292            if ($this->easify_options->get_easify_dont_upload_products())
     293            {
     294                Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Easify plugin settings dictate ignore product uploads. Not uploading product.');               
     295                return;           
     296            }
     297           
     298            if ($this->easify_options->get_easify_ignore_product_updates())
     299            {
     300                Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Easify plugin settings dictate ignore product updates. Not updating info.');               
     301                return;           
     302            }           
     303           
     304            Easify_Logging::Log('Easify_WC_Shop.UpdateProduct()');
     305            // get product
     306            if (empty($this->easify_server)) {
     307                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Easify Server is NULL");
     308            }
     309     
     310            if ($Product->Published == FALSE) {
     311                Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Not published, deleting product and not updating.');
     312                $this->DeleteProduct($EasifySku);
     313                return;
     314            }
     315
     316             if ($Product->Discontinued == 'true') {
     317                Easify_Logging::Log('Easify_WC_Shop.UpdateProduct() - Discontinued, deleting product and not updating.');
     318                $this->DeleteProduct($EasifySku);
     319                return;
     320            }           
     321           
     322            // If this is an update to a delivery product, don't need to update
     323            // anything other than price (which has already been handled).
     324            if (!$this->easify_options->get_easify_dont_update_product_prices())
     325            {
     326                // catch reserved delivery SKUs and update delivery prices
     327                if ($this->UpdateDeliveryPrice($Product->SKU, $Price))
     328                {
     329                    Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Product was delivery SKU, updated price and nothing more to do.");
     330                    return;               
     331                }               
     332            }
     333       
     334            // sanitise weight value
     335            $Product->Weight = (isset($Product->Weight) && is_numeric($Product->Weight) ? $Product->Weight : 0);
     336
     337            if (!$this->easify_options->get_dont_overwrite_woocommerce_product_categories())
     338            {
     339                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Updating product categories...");
     340             
     341                // get Easify product categories
     342                $EasifyCategories = $this->easify_server->GetEasifyProductCategories();
     343
     344                // get Easify category description by the Easify category id
     345                $CategoryDescription = $this->easify_server->GetEasifyCategoryDescriptionFromEasifyCategoryId($EasifyCategories, $Product->CategoryId);
     346
     347                // get Easify product sub categories by Easify category id
     348                $EasifySubCategories = $this->easify_server->GetEasifyProductSubCategoriesByCategory($Product->CategoryId);
     349
     350                // get Easify sub category description by Easify sub category id
     351                $SubCategoryDescription = $this->easify_server->GetEasifyCategoryDescriptionFromEasifyCategoryId($EasifySubCategories, $Product->SubcategoryId);
     352
     353                // insert new category if needed and return WooCommerce category id
     354                $CategoryId = $this->InsertCategoryIntoWooCommerce($CategoryDescription, $CategoryDescription);
     355
     356                // insert new sub category if needed and return WooCommerce sub category id
     357                $SubCategoryId = $this->InsertSubCategoryIntoWooCommerce($SubCategoryDescription, $SubCategoryDescription, $CategoryId);               
     358            }
     359           
     360            // create a WooCommerce stub for the new product
     361            $ProductStub = array(
     362                'ID' => $ProductId,
     363                'post_title' => $Product->Description,
     364                'post_content' => '',
     365                'post_status' => 'publish',
     366                'post_type' => 'product'
     367            );
     368           
     369            // insert product record and get WooCommerce product id
     370            $ProductId = wp_update_post($ProductStub);
     371
     372            if (!$this->easify_options->get_dont_overwrite_woocommerce_product_categories())
     373            {
     374                Easify_Logging::Log("Easify_WC_Shop.UpdateProduct() - Linking new product categories...");
     375               
     376                // link subcategory to product
     377                wp_set_post_terms($ProductId, array($SubCategoryId), "product_cat");           
     378            }
     379
     380            // Easify_Logging::Log("UpdateProduct.TaxClass: " . $TaxClass);
     381
     382            /*
     383              flesh out product record meta data
     384             */
     385                                 
    369386            // physical properties
    370387            update_post_meta($ProductId, '_weight', $Product->Weight);
  • easify-server-woocommerce/trunk/readme.txt

    r2473139 r2489733  
    44Tags: easify, epos, epos software, stock control software, accounting software, invoicing software, small business software, ecommerce, e-commerce, woothemes, wordpress ecommerce, woocommerce, shopping cart
    55Requires at least: 4.0
    6 Tested up to: 5.6
    7 Stable tag: 4.25
     6Tested up to: 5.7
     7Stable tag: 4.26
    88License: GPLv2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    101101
    102102== Changelog ==
     103= 4.26 =
     104* Added support for WooCommerce coupons for mixed VAT orders.
     105* Resolved issue where for certain Easify WooCommerce Plugin product
     106* settings, product stock level and price changes were not getting
     107* uploaded to WooCommerce.
     108* Verified support for WordPress 5.7 and WooCommerce 5.0.
     109* Various code refactorings.
    103110= 4.25 =
    104111* Added support for native WooCommerce Stripe payment plugin on Payments tab of
     
    181188
    182189== Upgrade Notice ==
    183 = 4.25 =
    184 * Added support for native WooCommerce Stripe payment plugin on Payments tab of
    185 Easify Plugin Settings page.
     190= 4.26 =
     191* Added support for WooCommerce coupons for mixed VAT orders.
     192* Resolved issue where for certain Easify WooCommerce Plugin product
     193* settings, product stock level and price changes were not getting
     194* uploaded to WooCommerce.
     195* Verified support for WordPress 5.7 and WooCommerce 5.0.
Note: See TracChangeset for help on using the changeset viewer.