plugin updates
This commit is contained in:
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Fired during plugin activation
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'Hubwoo_Activator' ) ) {
|
||||
|
||||
/**
|
||||
* Fired during plugin activation.
|
||||
*
|
||||
* This class defines all code necessary to run during the plugin's activation.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class Hubwoo_Activator {
|
||||
|
||||
/**
|
||||
* Schedule the realtime sync for HubSpot WooCommerce Integration
|
||||
*
|
||||
* Create a log file in the WooCommerce defined log directory
|
||||
* and use the same for the logging purpose of our plugin.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function activate() {
|
||||
|
||||
update_option( 'hubwoo_plugin_activated_time', time() );
|
||||
|
||||
fopen( WC_LOG_DIR . 'hubspot-for-woocommerce-logs.log', 'a' );
|
||||
|
||||
if ( ! as_next_scheduled_action( 'hubwoo_cron_schedule' ) ) {
|
||||
|
||||
as_schedule_recurring_action( time(), 300, 'hubwoo_cron_schedule' );
|
||||
}
|
||||
|
||||
if ( ! as_next_scheduled_action( 'hubwoo_deals_sync_check' ) ) {
|
||||
|
||||
as_schedule_recurring_action( time(), 300, 'hubwoo_deals_sync_check' );
|
||||
}
|
||||
|
||||
if ( ! as_next_scheduled_action( 'hubwoo_ecomm_deal_update' ) ) {
|
||||
|
||||
as_schedule_recurring_action( time(), 180, 'hubwoo_ecomm_deal_update' );
|
||||
}
|
||||
|
||||
if ( ! as_next_scheduled_action( 'hubwoo_products_sync_check' ) ) {
|
||||
|
||||
as_schedule_recurring_action( time(), 300, 'hubwoo_products_sync_check' );
|
||||
}
|
||||
|
||||
if ( ! as_next_scheduled_action( 'hubwoo_check_logs' ) ) {
|
||||
|
||||
as_schedule_recurring_action( time(), 86400, 'hubwoo_check_logs' );
|
||||
}
|
||||
|
||||
if ( ! as_next_scheduled_action( 'huwoo_abncart_clear_old_cart' ) ) {
|
||||
|
||||
as_schedule_recurring_action( time(), 86400, 'huwoo_abncart_clear_old_cart' );
|
||||
}
|
||||
|
||||
// Create log table in database.
|
||||
Hubwoo::hubwoo_create_log_table( Hubwoo::get_current_crm_name( 'slug' ) );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,185 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Handles all CSV generation functions.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manage all csv import.
|
||||
*
|
||||
* Provide a list of functions to manage all the information
|
||||
* about import csv handling.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class Hubwoo_Csv_Handler {
|
||||
|
||||
/**
|
||||
* Working dircetory path( Predefined Server properties ).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $working_path = '';
|
||||
|
||||
/**
|
||||
* Base dirctory path( Predefined Server properties ).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $base_dir = HUBWOO_ABSPATH;
|
||||
|
||||
/**
|
||||
* Activity folder( Data holding properties ).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $folder;
|
||||
|
||||
/**
|
||||
* Path validation.
|
||||
*
|
||||
* @var bool|array
|
||||
*/
|
||||
public $dirpath;
|
||||
|
||||
/**
|
||||
* Constructor function.
|
||||
*
|
||||
* @param string $folder Activity folder name.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct( $folder = 'hubwoo-temp-dir' ) {
|
||||
|
||||
$this->folder = $folder;
|
||||
|
||||
// Create Base Activity Directory.
|
||||
$this->working_path = $this->base_dir . $this->folder;
|
||||
$this->dirpath = $this->check_and_create_folder( $this->working_path );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Library functions :: Check/create folder.
|
||||
*
|
||||
* @param string $path Path to create folder.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function check_and_create_folder( $path = '' ) {
|
||||
|
||||
if ( ! empty( $path ) && ! is_dir( $path ) && is_writable( $this->base_dir ) ) {
|
||||
|
||||
$directory = mkdir( $path, 0755, true );
|
||||
|
||||
if ( $directory ) {
|
||||
return array(
|
||||
'status' => $directory,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return array(
|
||||
'status' => false,
|
||||
'message' => 'We do not have write permission to create dir/file.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Contacts csv.
|
||||
*
|
||||
* @param array $contacts An array of all users properties data.
|
||||
*/
|
||||
public function generate_contacts_csv( $contacts = array() ) {
|
||||
|
||||
$contact_file = fopen( $this->working_path . '/contacts-import.csv', 'w' );
|
||||
|
||||
fputcsv(
|
||||
$contact_file,
|
||||
array(
|
||||
'Firstname',
|
||||
'Lastname',
|
||||
'Email Address',
|
||||
'Company',
|
||||
'Phone Number',
|
||||
'Mobile Number',
|
||||
'Address',
|
||||
'City',
|
||||
'State',
|
||||
'Country',
|
||||
'Postcode',
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $contacts as $key => $value ) {
|
||||
$contact_cells = $this->format_contact_csv_cells( $value );
|
||||
|
||||
fputcsv(
|
||||
$contact_file,
|
||||
array(
|
||||
$contact_cells['firstname'],
|
||||
$contact_cells['lastname'],
|
||||
$contact_cells['email'],
|
||||
$contact_cells['company'],
|
||||
$contact_cells['phone'],
|
||||
$contact_cells['mobilephone'],
|
||||
$contact_cells['address'],
|
||||
$contact_cells['city'],
|
||||
$contact_cells['state'],
|
||||
$contact_cells['country'],
|
||||
$contact_cells['zip'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return fclose( $contact_file );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set of contact propertieis to be mapped in CSV.
|
||||
*
|
||||
* @param array $contact An array of user data.
|
||||
*
|
||||
* @return array An array of set of contact properties
|
||||
*/
|
||||
public function format_contact_csv_cells( $contact = array() ) {
|
||||
|
||||
$email = ! empty( $contact['email'] ) ? $contact['email'] : false;
|
||||
unset( $contact['email'] );
|
||||
|
||||
$properties = ! empty( $contact['properties'] ) ? $this->format_contact_properties( $contact['properties'] ) : array();
|
||||
$properties['email'] = ! empty( $email ) ? $email : false;
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format properties of an array of contact data.
|
||||
*
|
||||
* @param array $properties Set of properties to format.
|
||||
*
|
||||
* @return string Resultant of an array key.
|
||||
*/
|
||||
public function format_contact_properties( $properties = array() ) {
|
||||
|
||||
$resultant = array();
|
||||
|
||||
if ( ! empty( $properties ) && is_array( $properties ) ) {
|
||||
|
||||
foreach ( $properties as $key => $value ) {
|
||||
$resultant[ $value['property'] ] = ! empty( $value['value'] ) ? $value['value'] : false;
|
||||
}
|
||||
}
|
||||
|
||||
return $resultant;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Fired during plugin deactivation
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Fired during plugin deactivation.
|
||||
*
|
||||
* This class defines all code necessary to run during the plugin's deactivation.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'Hubwoo_Deactivator' ) ) {
|
||||
/**
|
||||
* Fired during plugin de activation.
|
||||
*
|
||||
* This class defines all code necessary to run during the plugin's de activation.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class Hubwoo_Deactivator {
|
||||
|
||||
/**
|
||||
* Clear log file saved for HubSpot API call logging. (use period)
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function deactivate() {
|
||||
|
||||
as_unschedule_action( 'hubwoo_cron_schedule' );
|
||||
as_unschedule_action( 'hubwoo_deals_sync_check' );
|
||||
as_unschedule_action( 'hubwoo_products_sync_check' );
|
||||
as_unschedule_action( 'hubwoo_deal_update_schedule' );
|
||||
as_unschedule_action( 'hubwoo_products_status_background' );
|
||||
as_unschedule_action( 'hubwoo_products_sync_background' );
|
||||
as_unschedule_action( 'hubwoo_contacts_sync_background' );
|
||||
as_unschedule_action( 'hubwoo_check_logs' );
|
||||
as_unschedule_action( 'hubwoo_ecomm_deal_upsert' );
|
||||
as_unschedule_action( 'hubwoo_ecomm_deal_update' );
|
||||
as_unschedule_action( 'huwoo_abncart_clear_old_cart' );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Define the internationalization functionality
|
||||
*
|
||||
* Loads and defines the internationalization files for this plugin
|
||||
* so that it is ready for translation.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Define the internationalization functionality.
|
||||
*
|
||||
* Loads and defines the internationalization files for this plugin
|
||||
* so that it is ready for translation.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class Hubwoo_I18n {
|
||||
|
||||
/**
|
||||
* Load the plugin text domain for translation.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function load_plugin_textdomain() {
|
||||
|
||||
load_plugin_textdomain(
|
||||
'makewebbetter-hubspot-for-woocommerce',
|
||||
false,
|
||||
dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Register all actions and filters for the plugin
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Register all actions and filters for the plugin.
|
||||
*
|
||||
* Maintain a list of all hooks that are registered throughout
|
||||
* the plugin, and register them with the WordPress API. Call the
|
||||
* run function to execute the list of actions and filters.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class Hubwoo_Loader {
|
||||
|
||||
/**
|
||||
* The array of actions registered with WordPress.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var array $actions The actions registered with WordPress to fire when the plugin loads.
|
||||
*/
|
||||
protected $actions;
|
||||
|
||||
/**
|
||||
* The array of filters registered with WordPress.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var array $filters The filters registered with WordPress to fire when the plugin loads.
|
||||
*/
|
||||
protected $filters;
|
||||
|
||||
/**
|
||||
* Initialize the collections used to maintain the actions and filters.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->actions = array();
|
||||
$this->filters = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new action to the collection to be registered with WordPress.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $hook The name of the WordPress action that is being registered.
|
||||
* @param object $component A reference to the instance of the object on which the action is defined.
|
||||
* @param string $callback The name of the function definition on the $component.
|
||||
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
|
||||
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
|
||||
*/
|
||||
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
|
||||
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new filter to the collection to be registered with WordPress.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $hook The name of the WordPress filter that is being registered.
|
||||
* @param object $component A reference to the instance of the object on which the filter is defined.
|
||||
* @param string $callback The name of the function definition on the $component.
|
||||
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
|
||||
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
|
||||
*/
|
||||
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
|
||||
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* A utility function that is used to register the actions and hooks into a single
|
||||
* collection.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param array $hooks The collection of hooks that is being registered (that is, actions or filters).
|
||||
* @param string $hook The name of the WordPress filter that is being registered.
|
||||
* @param object $component A reference to the instance of the object on which the filter is defined.
|
||||
* @param string $callback The name of the function definition on the $component.
|
||||
* @param int $priority The priority at which the function should be fired.
|
||||
* @param int $accepted_args The number of arguments that should be passed to the $callback.
|
||||
* @return array The collection of actions and filters registered with WordPress.
|
||||
*/
|
||||
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
|
||||
|
||||
$hooks[] = array(
|
||||
'hook' => $hook,
|
||||
'component' => $component,
|
||||
'callback' => $callback,
|
||||
'priority' => $priority,
|
||||
'accepted_args' => $accepted_args,
|
||||
);
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the filters and actions with WordPress.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function run() {
|
||||
|
||||
foreach ( $this->filters as $hook ) {
|
||||
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
|
||||
}
|
||||
|
||||
foreach ( $this->actions as $hook ) {
|
||||
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,251 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Settings template for RFM ratings to be used over HubSpot.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
if ( ! class_exists( 'WP_List_Table' ) ) {
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'Hubwoo_RFM_Configuration' ) ) {
|
||||
/**
|
||||
* Setup RFM settings board display.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class Hubwoo_RFM_Configuration extends WP_List_Table {
|
||||
/**
|
||||
* Prepare the RFM Settings data table.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function prepare_items() {
|
||||
|
||||
$per_page = 10;
|
||||
$columns = $this->get_columns();
|
||||
$hidden = array();
|
||||
$sortable = array();
|
||||
$this->_column_headers = array( $columns, $hidden, $sortable );
|
||||
$data = $this->table_data();
|
||||
$current_page = $this->get_pagenum();
|
||||
$total_items = count( $data );
|
||||
$this->set_pagination_args(
|
||||
array(
|
||||
'total_items' => $total_items,
|
||||
'per_page' => $per_page,
|
||||
)
|
||||
);
|
||||
$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
|
||||
$this->items = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the RFM Setting column.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get_columns() {
|
||||
|
||||
$columns = array(
|
||||
'score' => __( 'Score', 'makewebbetter-hubspot-for-woocommerce' ) . '<p>(' . __( 'Ratings for RFM Segmentation', 'makewebbetter-hubspot-for-woocommerce' ) . ')</p>',
|
||||
'recency' => __( 'Recency', 'makewebbetter-hubspot-for-woocommerce' ) . '<p>(' . __( 'Days Since last Order', 'makewebbetter-hubspot-for-woocommerce' ) . ')</p>',
|
||||
'frequency' => __( 'Frequency', 'makewebbetter-hubspot-for-woocommerce' ) . '<p>(' . __( 'Total Orders Placed', 'makewebbetter-hubspot-for-woocommerce' ) . ')</p>',
|
||||
'monetary' => __( 'Monetary', 'makewebbetter-hubspot-for-woocommerce' ) . '<p>(' . __( 'Total Money Spent', 'makewebbetter-hubspot-for-woocommerce' ) . ')</p>',
|
||||
);
|
||||
return $columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the table data its values.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function table_data() {
|
||||
|
||||
$temp_data = array();
|
||||
|
||||
$rfm_settings = array(
|
||||
'score_5' => 5,
|
||||
'score_4' => 4,
|
||||
'score_3' => 3,
|
||||
'score_2' => 2,
|
||||
'score_1' => 1,
|
||||
);
|
||||
$hubwoo_rfm_at_5 = get_option(
|
||||
'hubwoo_rfm_5',
|
||||
array(
|
||||
0 => 30,
|
||||
1 => 20,
|
||||
2 => 1000,
|
||||
)
|
||||
);
|
||||
$hubwoo_from_rfm_4 = get_option(
|
||||
'hubwoo_from_rfm_4',
|
||||
array(
|
||||
0 => 31,
|
||||
1 => 10,
|
||||
2 => 750,
|
||||
)
|
||||
);
|
||||
$hubwoo_to_rfm_4 = get_option(
|
||||
'hubwoo_to_rfm_4',
|
||||
array(
|
||||
0 => 90,
|
||||
1 => 20,
|
||||
2 => 1000,
|
||||
)
|
||||
);
|
||||
$hubwoo_from_rfm_3 = get_option(
|
||||
'hubwoo_from_rfm_3',
|
||||
array(
|
||||
0 => 91,
|
||||
1 => 5,
|
||||
2 => 500,
|
||||
)
|
||||
);
|
||||
$hubwoo_to_rfm_3 = get_option(
|
||||
'hubwoo_to_rfm_3',
|
||||
array(
|
||||
0 => 180,
|
||||
1 => 10,
|
||||
2 => 750,
|
||||
)
|
||||
);
|
||||
$hubwoo_from_rfm_2 = get_option(
|
||||
'hubwoo_from_rfm_2',
|
||||
array(
|
||||
0 => 181,
|
||||
1 => 2,
|
||||
2 => 250,
|
||||
)
|
||||
);
|
||||
$hubwoo_to_rfm_2 = get_option(
|
||||
'hubwoo_to_rfm_2',
|
||||
array(
|
||||
0 => 360,
|
||||
1 => 5,
|
||||
2 => 500,
|
||||
)
|
||||
);
|
||||
$hubwoo_rfm_at_1 = get_option(
|
||||
'hubwoo_rfm_1',
|
||||
array(
|
||||
0 => 361,
|
||||
1 => 2,
|
||||
2 => 250,
|
||||
)
|
||||
);
|
||||
|
||||
foreach ( $rfm_settings as $key => $single_setting ) {
|
||||
|
||||
if ( 5 == $single_setting ) {
|
||||
|
||||
$new_data = array(
|
||||
'score' => '<h2>' . $single_setting . '</h2>',
|
||||
'recency' => '<p><span>' . __( 'Less than', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_rfm_5[]" value = "' . $hubwoo_rfm_at_5[0] . '"></p>',
|
||||
'frequency' => '<p><span>' . __( 'More than', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_rfm_5[]" value = "' . $hubwoo_rfm_at_5[1] . '"></p>',
|
||||
'monetary' => '<p><span>' . __( 'More than', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_rfm_5[]" value = "' . $hubwoo_rfm_at_5[2] . '"></p>',
|
||||
);
|
||||
} elseif ( 1 == $single_setting ) {
|
||||
|
||||
$new_data = array(
|
||||
'score' => '<h2>' . $single_setting . '</h2>',
|
||||
'recency' => '<p><span>' . __( 'More than', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_rfm_1[]" value="' . $hubwoo_rfm_at_1[0] . '"></p>',
|
||||
'frequency' => '<p><span>' . __( 'Less than', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_rfm_1[]" value="' . $hubwoo_rfm_at_1[1] . '"></p>',
|
||||
'monetary' => '<p><span>' . __( 'Less than', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_rfm_1[]" value="' . $hubwoo_rfm_at_1[2] . '"></p>',
|
||||
);
|
||||
} else {
|
||||
|
||||
if ( 4 == $single_setting ) {
|
||||
|
||||
$rfm_from_0 = $hubwoo_from_rfm_4[0];
|
||||
$rfm_from_1 = $hubwoo_from_rfm_4[1];
|
||||
$rfm_from_2 = $hubwoo_from_rfm_4[2];
|
||||
$rfm_to_0 = $hubwoo_to_rfm_4[0];
|
||||
$rfm_to_1 = $hubwoo_to_rfm_4[1];
|
||||
$rfm_to_2 = $hubwoo_to_rfm_4[2];
|
||||
} elseif ( 3 == $single_setting ) {
|
||||
|
||||
$rfm_from_0 = $hubwoo_from_rfm_3[0];
|
||||
$rfm_from_1 = $hubwoo_from_rfm_3[1];
|
||||
$rfm_from_2 = $hubwoo_from_rfm_3[2];
|
||||
$rfm_to_0 = $hubwoo_to_rfm_3[0];
|
||||
$rfm_to_1 = $hubwoo_to_rfm_3[1];
|
||||
$rfm_to_2 = $hubwoo_to_rfm_3[2];
|
||||
} elseif ( 2 == $single_setting ) {
|
||||
|
||||
$rfm_from_0 = $hubwoo_from_rfm_2[0];
|
||||
$rfm_from_1 = $hubwoo_from_rfm_2[1];
|
||||
$rfm_from_2 = $hubwoo_from_rfm_2[2];
|
||||
$rfm_to_0 = $hubwoo_to_rfm_2[0];
|
||||
$rfm_to_1 = $hubwoo_to_rfm_2[1];
|
||||
$rfm_to_2 = $hubwoo_to_rfm_2[2];
|
||||
}
|
||||
|
||||
$new_data = array(
|
||||
'score' => '<h2>' . $single_setting . '</h2>',
|
||||
'recency' => '<p><span>' . __( 'From', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_from_rfm_' . $single_setting . '[]" value="' . $rfm_from_0 . '"></p><p><span>' . __( 'To', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_to_rfm_' . $single_setting . '[]" value="' . $rfm_to_0 . '"></p>',
|
||||
'frequency' => '<p><span>' . __( 'From', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_from_rfm_' . $single_setting . '[]" value="' . $rfm_from_1 . '"></p><p><span>' . __( 'To', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_to_rfm_' . $single_setting . '[]" value="' . $rfm_to_1 . '"></p>',
|
||||
'monetary' => '<p><span>' . __( 'From', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_from_rfm_' . $single_setting . '[]" value="' . $rfm_from_2 . '"></p><p><span>' . __( 'To', 'makewebbetter-hubspot-for-woocommerce' ) . '</span><input size="5" type="number" class="hubwoo_rfm_data_fields" min="1" name="hubwoo_to_rfm_' . $single_setting . '[]" value="' . $rfm_to_2 . '"></p>',
|
||||
);
|
||||
}
|
||||
|
||||
$temp_data[] = $new_data;
|
||||
}
|
||||
|
||||
return $temp_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return default column name.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $item rfm setting column type.
|
||||
* @param string $column_name column name.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function column_default( $item, $column_name ) {
|
||||
|
||||
switch ( $column_name ) {
|
||||
case 'score':
|
||||
case 'recency':
|
||||
case 'frequency':
|
||||
case 'monetary':
|
||||
return $item[ $column_name ];
|
||||
default:
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the row placeholder.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function display_rows_or_placeholder() {
|
||||
|
||||
if ( $this->has_items() ) {
|
||||
|
||||
$this->display_rows();
|
||||
} else {
|
||||
|
||||
?>
|
||||
<tr class="no-items"><td class="colspanchange" colspan="<?php echo esc_attr( $this->get_column_count() ); ?>">
|
||||
<?php
|
||||
$this->no_items();
|
||||
?>
|
||||
</td></tr>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Enums.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugin Enums.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooConst extends HubWooEnum {
|
||||
|
||||
const HUBWOOWORKFLOW = 'Workflow';
|
||||
const CHECKOUTFORM = 'Checkout';
|
||||
const CHECKOUTFORMNAME = 'Checkout Form';
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,311 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* All customer details.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Stores all customer data that needs to be updated on hubspot.
|
||||
*
|
||||
* Provide a list of properties and associated data for customer
|
||||
* so that at the time of updating a customer on hubspot we can
|
||||
* simply create an instance of this class and get everything
|
||||
* managed.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubWooCustomer {
|
||||
|
||||
/**
|
||||
* Contact in the form of acceptable by hubspot.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var json
|
||||
*/
|
||||
public $contact;
|
||||
|
||||
/**
|
||||
* WooCommerce Customer ID.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var json
|
||||
*/
|
||||
public $_contact_id;
|
||||
|
||||
/**
|
||||
* Contact Properties.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var Array
|
||||
*/
|
||||
private $_properties = array();
|
||||
|
||||
/**
|
||||
* Instance of HubWooPropertyCallbacks class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var HubWooPropertyCallbacks
|
||||
*/
|
||||
private $_callback_instance = null;
|
||||
|
||||
/**
|
||||
* Load the modified customer properties.
|
||||
*
|
||||
* Set all the modified customer properties so that they will be
|
||||
* ready in the form of directly acceptable by hubspot api.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $contact_id ID of the user.
|
||||
*/
|
||||
public function __construct( $contact_id ) {
|
||||
|
||||
// load the contact id in the class property.
|
||||
$this->_contact_id = $contact_id;
|
||||
|
||||
// store the instance of property callback.
|
||||
$this->_callback_instance = new HubWooPropertyCallbacks( $this->_contact_id );
|
||||
|
||||
// prepare the modified fields data and store it in the contact.
|
||||
$this->prepare_modified_fields();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user email.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get_email() {
|
||||
|
||||
return $this->_callback_instance->_get_mail();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all properties with values.
|
||||
*
|
||||
* @return array and key value pair array of properties.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get_contact_properties() {
|
||||
|
||||
// let others decide if they have modified fields in there integration.
|
||||
$this->_properties = apply_filters( 'hubwoo_contact_modified_fields', $this->_properties, $this->_contact_id );
|
||||
return $this->_properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all properties with values.
|
||||
*
|
||||
* @param array $properties array of contact properties.
|
||||
* @return array $properties array of properties with user value.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get_user_data_properties( $properties ) {
|
||||
|
||||
$fname = get_user_meta( $this->_contact_id, 'first_name', true );
|
||||
if ( ! empty( $fname ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'firstname',
|
||||
'value' => $fname,
|
||||
);
|
||||
}
|
||||
|
||||
$lname = get_user_meta( $this->_contact_id, 'last_name', true );
|
||||
if ( ! empty( $lname ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'lastname',
|
||||
'value' => $lname,
|
||||
);
|
||||
}
|
||||
|
||||
$cname = get_user_meta( $this->_contact_id, 'billing_company', true );
|
||||
if ( ! empty( $cname ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'company',
|
||||
'value' => $cname,
|
||||
);
|
||||
}
|
||||
|
||||
$phone = get_user_meta( $this->_contact_id, 'billing_phone', true );
|
||||
if ( ! empty( $phone ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'mobilephone',
|
||||
'value' => $phone,
|
||||
);
|
||||
$properties[] = array(
|
||||
'property' => 'phone',
|
||||
'value' => $phone,
|
||||
);
|
||||
}
|
||||
|
||||
$city = get_user_meta( $this->_contact_id, 'billing_city', true );
|
||||
if ( ! empty( $city ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'city',
|
||||
'value' => $city,
|
||||
);
|
||||
}
|
||||
|
||||
$state = get_user_meta( $this->_contact_id, 'billing_state', true );
|
||||
if ( ! empty( $state ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'state',
|
||||
'value' => $state,
|
||||
);
|
||||
}
|
||||
|
||||
$country = get_user_meta( $this->_contact_id, 'billing_country', true );
|
||||
if ( ! empty( $country ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'country',
|
||||
'value' => Hubwoo::map_country_by_abbr( $country ),
|
||||
);
|
||||
}
|
||||
|
||||
$address1 = get_user_meta( $this->_contact_id, 'billing_address_1', true );
|
||||
$address2 = get_user_meta( $this->_contact_id, 'billing_address_2', true );
|
||||
|
||||
if ( ! empty( $address1 ) || ! empty( $address2 ) ) {
|
||||
$address = $address1 . ' ' . $address2;
|
||||
$properties[] = array(
|
||||
'property' => 'address',
|
||||
'value' => $address,
|
||||
);
|
||||
}
|
||||
|
||||
$postcode = get_user_meta( $this->_contact_id, 'billing_postcode', true );
|
||||
if ( ! empty( $postcode ) ) {
|
||||
$properties[] = array(
|
||||
'property' => 'zip',
|
||||
'value' => $postcode,
|
||||
);
|
||||
}
|
||||
|
||||
$prop_index = array_search( 'customer_new_order', array_column( $properties, 'property' ) );
|
||||
|
||||
$customer_new_order_flag = 'no';
|
||||
|
||||
if ( ! Hubwoo_Admin::hubwoo_check_for_cart( $properties ) ) {
|
||||
if ( Hubwoo_Admin::hubwoo_check_for_properties( 'order_recency_rating', 5, $properties ) ) {
|
||||
if ( Hubwoo_Admin::hubwoo_check_for_properties( 'last_order_status', get_option( 'hubwoo_no_status', 'wc-completed' ), $properties ) ) {
|
||||
$customer_new_order_flag = 'yes';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $prop_index ) {
|
||||
$properties[ $prop_index ]['value'] = $customer_new_order_flag;
|
||||
} else {
|
||||
$properties[] = array(
|
||||
'property' => 'customer_new_order',
|
||||
'value' => $customer_new_order_flag,
|
||||
);
|
||||
}
|
||||
|
||||
$properties = apply_filters( 'hubwoo_unset_workflow_properties', $properties );
|
||||
$properties = apply_filters( 'hubwoo_map_new_properties', $properties, $this->_contact_id );
|
||||
|
||||
if ( Hubwoo_Admin::hubwoo_check_for_cart( $properties ) ) {
|
||||
update_user_meta( $this->_contact_id, 'hubwoo_pro_user_cart_sent', 'yes' );
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format modified fields of customer.
|
||||
*
|
||||
* Check for all the modified fields till the last update
|
||||
* and prepare them in the hubspot api acceptable form.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function prepare_modified_fields() {
|
||||
|
||||
$hubwoo_vid = $this->get_hubwoo_vid();
|
||||
|
||||
if ( ! empty( $hubwoo_vid ) && $hubwoo_vid > 0 ) {
|
||||
|
||||
$modified_fields = $this->get_contact_modified_fields();
|
||||
} else {
|
||||
|
||||
$modified_fields = HubWooContactProperties::get_instance()->hubwoo_get_filtered_properties();
|
||||
}
|
||||
|
||||
if ( is_array( $modified_fields ) && count( $modified_fields ) ) {
|
||||
|
||||
foreach ( $modified_fields as $group_fields ) {
|
||||
|
||||
if ( is_array( $group_fields ) ) {
|
||||
|
||||
foreach ( $group_fields as $field ) {
|
||||
$property = $this->_prepare_property( $field );
|
||||
|
||||
if ( is_array( $property ) && ! empty( $property['value'] ) ) {
|
||||
|
||||
$this->_properties[] = array(
|
||||
'property' => $property['property'],
|
||||
'value' => $property['value'],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the contact is not uploaded to hubspot.
|
||||
*
|
||||
* @return Int/null hubspot vid if pre-uploaded either null.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function get_hubwoo_vid() {
|
||||
|
||||
return get_user_meta( $this->_contact_id, 'hubwoo_vid', true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get modified fields since last update of the contact.
|
||||
*
|
||||
* @return Array Array of fields modified.
|
||||
*/
|
||||
public function get_contact_modified_fields() {
|
||||
|
||||
$modified_fields = get_user_meta( $this->_contact_id, 'hubwoo_modified_fields', true );
|
||||
|
||||
if ( ! is_array( $modified_fields ) ) {
|
||||
|
||||
$modified_fields = array();
|
||||
}
|
||||
|
||||
return $modified_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare property in the form of key value accepted by hubspot.
|
||||
*
|
||||
* @param array $property array of the property details to validate the value.
|
||||
* @return array formatted key value pair.
|
||||
*/
|
||||
public function _prepare_property( $property ) {
|
||||
|
||||
$property_name = isset( $property['name'] ) ? $property['name'] : '';
|
||||
|
||||
if ( ! empty( $property_name ) ) {
|
||||
|
||||
$property_val = $this->_callback_instance->_get_property_value( $property_name, $this->_contact_id );
|
||||
|
||||
$property = array(
|
||||
'property' => $property_name,
|
||||
'value' => $property_val,
|
||||
);
|
||||
|
||||
return $property;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,376 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Handling data for sync.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handling Data for Syncing Prodcess
|
||||
*
|
||||
* All the Data required for handling Users and
|
||||
* Order Data required by the plugin.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooDataSync {
|
||||
|
||||
/**
|
||||
* Retreive all of the
|
||||
* required user Data and Count
|
||||
*
|
||||
* @param int $count count of users to be get.
|
||||
* @param string $response_type sync type.
|
||||
* @param int $limit offset for users.
|
||||
*/
|
||||
public function hubwoo_get_all_unique_user( $count = false, $response_type = 'customer', $limit = 20 ) {
|
||||
|
||||
$limit = $count ? -1 : $limit;
|
||||
$unique_users = 0;
|
||||
$args = array();
|
||||
$date_range = false;
|
||||
if ( 'yes' == get_option( 'hubwoo_customers_manual_sync', 'no' ) ) {
|
||||
$date_range = true;
|
||||
$from_date = get_option( 'hubwoo_users_from_date', gmdate( 'd-m-Y' ) );
|
||||
$upto_date = get_option( 'hubwoo_users_upto_date', gmdate( 'd-m-Y' ) );
|
||||
}
|
||||
|
||||
// checking for guest users in the array.
|
||||
$roles = get_option( 'hubwoo_customers_role_settings', array() );
|
||||
|
||||
if ( empty( $roles ) ) {
|
||||
global $hubwoo;
|
||||
$roles = array_keys( $hubwoo->hubwoo_get_user_roles() );
|
||||
$key = array_search( 'guest_user', $roles );
|
||||
if ( false !== $key ) {
|
||||
unset( $roles[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( in_array( 'guest_user', $roles ) ) {
|
||||
|
||||
$key = array_search( 'guest_user', $roles );
|
||||
|
||||
$order_statuses = get_option( 'hubwoo-selected-order-status', array() );
|
||||
|
||||
if ( empty( $order_statuses ) || ( ! is_array( $order_statuses ) && count( $order_statuses ) < 1 ) ) {
|
||||
$order_statuses = array_keys( wc_get_order_statuses() );
|
||||
}
|
||||
|
||||
if ( false !== $key ) {
|
||||
unset( $roles[ $key ] );
|
||||
}
|
||||
|
||||
$order_args = array(
|
||||
'return' => 'ids',
|
||||
'limit' => $limit,
|
||||
'type' => wc_get_order_types(),
|
||||
'status' => $order_statuses,
|
||||
'customer' => 0,
|
||||
'hubwoo_pro_guest_order' => 'synced',
|
||||
);
|
||||
|
||||
if ( $date_range ) {
|
||||
$order_args['date_modified'] = gmdate( 'Y-m-d', strtotime( $from_date ) ) . '...' . gmdate( 'Y-m-d', strtotime( $upto_date . ' +1 day' ) );
|
||||
}
|
||||
|
||||
$guest_orders = wc_get_orders( $order_args );
|
||||
|
||||
$guest_emails = array_unique( self::get_guest_sync_data( $guest_orders, true ) );
|
||||
|
||||
if ( 'guestOrder' == $response_type ) {
|
||||
return $guest_orders;
|
||||
}
|
||||
|
||||
if ( is_array( $guest_orders ) ) {
|
||||
$unique_users += count( $guest_emails );
|
||||
}
|
||||
} else {
|
||||
|
||||
if ( 'guestOrder' == $response_type ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $date_range ) {
|
||||
|
||||
$args['date_query'] = array(
|
||||
array(
|
||||
'after' => gmdate( 'd-m-Y', strtotime( $from_date ) ),
|
||||
'before' => gmdate( 'd-m-Y', strtotime( $upto_date . ' +1 day' ) ),
|
||||
'inclusive' => true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// creating args for registered users.
|
||||
$args['meta_query'] = array(
|
||||
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => 'hubwoo_pro_user_data_change',
|
||||
'compare' => 'NOT EXISTS',
|
||||
),
|
||||
array(
|
||||
'key' => 'hubwoo_pro_user_data_change',
|
||||
'value' => 'synced',
|
||||
'compare' => '!=',
|
||||
),
|
||||
);
|
||||
|
||||
$args['role__in'] = $roles;
|
||||
|
||||
$args['number'] = $limit;
|
||||
|
||||
$args['fields'] = 'ID';
|
||||
|
||||
$registered_users = get_users( $args );
|
||||
|
||||
if ( $count ) {
|
||||
$unique_users += count( $registered_users );
|
||||
return $unique_users;
|
||||
}
|
||||
|
||||
return $registered_users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve User Data
|
||||
* for Guest Users
|
||||
*
|
||||
* @param array $hubwoo_orders array of order to be synced.
|
||||
* @param bool $only_email true/false.
|
||||
*/
|
||||
public static function get_guest_sync_data( $hubwoo_orders, $only_email = false ) {
|
||||
|
||||
global $hubwoo;
|
||||
|
||||
$guest_user_emails = array();
|
||||
$guest_contacts = array();
|
||||
$guest_user_properties = array();
|
||||
|
||||
if ( ! empty( $hubwoo_orders ) && count( $hubwoo_orders ) ) {
|
||||
|
||||
foreach ( $hubwoo_orders as $order_id ) {
|
||||
|
||||
$hubwoo_guest_order = wc_get_order( $order_id );
|
||||
|
||||
if ( $hubwoo_guest_order instanceof WC_Order ) {
|
||||
|
||||
$guest_email = $hubwoo_guest_order->get_billing_email();
|
||||
|
||||
if ( ! empty( $guest_email ) && $only_email ) {
|
||||
$guest_user_emails[] = $guest_email;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( empty( $guest_email ) ) {
|
||||
|
||||
$hubwoo_guest_order->delete_meta_data('hubwoo_pro_guest_order');
|
||||
continue;
|
||||
}
|
||||
|
||||
$guest_order_callback = new HubwooGuestOrdersManager( $order_id );
|
||||
|
||||
$guest_user_properties = $guest_order_callback->get_order_related_properties( $order_id, $guest_email );
|
||||
|
||||
$guest_user_properties = $hubwoo->hubwoo_filter_contact_properties( $guest_user_properties );
|
||||
|
||||
$fname = $hubwoo_guest_order->get_billing_first_name();
|
||||
if ( ! empty( $fname ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'firstname',
|
||||
'value' => $fname,
|
||||
);
|
||||
}
|
||||
|
||||
$lname = $hubwoo_guest_order->get_billing_last_name();
|
||||
if ( ! empty( $lname ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'lastname',
|
||||
'value' => $lname,
|
||||
);
|
||||
}
|
||||
|
||||
$cname = $hubwoo_guest_order->get_billing_company();
|
||||
if ( ! empty( $cname ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'company',
|
||||
'value' => $cname,
|
||||
);
|
||||
}
|
||||
|
||||
$city = $hubwoo_guest_order->get_billing_city();
|
||||
if ( ! empty( $city ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'city',
|
||||
'value' => $city,
|
||||
);
|
||||
}
|
||||
|
||||
$state = $hubwoo_guest_order->get_billing_state();
|
||||
if ( ! empty( $state ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'state',
|
||||
'value' => $state,
|
||||
);
|
||||
}
|
||||
|
||||
$country = $hubwoo_guest_order->get_billing_country();
|
||||
if ( ! empty( $country ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'country',
|
||||
'value' => Hubwoo::map_country_by_abbr( $country ),
|
||||
);
|
||||
}
|
||||
|
||||
$address1 = $hubwoo_guest_order->get_billing_address_1();
|
||||
$address2 = $hubwoo_guest_order->get_billing_address_2();
|
||||
if ( ! empty( $address1 ) || ! empty( $address2 ) ) {
|
||||
$address = $address1 . ' ' . $address2;
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'address',
|
||||
'value' => $address,
|
||||
);
|
||||
}
|
||||
|
||||
$zip = $hubwoo_guest_order->get_billing_postcode();
|
||||
if ( ! empty( $zip ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'zip',
|
||||
'value' => $zip,
|
||||
);
|
||||
}
|
||||
|
||||
$guest_phone = $hubwoo_guest_order->get_billing_phone();
|
||||
|
||||
if ( ! empty( $guest_phone ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'mobilephone',
|
||||
'value' => $guest_phone,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'phone',
|
||||
'value' => $guest_phone,
|
||||
);
|
||||
}
|
||||
|
||||
$customer_new_order_flag = 'no';
|
||||
$prop_index = array_search( 'customer_new_order', array_column( $guest_user_properties, 'property' ) );
|
||||
|
||||
if ( Hubwoo_Admin::hubwoo_check_for_properties( 'order_recency_rating', 5, $guest_user_properties ) ) {
|
||||
|
||||
if ( Hubwoo_Admin::hubwoo_check_for_properties( 'last_order_status', get_option( 'hubwoo_no_status', 'wc-completed' ), $guest_user_properties ) ) {
|
||||
|
||||
$customer_new_order_flag = 'yes';
|
||||
}
|
||||
}
|
||||
|
||||
if ( $prop_index ) {
|
||||
$guest_user_properties[ $prop_index ]['value'] = $customer_new_order_flag;
|
||||
} else {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'customer_new_order',
|
||||
'value' => $customer_new_order_flag,
|
||||
);
|
||||
}
|
||||
|
||||
$guest_user_properties_data = array(
|
||||
'email' => $guest_email,
|
||||
'properties' => $guest_user_properties,
|
||||
);
|
||||
|
||||
$guest_contacts[] = $guest_user_properties_data;
|
||||
|
||||
$hubwoo_guest_order->delete_meta_data('hubwoo_pro_guest_order');
|
||||
}
|
||||
}
|
||||
}
|
||||
$response = $only_email ? $guest_user_emails : $guest_contacts;
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scheduling Background Tasks
|
||||
*/
|
||||
public function schedule_background_task() {
|
||||
|
||||
delete_option( 'hubwoo_ocs_data_synced' );
|
||||
delete_option( 'hubwoo_ocs_contacts_synced' );
|
||||
update_option( 'hubwoo_background_process_running', true );
|
||||
|
||||
if ( ! as_next_scheduled_action( 'hubwoo_contacts_sync_background' ) ) {
|
||||
as_schedule_recurring_action( time(), 300, 'hubwoo_contacts_sync_background' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts Scheduling once
|
||||
* the data has been retrieved
|
||||
*/
|
||||
public function hubwoo_start_schedule() {
|
||||
|
||||
$hubwoo_unique_users = $this->hubwoo_get_all_unique_user( true );
|
||||
|
||||
if ( $hubwoo_unique_users ) {
|
||||
$this->schedule_background_task();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieving Data For Registered
|
||||
* Users
|
||||
*
|
||||
* @param array $hubwoo_unique_users array of users to sync.
|
||||
* @return User Data
|
||||
*/
|
||||
public static function get_sync_data( $hubwoo_unique_users ) {
|
||||
|
||||
// basic data function for user ids.
|
||||
$contacts = array();
|
||||
$role__in = get_option( 'hubwoo-selected-user-roles', array() );
|
||||
$user_role_in = get_option( 'hubwoo_customers_role_settings', array() );
|
||||
|
||||
if ( ! empty( $hubwoo_unique_users ) && count( $hubwoo_unique_users ) ) {
|
||||
|
||||
foreach ( $hubwoo_unique_users as $key => $id ) {
|
||||
|
||||
$hubwoo_customer = new HubWooCustomer( $id );
|
||||
|
||||
$email = $hubwoo_customer->get_email();
|
||||
$user_data = get_user_by( 'email', $email );
|
||||
|
||||
if( ! empty( $user_data ) ) {
|
||||
|
||||
if( ! empty( $user_data->roles[0] ) ) {
|
||||
|
||||
$role = $user_data->roles[0];
|
||||
if ( in_array( $role, $role__in ) || in_array( $role, $user_role_in ) ) {
|
||||
|
||||
if ( empty( $email ) ) {
|
||||
delete_user_meta( $id, 'hubwoo_pro_user_data_change' );
|
||||
continue;
|
||||
}
|
||||
$properties = $hubwoo_customer->get_contact_properties();
|
||||
$user_properties = $hubwoo_customer->get_user_data_properties( $properties );
|
||||
$properties_data = array(
|
||||
'email' => $email,
|
||||
'properties' => $user_properties,
|
||||
);
|
||||
|
||||
$contacts[] = $properties_data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $contacts;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,169 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* All object details.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Stores all object data that needs to be updated on hubspot.
|
||||
*
|
||||
* Provide a list of properties and associated data for objects
|
||||
* so that at the time of updating a object on hubspot we can
|
||||
* simply create an instance of this class and get everything
|
||||
* managed.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooEcommObject {
|
||||
|
||||
/**
|
||||
* Object in the form of acceptable by hubspot.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var json
|
||||
*/
|
||||
public $_object_type;
|
||||
|
||||
/**
|
||||
* WooCommerce Object ID
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var json
|
||||
*/
|
||||
public $_object_id;
|
||||
|
||||
/**
|
||||
* Object Properties.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var Array
|
||||
*/
|
||||
private $_properties = array();
|
||||
|
||||
/**
|
||||
* Instance of HubwooEcommPropertyCallbacks class
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var $_callback_instance
|
||||
*/
|
||||
private $_callback_instance = null;
|
||||
|
||||
/**
|
||||
* Load the modified object properties.
|
||||
*
|
||||
* Set all the modified object properties so that they will be
|
||||
* ready in the form of directly acceptable by hubspot api.
|
||||
*
|
||||
* @param int $id object id.
|
||||
* @param string $object_type ecomm object type.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct( $id, $object_type ) {
|
||||
|
||||
// load the object id in the class property.
|
||||
$this->_object_id = $id;
|
||||
|
||||
// load the object type in the class property.
|
||||
$this->_object_type = $object_type;
|
||||
|
||||
// store the instance of property callback.
|
||||
$this->_callback_instance = new HubwooEcommPropertyCallbacks( $this->_object_id, $this->_object_type );
|
||||
|
||||
// prepare the modified fields data and store it in the object.
|
||||
$this->prepare_modified_fields();
|
||||
}
|
||||
|
||||
/**
|
||||
* All properties for sync messages.
|
||||
*
|
||||
* @return array and key value pair array of properties.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get_object_properties() {
|
||||
|
||||
// let others decide if they have modified fields in there integration.
|
||||
$this->_properties = apply_filters( 'hubwoo_ecomm_' . $this->_object_type . '_modified_fields', $this->_properties, $this->_object_id );
|
||||
return $this->_properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* All properties for creation
|
||||
*
|
||||
* @return array and key value pair array of properties.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function get_object_properties_for_creation() {
|
||||
|
||||
$modified_fields = HubwooEcommProperties::get_instance()->hubwoo_ecomm_get_properties_for_object( $this->_object_type );
|
||||
|
||||
$properties = array();
|
||||
|
||||
if ( is_array( $modified_fields ) && count( $modified_fields ) ) {
|
||||
|
||||
foreach ( $modified_fields as $single_field ) {
|
||||
|
||||
$property_val = $this->_return_property_value( $single_field );
|
||||
|
||||
if ( ! empty( $property_val ) ) {
|
||||
|
||||
$properties[] = array(
|
||||
'name' => $single_field,
|
||||
'value' => $property_val,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format modified fields of customer.
|
||||
*
|
||||
* Check for all the modified fields till the last update
|
||||
* and prepare them in the hubspot api acceptable form.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function prepare_modified_fields() {
|
||||
|
||||
// need to update all fields, so lets get all the properties that we are working with.
|
||||
$modified_fields = HubwooEcommProperties::get_instance()->hubwoo_ecomm_get_properties_for_object( $this->_object_type );
|
||||
|
||||
if ( is_array( $modified_fields ) && count( $modified_fields ) ) {
|
||||
|
||||
foreach ( $modified_fields as $single_field ) {
|
||||
|
||||
$property = $this->_return_property_value( $single_field );
|
||||
|
||||
if ( ! empty( $property ) ) {
|
||||
|
||||
$this->_properties[ $single_field ] = $property;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepare property in the form of key value accepted by hubspot.
|
||||
*
|
||||
* @param array $property_name array of the property details to validate the value.
|
||||
* @return string property value
|
||||
*/
|
||||
public function _return_property_value( $property_name ) {
|
||||
|
||||
// if property name is not empty.
|
||||
if ( ! empty( $property_name ) ) {
|
||||
// get property value.
|
||||
$property_val = $this->_callback_instance->_get_object_property_value( $property_name, $this->_object_id );
|
||||
return $property_val;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,376 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Manage all object properties.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manage all object properties.
|
||||
*
|
||||
* Provide a list of functions to manage all the information
|
||||
* about contacts properties and lists along with option to
|
||||
* change/update the mapping field on hubspot.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooEcommProperties {
|
||||
|
||||
/**
|
||||
* Hubspot objects
|
||||
*
|
||||
* @var $objects
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private $objects;
|
||||
|
||||
/**
|
||||
* Object Properties.
|
||||
*
|
||||
* @var $properties;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private $properties;
|
||||
|
||||
/**
|
||||
* HubwooEcommProperties Instance.
|
||||
*
|
||||
* @var $_instance
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Main HubwooEcommProperties Instance.
|
||||
*
|
||||
* Ensures only one instance of hubwoo-ecommProperties is loaded or can be loaded.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @static
|
||||
* @return hubwoo-ecommProperties - Main instance.
|
||||
*/
|
||||
public static function get_instance() {
|
||||
|
||||
if ( is_null( self::$_instance ) ) {
|
||||
self::$_instance = new self();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the object prooperties related functionality.
|
||||
*
|
||||
* Set the object groups and properties that we are going to use
|
||||
* for creating/updating the object information for our tacking purpose
|
||||
* and providing other developers to add there field and group for tracking
|
||||
* too by simply using our hooks.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
$this->objects = $this->_set( 'objects' );
|
||||
$this->properties = $this->_set( 'properties' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups/properties.
|
||||
*
|
||||
* @param string $option groups/properties.
|
||||
* @return array Array of groups/properties information.
|
||||
*/
|
||||
public function _get( $option ) {
|
||||
|
||||
if ( 'objects' === $option ) {
|
||||
|
||||
return $this->objects;
|
||||
} elseif ( 'properties' === $option ) {
|
||||
|
||||
return $this->properties;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of required option.
|
||||
*
|
||||
* @param String $option the identifier.
|
||||
* @return Array An array of values.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function _set( $option ) {
|
||||
|
||||
$values = array();
|
||||
|
||||
if ( 'objects' === $option ) {
|
||||
$values = array( 'CONTACT', 'PRODUCT', 'DEAL', 'LINE_ITEM' );
|
||||
} elseif ( 'properties' === $option ) {
|
||||
$values = $this->hubwoo_ecomm_get_object_properties();
|
||||
}
|
||||
|
||||
return apply_filters( 'hubwoo_ecomm_' . $option, $values );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the required properties for an hubspot object for installation
|
||||
*
|
||||
* @return Array object properties
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function hubwoo_ecomm_get_object_properties() {
|
||||
|
||||
$objects = $this->_get( 'objects' );
|
||||
|
||||
$ecomm_object_properties = array();
|
||||
|
||||
if ( is_array( $objects ) && count( $objects ) ) {
|
||||
foreach ( $objects as $single_object ) {
|
||||
$ecomm_object_properties[ $single_object ] = $this->hubwoo_ecomm_set_all_properties( $single_object );
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'hubwoo_ecomm_bridge_object_properties', $ecomm_object_properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the required properties for sync messages to hubspot.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $object type of object.
|
||||
* @return Array object properties
|
||||
*/
|
||||
public function hubwoo_ecomm_get_properties_for_object( $object ) {
|
||||
|
||||
$object_properties = array();
|
||||
|
||||
if ( ! empty( $object ) ) {
|
||||
$properties = $this->hubwoo_ecomm_set_all_properties( $object );
|
||||
if ( is_array( $properties ) && count( $properties ) ) {
|
||||
foreach ( $properties as $single_property ) {
|
||||
if ( ! empty( $single_property['externalPropertyName'] ) ) {
|
||||
$object_properties[] = $single_property['externalPropertyName'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return apply_filters( 'hubwoo_ecomm_bridge_' . $object . '_properties', $object_properties );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prepares all the properties for hubspot objects.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $object_name object type.
|
||||
* @return Array object properties
|
||||
*/
|
||||
public function hubwoo_ecomm_set_all_properties( $object_name ) {
|
||||
|
||||
$object_properties = array();
|
||||
|
||||
if ( ! empty( $object_name ) ) {
|
||||
|
||||
switch ( $object_name ) {
|
||||
|
||||
case 'CONTACT':
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'email',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'email',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'firstname',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'firstname',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'lastname',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'lastname',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'company',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'company',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'phone',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'phone',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'mobilephone',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'mobilephone',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'address',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'address',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'city',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'city',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'state',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'state',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'country',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'country',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'zip',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'zip',
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 'PRODUCT':
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'name',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'name',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'hs_images',
|
||||
'dataType' => 'AVATAR_IMAGE',
|
||||
'hubspotPropertyName' => 'hs_images',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'price',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'price',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'pr_description',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'description',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'store_product_id',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'store_product_id',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'product_source_store',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'product_source_store',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'hs_sku',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'hs_sku',
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 'DEAL':
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'dealstage',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'dealstage',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'dealname',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'dealname',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'closedate',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'closedate',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'createdate',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'createdate',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'amount',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'amount',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'discount_amount',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'discount_amount',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'order_number',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'order_number',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'shipment_ids',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'shipment_ids',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'tax_amount',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'tax_amount',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'description',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'description',
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 'LINE_ITEM':
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'discount_amount',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'discount',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'quantity',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'quantity',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'name',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'name',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'price',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'price',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'sku',
|
||||
'dataType' => 'STRING',
|
||||
'hubspotPropertyName' => 'description',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'amount',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'amount',
|
||||
);
|
||||
$object_properties[] = array(
|
||||
'externalPropertyName' => 'tax_amount',
|
||||
'dataType' => 'NUMBER',
|
||||
'hubspotPropertyName' => 'tax',
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $object_properties;
|
||||
}
|
||||
}
|
||||
@@ -1,458 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* All property callbacks.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manage all property callbacks.
|
||||
*
|
||||
* Provide a list of functions to manage all the information
|
||||
* about contacts properties and there callback functions to
|
||||
* get value of that property.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooEcommPropertyCallbacks {
|
||||
|
||||
/**
|
||||
* Object id.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var int
|
||||
*/
|
||||
protected $_object_id;
|
||||
|
||||
/**
|
||||
* Object type
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var WP_User
|
||||
*/
|
||||
protected $_object;
|
||||
|
||||
|
||||
/**
|
||||
* Properties and there callbacks.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var Associated_array
|
||||
*/
|
||||
protected $_property_callbacks = array(
|
||||
|
||||
'email' => 'hubwoo_get_user_mail',
|
||||
'firstname' => 'hubwoo_get_user_meta',
|
||||
'lastname' => 'hubwoo_get_user_meta',
|
||||
'address' => 'hubwoo_get_user_meta',
|
||||
'company' => 'hubwoo_get_user_meta',
|
||||
'city' => 'hubwoo_get_user_meta',
|
||||
'state' => 'hubwoo_get_user_meta',
|
||||
'zip' => 'hubwoo_get_user_meta',
|
||||
'country' => 'hubwoo_get_user_meta',
|
||||
'phone' => 'hubwoo_get_user_meta',
|
||||
'mobilephone' => 'hubwoo_get_user_meta',
|
||||
|
||||
'name' => 'hubwoo_ecomm_product_info',
|
||||
'hs_images' => 'hubwoo_ecomm_product_info',
|
||||
'price' => 'hubwoo_ecomm_product_info',
|
||||
'pr_description' => 'hubwoo_ecomm_product_info',
|
||||
'product_source_store' => 'hubwoo_ecomm_product_info',
|
||||
'store_product_id' => 'hubwoo_ecomm_product_info',
|
||||
'hs_sku' => 'hubwoo_ecomm_product_info',
|
||||
|
||||
'dealstage' => 'hubwoo_ecomm_deal_info',
|
||||
'dealname' => 'hubwoo_ecomm_deal_info',
|
||||
'closedate' => 'hubwoo_ecomm_deal_info',
|
||||
'amount' => 'hubwoo_ecomm_deal_info',
|
||||
'order_abandoned_cart_url' => 'hubwoo_ecomm_deal_info',
|
||||
'discount_amount' => 'hubwoo_ecomm_deal_info',
|
||||
'order_number' => 'hubwoo_ecomm_deal_info',
|
||||
'shipment_ids' => 'hubwoo_ecomm_deal_info',
|
||||
'tax_amount' => 'hubwoo_ecomm_deal_info',
|
||||
'createdate' => 'hubwoo_ecomm_deal_info',
|
||||
'description' => 'hubwoo_ecomm_deal_info',
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $object_id object id to get property values of.
|
||||
* @param string $object_type object type to get property values of.
|
||||
*/
|
||||
public function __construct( $object_id, $object_type ) {
|
||||
|
||||
$this->_object_id = $object_id;
|
||||
$this->_object = $object_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Property value.
|
||||
*
|
||||
* @param string $property_name name of the object property.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function _get_object_property_value( $property_name ) {
|
||||
|
||||
$value = '';
|
||||
|
||||
if ( ! empty( $property_name ) ) {
|
||||
// get the callback.
|
||||
$callback_function = $this->_get_property_callback( $property_name );
|
||||
|
||||
if ( ! empty( $callback_function ) ) {
|
||||
|
||||
// get the value by calling respective callback.
|
||||
$value = $this->$callback_function( $property_name );
|
||||
}
|
||||
}
|
||||
|
||||
$value = apply_filters( 'hubwoo-ecomm_contact_property_value', $value, $property_name, $this->_object_id, $this->_object );
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the property callback to get value of.
|
||||
*
|
||||
* @param strig $property_name name of the property.
|
||||
* @return string/false callback function name or false.
|
||||
*/
|
||||
private function _get_property_callback( $property_name ) {
|
||||
// check if the property name exists in the array.
|
||||
if ( array_key_exists( $property_name, $this->_property_callbacks ) ) {
|
||||
// if exists then get the callback name.
|
||||
$callback = $this->_property_callbacks[ $property_name ];
|
||||
|
||||
return $callback;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* User email
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function hubwoo_get_user_mail() {
|
||||
// get it from user object.
|
||||
$user = get_user_by( 'id', $this->_object_id );
|
||||
return $user->data->user_email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer meta
|
||||
*
|
||||
* @param string $key meta key.
|
||||
* @return string customer meta detail.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function hubwoo_get_user_meta( $key ) {
|
||||
|
||||
$value = '';
|
||||
|
||||
switch ( $key ) {
|
||||
|
||||
case 'firstname':
|
||||
$value = get_user_meta( $this->_object_id, 'first_name', true );
|
||||
break;
|
||||
|
||||
case 'lastname':
|
||||
$value = get_user_meta( $this->_object_id, 'last_name', true );
|
||||
break;
|
||||
|
||||
case 'company':
|
||||
$value = get_user_meta( $this->_object_id, 'billing_company', true );
|
||||
break;
|
||||
|
||||
case 'city':
|
||||
$value = get_user_meta( $this->_object_id, 'billing_city', true );
|
||||
break;
|
||||
|
||||
case 'state':
|
||||
$value = get_user_meta( $this->_object_id, 'billing_state', true );
|
||||
break;
|
||||
|
||||
case 'country':
|
||||
$value = get_user_meta( $this->_object_id, 'billing_country', true );
|
||||
break;
|
||||
|
||||
case 'address':
|
||||
$address1 = get_user_meta( $this->_object_id, 'billing_address_1', true );
|
||||
$address2 = get_user_meta( $this->_object_id, 'billing_address_2', true );
|
||||
$address = '';
|
||||
if ( ! empty( $address1 ) || ! empty( $address2 ) ) {
|
||||
$address = $address1 . ' ' . $address2;
|
||||
}
|
||||
$value = $address;
|
||||
break;
|
||||
|
||||
case 'zip':
|
||||
$value = get_user_meta( $this->_object_id, 'billing_postcode', true );
|
||||
break;
|
||||
|
||||
case 'mobilephone':
|
||||
case 'phone':
|
||||
$value = get_user_meta( $this->_object_id, 'billing_phone', true );
|
||||
break;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create user date.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return string user registered date
|
||||
*/
|
||||
public function hubwoo_create_date() {
|
||||
|
||||
$create_date = '';
|
||||
$customer = new WP_User( $this->_object_id );
|
||||
$account_creation = isset( $customer->data->user_registered ) ? $customer->data->user_registered : '';
|
||||
if ( ! empty( $account_creation ) ) {
|
||||
$account_creation = strtotime( $account_creation );
|
||||
}
|
||||
if ( ! empty( $account_creation ) ) {
|
||||
$create_date = HubwooObjectProperties::get_instance()->hubwoo_set_utc_midnight( $account_creation );
|
||||
}
|
||||
return $create_date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get contact lifecycle stage
|
||||
*
|
||||
* @return string customer lifecycle stage
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function hubwoo_contact_stage() {
|
||||
|
||||
$stage = '';
|
||||
|
||||
$orders_count = 0;
|
||||
|
||||
//hpos changes
|
||||
$query = new WC_Order_Query(array(
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => array_keys( wc_get_order_statuses() ),
|
||||
'order' => 'desc',
|
||||
'post_parent' => 0,
|
||||
'customer_id' => $this->_object_id,
|
||||
));
|
||||
|
||||
$customer_orders = $query->get_orders();
|
||||
|
||||
if ( is_array( $customer_orders ) && count( $customer_orders ) ) {
|
||||
|
||||
$orders_count = count( $customer_orders );
|
||||
}
|
||||
|
||||
if ( $orders_count > 0 ) {
|
||||
|
||||
$stage = 'customer';
|
||||
} else {
|
||||
|
||||
$stage = 'lead';
|
||||
}
|
||||
|
||||
return $stage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for products objects
|
||||
*
|
||||
* @param string $key meta key.
|
||||
* @return string product properties
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function hubwoo_ecomm_product_info( $key ) {
|
||||
|
||||
$product = wc_get_product( $this->_object_id );
|
||||
|
||||
$value = '';
|
||||
|
||||
if ( ! empty( $product ) && ! is_wp_error( $product ) ) {
|
||||
|
||||
switch ( $key ) {
|
||||
|
||||
case 'name':
|
||||
$value = HubwooObjectProperties::hubwoo_ecomm_product_name( $product );
|
||||
break;
|
||||
|
||||
case 'hs_images':
|
||||
$attachment_src = wp_get_attachment_image_src( get_post_thumbnail_id( $this->_object_id ), 'single-post-thumbnail' );
|
||||
$image_url = isset( $attachment_src[0] ) ? $attachment_src[0] : wc_placeholder_img_src();
|
||||
$value = $image_url;
|
||||
break;
|
||||
|
||||
case 'price':
|
||||
$value = $product->get_price();
|
||||
break;
|
||||
|
||||
case 'pr_description':
|
||||
$value = $product->get_short_description();
|
||||
break;
|
||||
|
||||
case 'store_product_id':
|
||||
$value = $this->_object_id;
|
||||
break;
|
||||
|
||||
case 'product_source_store':
|
||||
$value = get_bloginfo( 'name' );
|
||||
break;
|
||||
|
||||
case 'hs_sku':
|
||||
$value = $product->get_sku();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for deal objects.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $key meta key.
|
||||
* @return string object properties
|
||||
*/
|
||||
public function hubwoo_ecomm_deal_info( $key ) {
|
||||
|
||||
$order = wc_get_order( $this->_object_id );
|
||||
|
||||
$value = '';
|
||||
|
||||
if ( $order instanceof WC_Order ) {
|
||||
$status = $order->get_status();
|
||||
$deal_name = '#' . $order->get_order_number();
|
||||
$order_date = get_post_time( 'U', true, $this->_object_id );
|
||||
|
||||
$create_date = $order_date;
|
||||
|
||||
$user_info['first_name'] = $order->get_billing_first_name();
|
||||
$user_info['last_name'] = $order->get_billing_last_name();
|
||||
|
||||
foreach ( $user_info as $value ) {
|
||||
if ( ! empty( $value ) ) {
|
||||
$deal_name .= ' ' . $value;
|
||||
}
|
||||
}
|
||||
$deal_stage = self::hubwoo_get_valid_deal_stage( 'wc-' . $status );
|
||||
|
||||
if ( ! in_array( $deal_stage, self::hubwoo_ecomm_won_stages() ) ) {
|
||||
|
||||
$order_date = get_post_time( 'U', true, $this->_object_id ) + ( get_option( 'hubwoo_ecomm_closedate_days', 1 ) * 24 * 60 * 60 );
|
||||
}
|
||||
|
||||
if ( ! empty( $order ) && ! is_wp_error( $order ) ) {
|
||||
|
||||
switch ( $key ) {
|
||||
|
||||
case 'dealstage':
|
||||
$value = $deal_stage;
|
||||
break;
|
||||
|
||||
case 'dealname':
|
||||
$value = $deal_name;
|
||||
break;
|
||||
|
||||
case 'closedate':
|
||||
$value = HubwooObjectProperties::get_instance()->hubwoo_set_utc_midnight( $order_date );
|
||||
break;
|
||||
|
||||
case 'createdate':
|
||||
$value = HubwooObjectProperties::get_instance()->hubwoo_set_utc_midnight( $create_date );
|
||||
break;
|
||||
|
||||
case 'amount':
|
||||
$value = $order->get_total();
|
||||
break;
|
||||
|
||||
case 'order_abandoned_cart_url':
|
||||
$value = $order->get_checkout_payment_url();
|
||||
break;
|
||||
|
||||
case 'discount_amount':
|
||||
$value = $order->get_discount_total();
|
||||
break;
|
||||
|
||||
case 'order_number':
|
||||
$value = $order->get_order_number();
|
||||
break;
|
||||
|
||||
case 'shipment_ids':
|
||||
$value = $order->get_shipping_method();
|
||||
break;
|
||||
|
||||
case 'tax_amount':
|
||||
$value = $order->get_total_tax();
|
||||
break;
|
||||
|
||||
case 'description':
|
||||
$value = $order->get_customer_note();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an array in hubspot accepted enumeration value.
|
||||
*
|
||||
* @param array $properties Array of values.
|
||||
* @return string formatted string.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function hubwoo_ecomm_format_array( $properties ) {
|
||||
|
||||
if ( is_array( $properties ) ) {
|
||||
|
||||
$properties = array_unique( $properties );
|
||||
$properties = implode( ',', $properties );
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a valid deal stage for the order status
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $order_key order status key.
|
||||
* @return string hubspot deal stage
|
||||
*/
|
||||
public static function hubwoo_get_valid_deal_stage( $order_key ) {
|
||||
|
||||
$saved_mappings = get_option( 'hubwoo_ecomm_final_mapping', array() );
|
||||
$key = array_search( $order_key, array_column( $saved_mappings, 'status' ) );
|
||||
return false === $key ? 'checkout_completed' : $saved_mappings[ $key ]['deal_stage'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns won deal stages.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array won deal stages
|
||||
*/
|
||||
public static function hubwoo_ecomm_won_stages() {
|
||||
|
||||
$won_stages = get_option( 'hubwoo_ecomm_won_stages', array() );
|
||||
|
||||
if ( empty( $won_stages ) ) {
|
||||
|
||||
$won_stages = array( 'processed', 'shipped' );
|
||||
}
|
||||
|
||||
return $won_stages;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,157 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* The class responsible for Error Handling.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.4
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* The class responsible for Error Handling.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooErrorHandling {
|
||||
|
||||
/**
|
||||
* The single instance of the class.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @var HubwooErrorHandling The single instance of the HubwooErrorHandling
|
||||
*/
|
||||
protected static $instance = null;
|
||||
|
||||
/**
|
||||
* Main HubwooErrorHandling Instance.
|
||||
*
|
||||
* Ensures only one instance of HubwooErrorHandling is loaded or can be loaded.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @static
|
||||
* @return HubwooErrorHandling - Main instance.
|
||||
*/
|
||||
public static function get_instance() {
|
||||
|
||||
if ( is_null( self::$instance ) ) {
|
||||
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handling the error response from HubSpot.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @param array $response response from HubSpot.
|
||||
* @param string $type type of error.
|
||||
* @param array $additional_args any additional args.
|
||||
*/
|
||||
public function hubwoo_handle_response( $response, $type, $additional_args = array() ) {
|
||||
|
||||
switch ( $type ) {
|
||||
case HubwooConst::HUBWOOWORKFLOW:
|
||||
return $this->hubwoo_handle_workflow( $response, $additional_args );
|
||||
case HubwooConst::CHECKOUTFORM:
|
||||
return $this->hubwoo_handle_form( $response, $additional_args );
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handling errors in Forms.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @param array $response response from HubSpot.
|
||||
* @param array $additional_args any additional args.
|
||||
*/
|
||||
public function hubwoo_handle_form( $response, $additional_args ) {
|
||||
|
||||
switch ( $response['status_code'] ) {
|
||||
case 409:
|
||||
$res = HubWooConnectionMananager::get_instance()->hubwoo_get_all_forms();
|
||||
if ( 200 == $res['status_code'] ) {
|
||||
$res = json_decode( $res['body'], true );
|
||||
if ( ! empty( $res ) ) {
|
||||
foreach ( $res as $form_value ) {
|
||||
if ( HubwooConst::CHECKOUTFORMNAME == $form_value['name'] ) {
|
||||
update_option( 'hubwoo_checkout_form_created', 'yes' );
|
||||
update_option( 'hubwoo_checkout_form_id', $form_value['guid'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handling errors in Workflows.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @param array $response response from HubSpot.
|
||||
* @param array $additional_args any additional args.
|
||||
*/
|
||||
public function hubwoo_handle_workflow( $response, $additional_args ) {
|
||||
|
||||
if ( array_key_exists( 'current_workflow', $additional_args ) && ! empty( $additional_args['current_workflow'] ) ) {
|
||||
|
||||
$properties = array();
|
||||
|
||||
$current_workflow = $additional_args['current_workflow'];
|
||||
|
||||
switch ( $response['status_code'] ) {
|
||||
case 500:
|
||||
foreach ( $current_workflow['actions'] as $action ) {
|
||||
if ( array_key_exists( 'propertyName', $action ) ) {
|
||||
$properties[] = $action['propertyName'];
|
||||
}
|
||||
}
|
||||
if ( ! empty( $properties ) ) {
|
||||
$all_properties = HubWooContactProperties::get_instance()->_get( 'properties', '', true );
|
||||
foreach ( $properties as $property ) {
|
||||
|
||||
$response = HubWooConnectionMananager::get_instance()->hubwoo_read_object_property( 'contact', $property );
|
||||
if ( 404 == $response['status_code'] ) {
|
||||
$this->hubwoo_prepare_single_property( $all_properties, $property );
|
||||
}
|
||||
}
|
||||
return HubWooConnectionMananager::get_instance()->create_workflow( $current_workflow );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and create property in HubSpot.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @param array $all_properties all properties.
|
||||
* @param string $property current property to be checked.
|
||||
*/
|
||||
public function hubwoo_prepare_single_property( $all_properties, $property ) {
|
||||
$property_details = array();
|
||||
foreach ( $all_properties as $group_name => $group_properties ) {
|
||||
foreach ( $group_properties as $group_property ) {
|
||||
if ( $property == $group_property['name'] ) {
|
||||
$property_details = $group_property;
|
||||
$property_details['groupName'] = $group_name;
|
||||
HubWooConnectionMananager::get_instance()->create_property( $property_details, 'contacts' );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,837 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* All property callbacks for guest orders.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manage all property callbacks for guest orders.
|
||||
*
|
||||
* Provide a list of functions to manage all the information
|
||||
* about guest contacts properties and there callback functions to
|
||||
* get value of that property.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooGuestOrdersManager {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param int $order_id contact id to get property values of.
|
||||
*/
|
||||
public function __construct( $order_id ) {
|
||||
|
||||
$this->_order_id = $order_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer shopping cart details from order meta.
|
||||
*
|
||||
* @return string order meta detail.
|
||||
* @param int $order_id id of the order.
|
||||
* @param string $property_name name of the property.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function get_order_meta( $order_id, $property_name ) {
|
||||
//hpos changes
|
||||
$order = wc_get_order( $order_id );
|
||||
|
||||
return $order->get_meta($property_name, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Guest customer details.
|
||||
*
|
||||
* @param int $order_id id of the order.
|
||||
* @param string $email email of the user.
|
||||
* @return array full of HubSpot contact properties and values
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function get_order_related_properties( $order_id, $email ) {
|
||||
|
||||
$guest_user_properties = array();
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
$billing_country = $order->get_billing_country();
|
||||
$billing_state = $order->get_billing_state();
|
||||
$shipping_state = $order->get_shipping_state();
|
||||
$shipping_country = $order->get_shipping_country();
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'shipping_address_line_1',
|
||||
'value' => $order->get_shipping_address_1(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'shipping_address_line_2',
|
||||
'value' => $order->get_shipping_address_2(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'shipping_city',
|
||||
'value' => $order->get_shipping_city(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'shipping_country',
|
||||
'value' => Hubwoo::map_country_by_abbr( $shipping_country ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'shipping_state',
|
||||
'value' => Hubwoo::map_state_by_abbr( $shipping_state, $shipping_country ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'shipping_postal_code',
|
||||
'value' => $order->get_shipping_postcode(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'billing_address_line_1',
|
||||
'value' => $order->get_billing_address_1(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'billing_address_line_2',
|
||||
'value' => $order->get_billing_address_2(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'billing_city',
|
||||
'value' => $order->get_billing_city(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'billing_country',
|
||||
'value' => Hubwoo::map_country_by_abbr( $billing_country ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'billing_state',
|
||||
'value' => Hubwoo::map_state_by_abbr( $billing_state, $billing_country ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'billing_postal_code',
|
||||
'value' => $order->get_billing_postcode(),
|
||||
);
|
||||
|
||||
$categories_bought = array();
|
||||
$skus_bought = array();
|
||||
$product_types_bought = array();
|
||||
$last_3_products_ids = array();
|
||||
$last_products_bought = array();
|
||||
$last_order_date = 0;
|
||||
$last_products_bought_html = array();
|
||||
$days_differences = array();
|
||||
$last_type_bought = '';
|
||||
$first_date = 0;
|
||||
$last_date = 0;
|
||||
$average_days = array();
|
||||
$products_bought = array();
|
||||
$last_product_bought = '';
|
||||
$total_value_of_orders = 0;
|
||||
$total_number_of_orders = 0;
|
||||
$total_number_of_current_orders = 0;
|
||||
$last_order_for_html = 0;
|
||||
$last_order_id = 0;
|
||||
|
||||
$order_statuses = get_option( 'hubwoo-selected-order-status', array() );
|
||||
|
||||
if ( empty( $order_statuses ) ) {
|
||||
|
||||
$order_statuses = array_keys( wc_get_order_statuses() );
|
||||
}
|
||||
|
||||
//hpos changes
|
||||
if( Hubwoo::hubwoo_check_hpos_active() ) {
|
||||
$query = new WC_Order_Query(array(
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => $order_statuses,
|
||||
'orderby' => 'date',
|
||||
'order' => 'desc',
|
||||
'return' => 'ids',
|
||||
'no_found_rows' => true,
|
||||
'ignore_sticky_posts' => true,
|
||||
'customer' => $email,
|
||||
));
|
||||
|
||||
$customer_orders = $query->get_orders();
|
||||
|
||||
} else {
|
||||
$query = new WP_Query();
|
||||
|
||||
$customer_orders = $query->query(
|
||||
array(
|
||||
'post_type' => 'shop_order',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => $order_statuses,
|
||||
'orderby' => 'date',
|
||||
'order' => 'desc',
|
||||
'fields' => 'ids',
|
||||
'no_found_rows' => true,
|
||||
'ignore_sticky_posts' => true,
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'key' => '_billing_email',
|
||||
'value' => $email,
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'customer_group',
|
||||
'value' => 'guest',
|
||||
);
|
||||
|
||||
$last_order = ! empty( $customer_orders ) && is_array( $customer_orders ) ? $customer_orders[0] : '';
|
||||
$last_order_object = wc_get_order($last_order);
|
||||
$order_tracking_number = $last_order_object->get_meta('_wc_shipment_tracking_items', true);
|
||||
if ( ! empty( $order_tracking_number ) ) {
|
||||
|
||||
$shipment_data = $order_tracking_number[0];
|
||||
|
||||
if ( ! empty( $shipment_data['date_shipped'] ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_shipment_date',
|
||||
'value' => self::hubwoo_set_utc_midnight( $shipment_data['date_shipped'] ),
|
||||
);
|
||||
}
|
||||
if ( ! empty( $shipment_data['tracking_number'] ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_tracking_number',
|
||||
'value' => $shipment_data['tracking_number'],
|
||||
);
|
||||
}
|
||||
if ( ! empty( $shipment_data['custom_tracking_link'] ) ) {
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_tracking_url',
|
||||
'value' => $shipment_data['custom_tracking_link'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$optin = $order->get_meta('hubwoo_checkout_marketing_optin', true);
|
||||
$optin_sources = array();
|
||||
if ( ! empty( $optin ) && 'yes' == $optin ) {
|
||||
$optin_sources[] = 'checkout';
|
||||
} else {
|
||||
$optin = 'no';
|
||||
}
|
||||
|
||||
$property_updated = get_option( 'hubwoo_newsletter_property_update', 'no' );
|
||||
|
||||
if ( ! empty( $property_updated ) && 'yes' == $property_updated ) {
|
||||
if ( 'yes' == $optin ) {
|
||||
$optin = true;
|
||||
} else {
|
||||
$optin = false;
|
||||
}
|
||||
}
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'newsletter_subscription',
|
||||
'value' => $optin,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'marketing_newsletter',
|
||||
'value' => self::hubwoo_format_array( $optin_sources ),
|
||||
);
|
||||
|
||||
// if customer have orders.
|
||||
if ( is_array( $customer_orders ) && count( $customer_orders ) ) {
|
||||
|
||||
// total number of customer orders.
|
||||
$total_number_of_orders = count( $customer_orders );
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_number_of_orders',
|
||||
'value' => count( $customer_orders ),
|
||||
);
|
||||
|
||||
$order_frequency = count( $customer_orders );
|
||||
|
||||
$counter = 0;
|
||||
|
||||
$products_count = 0;
|
||||
|
||||
foreach ( $customer_orders as $order_id ) {
|
||||
|
||||
// if order id not found let's check for another order.
|
||||
if ( ! $order_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// order date.
|
||||
$order_date = get_post_time( 'U', false, $order_id );
|
||||
|
||||
$last_date = $order_date;
|
||||
|
||||
if ( 0 == $first_date ) {
|
||||
$first_date = $last_date;
|
||||
}
|
||||
|
||||
$average_days[] = self::hubwoo_get_average_days( $first_date, $last_date );
|
||||
|
||||
$first_date = $last_date;
|
||||
|
||||
// get order.
|
||||
$order = new WC_Order( $order_id );
|
||||
|
||||
// check for WP_Error object.
|
||||
if ( empty( $order ) || is_wp_error( $order ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$order_status = $order->get_status();
|
||||
|
||||
if ( 'failed' !== $order_status && 'cancelled' !== $order_status ) {
|
||||
|
||||
$last_order_for_html ++;
|
||||
|
||||
$order_items = $order->get_items();
|
||||
|
||||
// check if order has items.
|
||||
if ( is_array( $order_items ) && count( $order_items ) ) {
|
||||
|
||||
// let's loop each order item to get the details.
|
||||
foreach ( $order_items as $item_id_1 => $wc_order_item_product ) {
|
||||
|
||||
if ( ! empty( $wc_order_item_product ) && $wc_order_item_product instanceof WC_Order_Item ) {
|
||||
|
||||
$item_id = $wc_order_item_product->get_product_id();
|
||||
$parent_item_sku = get_post_meta( $item_id, '_sku', true );
|
||||
$item_var_id = $wc_order_item_product->get_variation_id();
|
||||
|
||||
if ( get_post_status( $item_id ) == 'trash' || get_post_status( $item_id ) == false ) {
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$product_cats_ids = wc_get_product_term_ids( $item_id, 'product_cat' );
|
||||
|
||||
if ( is_array( $product_cats_ids ) && count( $product_cats_ids ) ) {
|
||||
|
||||
foreach ( $product_cats_ids as $cat_id ) {
|
||||
|
||||
$term = get_term_by( 'id', $cat_id, 'product_cat' );
|
||||
$categories_bought[] = $term->slug;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $item_var_id ) {
|
||||
|
||||
$item_id = $item_var_id;
|
||||
}
|
||||
|
||||
$products_count += $wc_order_item_product->get_quantity();
|
||||
|
||||
$item_sku = get_post_meta( $item_id, '_sku', true );
|
||||
|
||||
if ( empty( $item_sku ) || '' == $item_sku ) {
|
||||
|
||||
$item_sku = $parent_item_sku;
|
||||
}
|
||||
|
||||
if ( empty( $item_sku ) || '' == $item_sku ) {
|
||||
|
||||
$item_sku = $item_id;
|
||||
}
|
||||
|
||||
$skus_bought[] = $item_sku;
|
||||
|
||||
$post = get_post( $item_id );
|
||||
|
||||
$product_uni_name = isset( $post->post_name ) ? $post->post_name : '';
|
||||
|
||||
if ( ! empty( $product_uni_name ) ) {
|
||||
|
||||
$products_bought[] = $product_uni_name . '-' . $item_id;
|
||||
$last_product_bought = $product_uni_name . '-' . $item_id;
|
||||
$last_products_bought[] = $product_uni_name . '-' . $item_id;
|
||||
}
|
||||
|
||||
$product = $wc_order_item_product->get_product();
|
||||
|
||||
if ( $product instanceof WC_Product ) {
|
||||
|
||||
$product_type = $product->get_type();
|
||||
} else {
|
||||
|
||||
$product_type = '';
|
||||
}
|
||||
|
||||
if ( 'variation' == $product_type ) {
|
||||
$product_type = 'variable';
|
||||
}
|
||||
|
||||
if ( 'subscription_variation' == $product_type ) {
|
||||
$product_type = 'variable-subscription';
|
||||
}
|
||||
|
||||
if ( ! empty( $product_type ) ) {
|
||||
|
||||
$product_types_bought[] = $product_type;
|
||||
}
|
||||
|
||||
if ( count( $last_3_products_ids ) < 3 ) {
|
||||
|
||||
$last_3_products_ids[] = $item_id;
|
||||
}
|
||||
|
||||
if ( 1 == $last_order_for_html ) {
|
||||
|
||||
$last_order_id = $order_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$order_total = $order->get_total();
|
||||
|
||||
$total_value_of_orders += floatval( $order_total );
|
||||
|
||||
if ( 'failed' !== $order_status && 'cancelled' !== $order_status && 'refunded' !== $order_status && 'completed' !== $order_status ) {
|
||||
|
||||
$total_number_of_current_orders ++;
|
||||
}
|
||||
|
||||
// check for last order and finish all last order calculations.
|
||||
if ( ! $counter ) {
|
||||
|
||||
// last order date.
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_date',
|
||||
'value' => self::hubwoo_set_utc_midnight( get_post_time( 'U', false, $order_id ) ),
|
||||
);
|
||||
|
||||
$last_order_date = get_post_time( 'U', false, $order_id );
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_value',
|
||||
'value' => $order_total,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_order_number',
|
||||
'value' => $order_id,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_fulfillment_status',
|
||||
'value' => 'wc-' . $order->get_status(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_status',
|
||||
'value' => 'wc-' . $order->get_status(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_order_currency',
|
||||
'value' => $order->get_currency(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_product_bought',
|
||||
'value' => self::hubwoo_format_array( $last_product_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_products_bought',
|
||||
'value' => self::hubwoo_format_array( $last_products_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_skus_bought',
|
||||
'value' => self::hubwoo_format_array( $skus_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_categories_bought',
|
||||
'value' => self::hubwoo_format_array( $categories_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_product_types_bought',
|
||||
'value' => self::hubwoo_format_array( $product_types_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_total_number_of_products_bought',
|
||||
'value' => $products_count,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_products_bought_html',
|
||||
'value' => HubWooContactProperties::get_instance()->hubwoo_last_order_html( $last_order_id ),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// check for first order.
|
||||
if ( ( count( $customer_orders ) - 1 ) == $counter ) {
|
||||
|
||||
// first order based calculation here..
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'first_order_date',
|
||||
'value' => self::hubwoo_set_utc_midnight( get_post_time( 'U', false, $order_id ) ),
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'first_order_value',
|
||||
'value' => $order_total,
|
||||
);
|
||||
}
|
||||
|
||||
$counter++;
|
||||
}
|
||||
|
||||
// rest calculations here.
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'average_order_value',
|
||||
'value' => floatval( $total_value_of_orders / $total_number_of_orders ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_number_of_current_orders',
|
||||
'value' => $total_number_of_current_orders,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_value_of_orders',
|
||||
'value' => $total_value_of_orders,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'average_days_between_orders',
|
||||
'value' => floatval( array_sum( $average_days ) / $total_number_of_orders ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'skus_bought',
|
||||
'value' => self::hubwoo_format_array( $skus_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'categories_bought',
|
||||
'value' => self::hubwoo_format_array( $categories_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'product_types_bought',
|
||||
'value' => self::hubwoo_format_array( $product_types_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'products_bought',
|
||||
'value' => self::hubwoo_format_array( $products_bought ),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_number_of_products_bought',
|
||||
'value' => $products_count,
|
||||
);
|
||||
|
||||
if ( is_array( $last_3_products_ids ) && count( $last_3_products_ids ) ) {
|
||||
|
||||
$counter = 1;
|
||||
|
||||
foreach ( $last_3_products_ids as $last_3_product_id ) {
|
||||
|
||||
$_product = wc_get_product( $last_3_product_id );
|
||||
|
||||
if ( $_product instanceof WC_Product ) {
|
||||
|
||||
$image_url = '';
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_products_bought_product_' . $counter . '_name',
|
||||
'value' => $_product->get_title(),
|
||||
);
|
||||
|
||||
$attachment_src = wp_get_attachment_image_src( get_post_thumbnail_id( $last_3_product_id ), 'single-post-thumbnail' );
|
||||
|
||||
if ( empty( $attachment_src[0] ) ) {
|
||||
|
||||
$parent_id = $_product->get_parent_id();
|
||||
$attachment_src = wp_get_attachment_image_src( get_post_thumbnail_id( $parent_id ), 'single-post-thumbnail' );
|
||||
}
|
||||
|
||||
if ( ! empty( $attachment_src[0] ) ) {
|
||||
$image_url = $attachment_src[0];
|
||||
}
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_products_bought_product_' . $counter . '_image_url',
|
||||
'value' => $image_url,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_products_bought_product_' . $counter . '_price',
|
||||
'value' => $_product->get_price(),
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_products_bought_product_' . $counter . '_url',
|
||||
'value' => get_permalink( $last_3_product_id ),
|
||||
);
|
||||
|
||||
$counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$hubwoo_rfm_at_5 = get_option(
|
||||
'hubwoo_rfm_5',
|
||||
array(
|
||||
0 => 30,
|
||||
1 => 20,
|
||||
2 => 1000,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_from_rfm_4 = get_option(
|
||||
'hubwoo_from_rfm_4',
|
||||
array(
|
||||
0 => 31,
|
||||
1 => 10,
|
||||
2 => 750,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_to_rfm_4 = get_option(
|
||||
'hubwoo_to_rfm_4',
|
||||
array(
|
||||
0 => 90,
|
||||
1 => 20,
|
||||
2 => 1000,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_from_rfm_3 = get_option(
|
||||
'hubwoo_from_rfm_3',
|
||||
array(
|
||||
0 => 91,
|
||||
1 => 5,
|
||||
2 => 500,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_to_rfm_3 = get_option(
|
||||
'hubwoo_to_rfm_3',
|
||||
array(
|
||||
0 => 180,
|
||||
1 => 10,
|
||||
2 => 750,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_from_rfm_2 = get_option(
|
||||
'hubwoo_from_rfm_2',
|
||||
array(
|
||||
0 => 181,
|
||||
1 => 2,
|
||||
2 => 250,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_to_rfm_2 = get_option(
|
||||
'hubwoo_to_rfm_2',
|
||||
array(
|
||||
0 => 365,
|
||||
1 => 5,
|
||||
2 => 500,
|
||||
)
|
||||
);
|
||||
|
||||
$hubwoo_rfm_at_1 = get_option(
|
||||
'hubwoo_rfm_1',
|
||||
array(
|
||||
0 => 365,
|
||||
1 => 2,
|
||||
2 => 250,
|
||||
)
|
||||
);
|
||||
|
||||
$order_monetary = $total_value_of_orders;
|
||||
|
||||
$current_date = gmdate( 'Y-m-d H:i:s', time() );
|
||||
$current_date = new DateTime( $current_date );
|
||||
$last_order_date = gmdate( 'Y-m-d H:i:s', $last_order_date );
|
||||
$last_order_date = new DateTime( $last_order_date );
|
||||
$order_recency = date_diff( $current_date, $last_order_date, true );
|
||||
|
||||
$order_recency = $order_recency->days;
|
||||
$monetary_rating = 1;
|
||||
$order_recency_rating = 1;
|
||||
$order_frequency_rating = 1;
|
||||
|
||||
if ( $order_recency <= $hubwoo_rfm_at_5[0] ) {
|
||||
$order_recency_rating = 5;
|
||||
} elseif ( $order_recency >= $hubwoo_from_rfm_4[0] && $order_recency <= $hubwoo_to_rfm_4[0] ) {
|
||||
$order_recency_rating = 4;
|
||||
} elseif ( $order_recency >= $hubwoo_from_rfm_3[0] && $order_recency <= $hubwoo_to_rfm_3[0] ) {
|
||||
$order_recency_rating = 3;
|
||||
} elseif ( $order_recency >= $hubwoo_from_rfm_2[0] && $order_recency <= $hubwoo_to_rfm_2[0] ) {
|
||||
$order_recency_rating = 2;
|
||||
} else {
|
||||
$order_recency_rating = 1;
|
||||
}
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'order_recency_rating',
|
||||
'value' => $order_recency_rating,
|
||||
);
|
||||
|
||||
if ( $order_frequency > $hubwoo_rfm_at_5[1] ) {
|
||||
$order_frequency_rating = 5;
|
||||
} elseif ( $order_frequency >= $hubwoo_from_rfm_4[1] && $order_frequency < $hubwoo_to_rfm_4[1] ) {
|
||||
$order_frequency_rating = 4;
|
||||
} elseif ( $order_frequency >= $hubwoo_from_rfm_3[1] && $order_frequency < $hubwoo_to_rfm_3[1] ) {
|
||||
$order_frequency_rating = 3;
|
||||
} elseif ( $order_frequency >= $hubwoo_from_rfm_2[1] && $order_frequency < $hubwoo_to_rfm_2[1] ) {
|
||||
$order_frequency_rating = 2;
|
||||
} else {
|
||||
$order_frequency_rating = 1;
|
||||
}
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'order_frequency_rating',
|
||||
'value' => $order_frequency_rating,
|
||||
);
|
||||
|
||||
if ( $order_monetary > $hubwoo_rfm_at_5[2] ) {
|
||||
$monetary_rating = 5;
|
||||
} elseif ( $order_monetary >= $hubwoo_from_rfm_4[2] && $order_monetary < $hubwoo_to_rfm_4[2] ) {
|
||||
$monetary_rating = 4;
|
||||
} elseif ( $order_monetary >= $hubwoo_from_rfm_3[2] && $order_monetary < $hubwoo_to_rfm_3[2] ) {
|
||||
$monetary_rating = 3;
|
||||
} elseif ( $order_monetary >= $hubwoo_from_rfm_2[2] && $order_monetary < $hubwoo_to_rfm_2[2] ) {
|
||||
$monetary_rating = 2;
|
||||
} else {
|
||||
$monetary_rating = 1;
|
||||
}
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'monetary_rating',
|
||||
'value' => $monetary_rating,
|
||||
);
|
||||
} else {
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_number_of_orders',
|
||||
'value' => $total_number_of_orders,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_number_of_products_bought',
|
||||
'value' => 0,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_number_of_current_orders',
|
||||
'value' => $total_number_of_current_orders,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'total_value_of_orders',
|
||||
'value' => $total_value_of_orders,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'last_total_number_of_products_bought',
|
||||
'value' => 0,
|
||||
);
|
||||
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'monetary_rating',
|
||||
'value' => 1,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'order_frequency_rating',
|
||||
'value' => 1,
|
||||
);
|
||||
$guest_user_properties[] = array(
|
||||
'property' => 'order_recency_rating',
|
||||
'value' => 1,
|
||||
);
|
||||
}
|
||||
|
||||
return $guest_user_properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an array in hubspot accepted enumeration value.
|
||||
*
|
||||
* @param array $properties Array of values.
|
||||
* @return string formatted string.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function hubwoo_format_array( $properties ) {
|
||||
|
||||
if ( is_array( $properties ) ) {
|
||||
|
||||
$properties = array_unique( $properties );
|
||||
|
||||
$properties = implode( ';', $properties );
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert unix timestamp to hubwoo formatted midnight time.
|
||||
*
|
||||
* @param int $unix_timestamp timestamp for date.
|
||||
* @param bool $for_deals true/false.
|
||||
* @return Unix midnight timestamp.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function hubwoo_set_utc_midnight( $unix_timestamp, $for_deals = false ) {
|
||||
$string = gmdate( 'Y-m-d H:i:s', $unix_timestamp );
|
||||
$date = new DateTime( $string );
|
||||
|
||||
if ( $for_deals ) {
|
||||
|
||||
$wptimezone = get_option( 'timezone_string', '' );
|
||||
if ( empty( $wptimezone ) ) {
|
||||
$wptimezone = 'UTC';
|
||||
}
|
||||
$timezone = new DateTimeZone( $wptimezone );
|
||||
//phpcs:disable
|
||||
$date->setTimezone( $timezone );
|
||||
//phpcs:enable
|
||||
} else {
|
||||
$wptimezone = 'UTC';
|
||||
$timezone = new DateTimeZone( $wptimezone );
|
||||
$date->setTimezone( $timezone );
|
||||
$date->modify( 'midnight' );
|
||||
}
|
||||
return $date->getTimestamp() * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return days between two dates.
|
||||
*
|
||||
* @param int $first_date first order date.
|
||||
* @param int $last_date last order date.
|
||||
* @return int $datediff date differences.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function hubwoo_get_average_days( $first_date, $last_date ) {
|
||||
|
||||
$now = $first_date;
|
||||
$your_date = $last_date;
|
||||
$datediff = $now - $your_date;
|
||||
return floor( $datediff / ( 60 * 60 * 24 ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,572 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* All api GET/POST functionalities.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handles all hubspot api reqests/response related functionalities of the plugin.
|
||||
*
|
||||
* Provide a list of functions to manage all the requests
|
||||
* that needs in our integration to get/fetch data
|
||||
* from/to hubspot.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes
|
||||
*/
|
||||
class HubwooObjectProperties {
|
||||
|
||||
/**
|
||||
* The single instance of the class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var HubwooObjectProperties The single instance of the HubwooObjectProperties
|
||||
*/
|
||||
protected static $instance = null;
|
||||
/**
|
||||
* Main HubwooObjectProperties Instance.
|
||||
*
|
||||
* Ensures only one instance of HubwooObjectProperties is loaded or can be loaded.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @static
|
||||
* @return HubwooObjectProperties - Main instance.
|
||||
*/
|
||||
public static function get_instance() {
|
||||
|
||||
if ( is_null( self::$instance ) ) {
|
||||
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
/**
|
||||
* Create/update contact and associate with a deal.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $user_id - User Id of the contact.
|
||||
* @static
|
||||
* @return void.
|
||||
*/
|
||||
public static function hubwoo_ecomm_contacts_with_id( $user_id ) {
|
||||
|
||||
$object_type = 'CONTACT';
|
||||
$contact = array();
|
||||
$user_info = json_decode( json_encode( get_userdata( $user_id ) ), true );
|
||||
$user_email = $user_info['data']['user_email'];
|
||||
$hubwoo_ecomm_customer = new HubwooEcommObject( $user_id, $object_type );
|
||||
$contact_properties = $hubwoo_ecomm_customer->get_object_properties();
|
||||
$contact_properties = apply_filters( 'hubwoo_map_ecomm_' . $object_type . '_properties', $contact_properties, $user_id );
|
||||
$user_vid = get_user_meta( $user_id, 'hubwoo_user_vid', true );
|
||||
|
||||
$contact = $contact_properties;
|
||||
$contact['email'] = $user_email;
|
||||
$contact = array(
|
||||
'properties' => $contact,
|
||||
);
|
||||
|
||||
if ( count( $contact ) ) {
|
||||
|
||||
$flag = true;
|
||||
|
||||
if ( Hubwoo::is_access_token_expired() ) {
|
||||
|
||||
$hapikey = HUBWOO_CLIENT_ID;
|
||||
$hseckey = HUBWOO_SECRET_ID;
|
||||
$status = HubWooConnectionMananager::get_instance()->hubwoo_refresh_token( $hapikey, $hseckey );
|
||||
|
||||
if ( ! $status ) {
|
||||
|
||||
$flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $flag ) {
|
||||
|
||||
if ( ! empty( $user_vid ) ) {
|
||||
$response = HubWooConnectionMananager::get_instance()->update_object_record( 'contacts', $user_vid, $contact );
|
||||
} else {
|
||||
$response = HubWooConnectionMananager::get_instance()->create_object_record( 'contacts', $contact );
|
||||
|
||||
if ( 201 == $response['status_code'] ) {
|
||||
$contact_vid = json_decode( $response['body'] );
|
||||
update_user_meta( $user_id, 'hubwoo_user_vid', $contact_vid->id );
|
||||
update_user_meta( $user_id, 'hubwoo_pro_user_data_change', 'synced' );
|
||||
|
||||
} else if ( 409 == $response['status_code'] ) {
|
||||
$contact_vid = json_decode( $response['body'] );
|
||||
$hs_id = explode( 'ID: ', $contact_vid->message );
|
||||
update_user_meta( $user_id, 'hubwoo_user_vid', $hs_id[1] );
|
||||
update_user_meta( $user_id, 'hubwoo_pro_user_data_change', 'synced' );
|
||||
} else if ( 400 == $response['status_code'] ) {
|
||||
update_user_meta( $user_id, 'hubwoo_invalid_contact', 'yes' );
|
||||
update_user_meta( $user_id, 'hubwoo_pro_user_data_change', 'synced' );
|
||||
}
|
||||
}
|
||||
|
||||
do_action( 'hubwoo_ecomm_contact_synced', $user_email );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update a guest user and associate with a deal.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $order_id - order id of the contact.
|
||||
* @static
|
||||
* @return void.
|
||||
*/
|
||||
public static function hubwoo_ecomm_guest_user( $order_id ) {
|
||||
|
||||
global $hubwoo;
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
$guest_email = $order->get_billing_email();
|
||||
|
||||
$contact = array();
|
||||
|
||||
if ( ! empty( $guest_email ) ) {
|
||||
|
||||
$object_type = 'CONTACT';
|
||||
$guest_user_info = array();
|
||||
$guest_order_callback = new HubwooGuestOrdersManager( $order_id );
|
||||
$guest_user_properties = $guest_order_callback->get_order_related_properties( $order_id, $guest_email );
|
||||
$guest_user_properties = $hubwoo->hubwoo_filter_contact_properties( $guest_user_properties );
|
||||
|
||||
foreach ( $guest_user_properties as $key => $value ) {
|
||||
$guest_user_info[ $value['property'] ] = $value['value'];
|
||||
}
|
||||
|
||||
$guest_user_info['email'] = $guest_email;
|
||||
$guest_user_info['firstname'] = $order->get_billing_first_name();
|
||||
$guest_user_info['lastname'] = $order->get_billing_last_name();
|
||||
$guest_user_info['phone'] = $order->get_billing_phone();
|
||||
$guest_user_info['billing_address_line_1'] = $order->get_billing_address_1();
|
||||
$guest_user_info['billing_address_line_2'] = $order->get_billing_address_2();
|
||||
$guest_user_info['billing_city'] = $order->get_billing_city();
|
||||
$guest_user_info['billing_state'] = $order->get_billing_state();
|
||||
$guest_user_info['billing_country'] = $order->get_billing_country();
|
||||
$guest_user_info['billing_postal_code'] = $order->get_billing_postcode();
|
||||
$guest_user_info['lifecyclestage'] = 'customer';
|
||||
$guest_user_info['customer_source_store'] = get_bloginfo( 'name' );
|
||||
$guest_user_info['hs_language'] = $order->get_meta('hubwoo_preferred_language', true);
|
||||
$guest_contact_properties = apply_filters( 'hubwoo_map_ecomm_guest_' . $object_type . '_properties', $guest_user_info, $order_id );
|
||||
$user_vid = $order->get_meta('hubwoo_user_vid', true);
|
||||
$contact = array(
|
||||
'properties' => $guest_contact_properties,
|
||||
);
|
||||
}
|
||||
if ( count( $contact ) ) {
|
||||
|
||||
$flag = true;
|
||||
|
||||
if ( Hubwoo::is_access_token_expired() ) {
|
||||
|
||||
$hapikey = HUBWOO_CLIENT_ID;
|
||||
$hseckey = HUBWOO_SECRET_ID;
|
||||
$status = HubWooConnectionMananager::get_instance()->hubwoo_refresh_token( $hapikey, $hseckey );
|
||||
|
||||
if ( ! $status ) {
|
||||
|
||||
$flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $flag ) {
|
||||
|
||||
if ( ! empty( $user_vid ) ) {
|
||||
$response = HubWooConnectionMananager::get_instance()->update_object_record( 'contacts', $user_vid, $contact );
|
||||
} else {
|
||||
$response = HubWooConnectionMananager::get_instance()->create_object_record( 'contacts', $contact );
|
||||
|
||||
if ( 201 == $response['status_code'] ) {
|
||||
$contact_vid = json_decode( $response['body'] );
|
||||
$order->update_meta_data('hubwoo_user_vid', $contact_vid->id);
|
||||
$order->update_meta_data('hubwoo_pro_guest_order', 'synced');
|
||||
$order->save();
|
||||
|
||||
} else if ( 409 == $response['status_code'] ) {
|
||||
$contact_vid = json_decode( $response['body'] );
|
||||
$hs_id = explode( 'ID: ', $contact_vid->message );
|
||||
$order->update_meta_data('hubwoo_user_vid', $hs_id[1]);
|
||||
$order->update_meta_data('hubwoo_pro_guest_order', 'synced');
|
||||
$order->save();
|
||||
} else if ( 400 == $response['status_code'] ) {
|
||||
$order->update_meta_data('hubwoo_invalid_contact', 'yes');
|
||||
$order->update_meta_data('hubwoo_pro_guest_order', 'synced');
|
||||
$order->update_meta_data('hubwoo_pro_user_data_change', 'synced');
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
|
||||
do_action( 'hubwoo_ecomm_contact_synced', $guest_email );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update an ecommerce deal.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $order_id - order id.
|
||||
* @param int $source - register or guest.
|
||||
* @param int $customer_id - user id.
|
||||
* @static
|
||||
* @return array sync response from HubSpot.
|
||||
*/
|
||||
public static function hubwoo_ecomm_sync_deal( $order_id, $source, $customer_id ) {
|
||||
$object_type = 'DEAL';
|
||||
$deal_updates = array();
|
||||
$assc_deal_cmpy = get_option( 'hubwoo_assoc_deal_cmpy_enable', 'yes' );
|
||||
$pipeline_id = get_option( 'hubwoo_ecomm_pipeline_id', false );
|
||||
$hubwoo_ecomm_deal = new HubwooEcommObject( $order_id, $object_type );
|
||||
$deal_properties = $hubwoo_ecomm_deal->get_object_properties();
|
||||
$deal_properties = apply_filters( 'hubwoo_map_ecomm_' . $object_type . '_properties', $deal_properties, $order_id );
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
if ( 'yes' == get_option( 'hubwoo_deal_multi_currency_enable', 'no' ) ) {
|
||||
$currency = $order->get_currency();
|
||||
if ( ! empty( $currency ) ) {
|
||||
$deal_properties['deal_currency_code'] = $currency;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $pipeline_id ) ) {
|
||||
Hubwoo::get_all_deal_stages();
|
||||
$pipeline_id = get_option( 'hubwoo_ecomm_pipeline_id', false );
|
||||
}
|
||||
|
||||
$deal_properties['pipeline'] = $pipeline_id;
|
||||
|
||||
$deal_updates = array(
|
||||
'properties' => $deal_properties,
|
||||
);
|
||||
$response = '';
|
||||
|
||||
if ( 'user' == $source ) {
|
||||
$user_info = json_decode( wp_json_encode( get_userdata( $customer_id ) ), true );
|
||||
$user_email = $user_info['data']['user_email'];
|
||||
$contact = $user_email;
|
||||
if ( empty( $contact ) ) {
|
||||
$contact = $customer_id;
|
||||
}
|
||||
$contact_vid = get_user_meta( $customer_id, 'hubwoo_user_vid', true );
|
||||
$invalid_contact = get_user_meta( $customer_id, 'hubwoo_invalid_contact', true );
|
||||
} else {
|
||||
$contact_vid = $order->get_meta('hubwoo_user_vid', true);
|
||||
$contact = $order->get_billing_email();
|
||||
$invalid_contact = $order->get_meta('hubwoo_invalid_contact', true);
|
||||
}
|
||||
|
||||
if ( count( $deal_updates ) ) {
|
||||
|
||||
$flag = true;
|
||||
if ( Hubwoo::is_access_token_expired() ) {
|
||||
|
||||
$hapikey = HUBWOO_CLIENT_ID;
|
||||
$hseckey = HUBWOO_SECRET_ID;
|
||||
$status = HubWooConnectionMananager::get_instance()->hubwoo_refresh_token( $hapikey, $hseckey );
|
||||
|
||||
if ( ! $status ) {
|
||||
|
||||
$flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $flag ) {
|
||||
|
||||
$deal_name = '#' . $order_id;
|
||||
|
||||
$user_detail['first_name'] = $order->get_billing_first_name();
|
||||
$user_detail['last_name'] = $order->get_billing_last_name();
|
||||
|
||||
foreach ( $user_detail as $value ) {
|
||||
if ( ! empty( $value ) ) {
|
||||
$deal_name .= ' ' . $value;
|
||||
}
|
||||
}
|
||||
|
||||
$filtergps = array(
|
||||
'filterGroups' => array(
|
||||
array(
|
||||
'filters' => array(
|
||||
array(
|
||||
'value' => $deal_name,
|
||||
'propertyName' => 'dealname',
|
||||
'operator' => 'EQ',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$response = HubWooConnectionMananager::get_instance()->search_object_record( 'deals', $filtergps );
|
||||
|
||||
if ( 200 == $response['status_code'] ) {
|
||||
$responce_body = json_decode( $response['body'] );
|
||||
$result = $responce_body->results;
|
||||
if ( ! empty( $result ) ) {
|
||||
foreach ( $result as $key => $value ) {
|
||||
$order->update_meta_data('hubwoo_ecomm_deal_id', $value->id);
|
||||
$order->update_meta_data('hubwoo_order_line_item_created', 'yes');
|
||||
$order->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$hubwoo_ecomm_deal_id =$order->get_meta('hubwoo_ecomm_deal_id', true);
|
||||
|
||||
if ( empty( $hubwoo_ecomm_deal_id ) ) {
|
||||
$response = HubWooConnectionMananager::get_instance()->create_object_record( 'deals', $deal_updates );
|
||||
if ( 201 == $response['status_code'] ) {
|
||||
$response_body = json_decode( $response['body'] );
|
||||
$hubwoo_ecomm_deal_id = $response_body->id;
|
||||
$order->update_meta_data('hubwoo_ecomm_deal_id', $hubwoo_ecomm_deal_id);
|
||||
$order->save();
|
||||
}
|
||||
} else {
|
||||
$response = HubWooConnectionMananager::get_instance()->update_object_record( 'deals', $hubwoo_ecomm_deal_id, $deal_updates );
|
||||
}
|
||||
|
||||
HubWooConnectionMananager::get_instance()->associate_object( 'deal', $hubwoo_ecomm_deal_id, 'contact', $contact_vid, 3 );
|
||||
|
||||
do_action( 'hubwoo_ecomm_deal_created', $order_id );
|
||||
|
||||
if ( 'yes' == $assc_deal_cmpy ) {
|
||||
if ( ! empty( $contact ) && empty( $invalid_contact ) ) {
|
||||
Hubwoo::hubwoo_associate_deal_company( $contact, $hubwoo_ecomm_deal_id );
|
||||
}
|
||||
}
|
||||
|
||||
$order->update_meta_data('hubwoo_ecomm_deal_upsert', 'no');
|
||||
$order->delete_meta_data('hubwoo_ecomm_deal_upsert');
|
||||
$order->save();
|
||||
|
||||
$response = self::hubwoo_ecomm_sync_line_items( $order_id );
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and Associate Line Items for an order.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $order_id - order id.
|
||||
* @static
|
||||
* @return array sync response from HubSpot.
|
||||
*/
|
||||
public static function hubwoo_ecomm_sync_line_items( $order_id ) {
|
||||
|
||||
if ( ! empty( $order_id ) ) {
|
||||
|
||||
$order = wc_get_order( $order_id );
|
||||
$line_updates = array();
|
||||
$order_items = $order->get_items();
|
||||
$object_ids = array();
|
||||
$response = array( 'status_code' => 206 );
|
||||
$no_products_found = false;
|
||||
|
||||
if ( is_array( $order_items ) && count( $order_items ) ) {
|
||||
|
||||
foreach ( $order_items as $item_key => $single_item ) :
|
||||
|
||||
$product_id = $single_item->get_variation_id();
|
||||
if ( 0 === $product_id ) {
|
||||
$product_id = $single_item->get_product_id();
|
||||
if ( 0 === $product_id ) {
|
||||
$no_products_found = true;
|
||||
}
|
||||
}
|
||||
if ( get_post_status( $product_id ) == 'trash' || get_post_status( $product_id ) == false ) {
|
||||
continue;
|
||||
}
|
||||
$item_sku = get_post_meta( $product_id, '_sku', true );
|
||||
if ( empty( $item_sku ) ) {
|
||||
$item_sku = $product_id;
|
||||
}
|
||||
|
||||
$line_item_hs_id = wc_get_order_item_meta( $item_key, 'hubwoo_ecomm_line_item_id', true );
|
||||
|
||||
if ( ! empty( $line_item_hs_id ) || 'yes' == $order->get_meta('hubwoo_order_line_item_created', 'no') ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$quantity = ! empty( $single_item->get_quantity() ) ? $single_item->get_quantity() : 0;
|
||||
$item_total = ! empty( $single_item->get_total() ) ? $single_item->get_total() : 0;
|
||||
$item_sub_total = ! empty( $single_item->get_subtotal() ) ? $single_item->get_subtotal() : 0;
|
||||
$product = $single_item->get_product();
|
||||
$name = self::hubwoo_ecomm_product_name( $product );
|
||||
$discount_amount = abs( $item_total - $item_sub_total );
|
||||
$discount_amount = $discount_amount / $quantity;
|
||||
$item_sub_total = $item_sub_total / $quantity;
|
||||
$hs_product_id = get_post_meta( $product_id, 'hubwoo_ecomm_pro_id', true );
|
||||
$object_ids[] = $item_key;
|
||||
|
||||
$properties = array(
|
||||
'quantity' => $quantity,
|
||||
'price' => $item_sub_total,
|
||||
'amount' => $item_total,
|
||||
'name' => $name,
|
||||
'discount_amount' => $discount_amount,
|
||||
'sku' => $item_sku,
|
||||
'tax_amount' => $single_item->get_total_tax(),
|
||||
);
|
||||
|
||||
if ( 'yes' != get_option( 'hubwoo_product_scope_needed', 'no' ) ) {
|
||||
$properties['hs_product_id'] = $hs_product_id;
|
||||
}
|
||||
|
||||
$properties = apply_filters( 'hubwoo_line_item_properties', $properties, $product_id, $order_id );
|
||||
|
||||
$line_updates[] = array(
|
||||
'properties' => $properties,
|
||||
);
|
||||
endforeach;
|
||||
}
|
||||
|
||||
$line_updates = apply_filters( 'hubwoo_custom_line_item', $line_updates, $order_id );
|
||||
|
||||
if ( count( $line_updates ) ) {
|
||||
|
||||
$line_updates = array(
|
||||
'inputs' => $line_updates,
|
||||
);
|
||||
|
||||
$flag = true;
|
||||
if ( Hubwoo::is_access_token_expired() ) {
|
||||
$hapikey = HUBWOO_CLIENT_ID;
|
||||
$hseckey = HUBWOO_SECRET_ID;
|
||||
$status = HubWooConnectionMananager::get_instance()->hubwoo_refresh_token( $hapikey, $hseckey );
|
||||
if ( ! $status ) {
|
||||
$flag = false;
|
||||
}
|
||||
}
|
||||
if ( $flag ) {
|
||||
$response = HubWooConnectionMananager::get_instance()->create_batch_object_record( 'line_items', $line_updates );
|
||||
}
|
||||
}
|
||||
|
||||
if ( 201 == $response['status_code'] || 206 == $response['status_code'] || empty( $object_ids ) ) {
|
||||
|
||||
$order->update_meta_data('hubwoo_ecomm_deal_created', 'yes');
|
||||
$order->save();
|
||||
|
||||
$deal_id = $order->get_meta('hubwoo_ecomm_deal_id', true);
|
||||
if ( isset( $response['body'] ) && ! empty( $response['body'] ) ) {
|
||||
$response_body = json_decode( $response['body'] );
|
||||
foreach ( $order_items as $item_key => $single_item ) :
|
||||
|
||||
$product_id = $single_item->get_variation_id();
|
||||
if ( 0 === $product_id ) {
|
||||
$product_id = $single_item->get_product_id();
|
||||
if ( 0 === $product_id ) {
|
||||
$no_products_found = true;
|
||||
}
|
||||
}
|
||||
if ( get_post_status( $product_id ) == 'trash' || get_post_status( $product_id ) == false ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$product = $single_item->get_product();
|
||||
$name = self::get_instance()->hubwoo_ecomm_product_name( $product );
|
||||
|
||||
if ( isset( $response_body ) && ! empty( $response_body ) ) {
|
||||
|
||||
foreach ( $response_body->results as $key => $value ) {
|
||||
|
||||
$line_item_hs_id = $value->id;
|
||||
$order->update_meta_data('hubwoo_order_line_item_created', 'yes');
|
||||
$order->save();
|
||||
$response = HubWooConnectionMananager::get_instance()->associate_object( 'deal', $deal_id, 'line_item', $line_item_hs_id, 19 );
|
||||
}
|
||||
}
|
||||
endforeach;
|
||||
|
||||
if ( 1 == get_option( 'hubwoo_deals_sync_running', 0 ) ) {
|
||||
|
||||
$current_count = get_option( 'hubwoo_deals_current_sync_count', 0 );
|
||||
update_option( 'hubwoo_deals_current_sync_count', ++$current_count );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start syncing an ecommerce deal
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $order_id - order id.
|
||||
* @return array sync response from HubSpot.
|
||||
*/
|
||||
public function hubwoo_ecomm_deals_sync( $order_id ) {
|
||||
|
||||
if ( ! empty( $order_id ) ) {
|
||||
$hubwoo_ecomm_order = wc_get_order( $order_id );
|
||||
if ( $hubwoo_ecomm_order instanceof WC_Order ) {
|
||||
$customer_id = $hubwoo_ecomm_order->get_customer_id();
|
||||
|
||||
if ( ! empty( $customer_id ) ) {
|
||||
$source = 'user';
|
||||
self::hubwoo_ecomm_contacts_with_id( $customer_id );
|
||||
} else {
|
||||
$source = 'guest';
|
||||
self::hubwoo_ecomm_guest_user( $order_id );
|
||||
}
|
||||
|
||||
$response = self::hubwoo_ecomm_sync_deal( $order_id, $source, $customer_id );
|
||||
update_option( 'hubwoo_last_sync_date', time() );
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create a formatted name of the product.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param int $product product object.
|
||||
* @return string formatted name of the product.
|
||||
*/
|
||||
public static function hubwoo_ecomm_product_name( $product ) {
|
||||
|
||||
if ( $product->get_sku() ) {
|
||||
$identifier = $product->get_sku();
|
||||
} else {
|
||||
$identifier = '#' . $product->get_id();
|
||||
}
|
||||
return sprintf( '%2$s (%1$s)', $identifier, $product->get_name() );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return formatted time for HubSpot
|
||||
*
|
||||
* @param int $unix_timestamp current timestamp.
|
||||
* @return string formatted time.
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function hubwoo_set_utc_midnight( $unix_timestamp ) {
|
||||
|
||||
$string = gmdate( 'Y-m-d H:i:s', $unix_timestamp );
|
||||
$date = new DateTime( $string );
|
||||
$wp_time_zone = get_option( 'timezone_string', '' );
|
||||
if ( empty( $wp_time_zone ) ) {
|
||||
$wp_time_zone = 'UTC';
|
||||
}
|
||||
$time_zone = new DateTimeZone( $wp_time_zone );
|
||||
$date->setTimezone( $time_zone );
|
||||
return $date->getTimestamp() * 1000; // in miliseconds.
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Manage Enums.
|
||||
*
|
||||
* @link https://makewebbetter.com/
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes/classes
|
||||
*/
|
||||
|
||||
/**
|
||||
* Manage Enums.
|
||||
*
|
||||
* @package makewebbetter-hubspot-for-woocommerce
|
||||
* @subpackage makewebbetter-hubspot-for-woocommerce/includes/classes
|
||||
*/
|
||||
abstract class HubWooEnum {
|
||||
|
||||
/**
|
||||
* Register all of the Enums.
|
||||
*
|
||||
* @since 1.0.4
|
||||
* @param any $value value for the enum.
|
||||
* @throws IllegalArgumentException If the value is illegal.
|
||||
*/
|
||||
final public function __construct( $value ) {
|
||||
$c = new ReflectionClass( $this );
|
||||
if ( ! in_array( $value, $c->getConstants() ) ) {
|
||||
throw IllegalArgumentException();
|
||||
}
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Conver the Enum value to String.
|
||||
*
|
||||
* @since 1.0.4
|
||||
*/
|
||||
final public function __toString() {
|
||||
return $this->value;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
<?php
|
||||
/* Silence is golden. */
|
||||
Reference in New Issue
Block a user