1
0
mirror of synced 2025-01-31 07:11:42 +03:00

some improvements

This commit is contained in:
Akolzin Dmitry 2019-03-20 15:51:31 +03:00
parent a04886c57f
commit 9394cd3524
23 changed files with 1666 additions and 926 deletions

View File

@ -0,0 +1,30 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data
{
protected $data = array(
'index' => '',
'city' => '',
'region' => '',
'text' => '',
);
public function reset_data()
{
$this->data = array(
'index' => '',
'city' => '',
'region' => '',
'text' => '',
);
}
}

View File

@ -0,0 +1,63 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
/**
* Class WC_Retailcrm_Abstracts_Data
*/
abstract class WC_Retailcrm_Abstracts_Data
{
/** @var string */
protected $filter_name;
/** @var array */
protected $data = array();
/**
* @return void
*/
abstract public function reset_data();
/**
* @param $data
*
* @return self
*/
abstract public function build($data);
protected function set_data_field($field, $value)
{
if (isset($this->data[$field]) && \gettype($value) !== \gettype($this->data[$field])) {
return false;
}
$this->data[$field] = $value;
return true;
}
/**
* @param $fields
*/
protected function set_data_fields($fields)
{
foreach ($fields as $field => $value) {
$this->set_data_field($field, $value);
}
}
/**
* @return array
*/
public function get_data()
{
return apply_filters('retailcrm_before_send_' . $this->filter_name, $this->data);
}
}

View File

@ -0,0 +1,705 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
abstract class WC_Retailcrm_Abstracts_Settings extends WC_Integration
{
/** @var string */
const YES = 'yes';
/** @var string */
const NO = 'no';
/** @var string */
public static $option_key;
/**
* WC_Retailcrm_Abstracts_Settings constructor.
*/
public function __construct() {
$this->id = 'integration-retailcrm';
$this->method_title = __('retailCRM', 'retailcrm');
$this->method_description = __('Integration with retailCRM management system.', 'retailcrm');
static::$option_key = $this->get_option_key();
if (isset($_GET['page']) && $_GET['page'] == 'wc-settings'
&& isset($_GET['tab']) && $_GET['tab'] == 'integration'
) {
add_action('init', array($this, 'init_settings_fields'), 99);
}
}
public function ajax_upload()
{
$ajax_url = admin_url('admin-ajax.php');
?>
<script type="text/javascript">
jQuery('#uploads-retailcrm').bind('click', function() {
jQuery.ajax({
type: "POST",
url: '<?php echo $ajax_url; ?>?action=do_upload',
success: function (response) {
alert('<?php echo __('Customers and orders were uploaded', 'retailcrm'); ?>');
console.log('AJAX response : ',response);
}
});
});
</script>
<?php
}
public function ajax_generate_icml()
{
$ajax_url = admin_url('admin-ajax.php');
?>
<script type="text/javascript">
jQuery('#icml-retailcrm, #wp-admin-bar-retailcrm_ajax_generate_icml').bind('click', function() {
jQuery.ajax({
type: "POST",
url: '<?php echo $ajax_url; ?>?action=generate_icml',
success: function (response) {
alert('<?php echo __('Catalog was generated', 'retailcrm'); ?>');
console.log('AJAX response : ', response);
}
});
});
</script>
<?php
}
public function ajax_selected_order()
{
$ajax_url = admin_url('admin-ajax.php');
$ids = $this->plugin_id . $this->id . '_single_order';
?>
<script type="text/javascript">
jQuery('#single_order_btn').bind('click', function() {
if (jQuery('#<?php echo $ids; ?>').val() == '') {
alert('<?php echo __('The field cannot be empty, enter the order ID', 'retailcrm'); ?>');
} else {
jQuery.ajax({
type: "POST",
url: '<?php echo $ajax_url; ?>?action=order_upload&order_ids_retailcrm=' + jQuery('#<?php echo $ids; ?>').val(),
success: function (response) {
alert('<?php echo __('Orders were uploaded', 'retailcrm'); ?>');
}
});
}
});
</script>
<?php
}
/**
* Initialize integration settings form fields.
*/
public function init_form_fields()
{
$this->form_fields = array(
array( 'title' => __( 'Main settings', 'retailcrm' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ),
'api_url' => array(
'title' => __( 'API of URL', 'retailcrm' ),
'type' => 'text',
'description' => __( 'Enter API of URL (https://yourdomain.retailcrm.pro).', 'retailcrm' ),
'desc_tip' => true,
'default' => ''
),
'api_key' => array(
'title' => __( 'API key', 'retailcrm' ),
'type' => 'text',
'description' => __( 'Enter your API key. You can find it in the administration section of retailCRM', 'retailcrm' ),
'desc_tip' => true,
'default' => ''
)
);
$api_version_list = array(
'v4' => 'v4',
'v5' => 'v5'
);
$this->form_fields[] = array(
'title' => __( 'API settings', 'retailcrm' ),
'type' => 'title',
'description' => '',
'id' => 'api_options'
);
$this->form_fields['api_version'] = array(
'title' => __( 'API version', 'retailcrm' ),
'description' => __( 'Select API version', 'retailcrm' ),
'css' => 'min-width:50px;',
'class' => 'select',
'type' => 'select',
'options' => $api_version_list,
'desc_tip' => true,
);
$this->form_fields[] = array(
'title' => __( 'Catalog settings', 'retailcrm' ),
'type' => 'title',
'description' => '',
'id' => 'catalog_options'
);
foreach (get_post_statuses() as $status_key => $status_value) {
$this->form_fields['p_' . $status_key] = array(
'title' => $status_value,
'label' => ' ',
'description' => '',
'class' => 'checkbox',
'type' => 'checkbox',
'desc_tip' => true,
);
}
if ($this->apiClient) {
if (isset($_GET['page']) && $_GET['page'] == 'wc-settings'
&& isset($_GET['tab']) && $_GET['tab'] == 'integration'
) {
add_action('admin_print_footer_scripts', array($this, 'show_blocks'), 99);
/**
* Order methods options
*/
$order_methods_option = array();
$order_methods_list = $this->apiClient->orderMethodsList();
if ($order_methods_list->isSuccessful()) {
foreach ($order_methods_list['orderMethods'] as $order_method) {
if ($order_method['active'] == false) {
continue;
}
$order_methods_option[$order_method['code']] = $order_method['name'];
}
$this->form_fields[] = array(
'title' => __('Order methods', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'order_methods_options'
);
$this->form_fields['order_methods'] = array(
'label' => ' ',
'title' => __('Order methods available for uploading from retailCRM', 'retailcrm'),
'class' => '',
'type' => 'multiselect',
'description' => __('Select order methods which will be uploaded from retailCRM to the website', 'retailcrm'),
'options' => $order_methods_option,
'css' => 'min-height:100px;',
'select_buttons' => true
);
}
/**
* Shipping options
*/
$shipping_option_list = array();
$retailcrm_shipping_list = $this->apiClient->deliveryTypesList();
if ($retailcrm_shipping_list->isSuccessful()) {
foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) {
$shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name'];
}
$wc_shipping_list = get_wc_shipping_methods();
$this->form_fields[] = array(
'title' => __('Delivery types', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'shipping_options'
);
foreach ($wc_shipping_list as $shipping_code => $shipping) {
if (isset($shipping['enabled']) && $shipping['enabled'] == static::YES) {
$this->form_fields[$shipping_code] = array(
'title' => __($shipping['title'], 'woocommerce'),
'description' => __($shipping['description'], 'woocommerce'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $shipping_option_list,
'desc_tip' => true,
);
}
}
}
/**
* Payment options
*/
$payment_option_list = array();
$retailcrm_payment_list = $this->apiClient->paymentTypesList();
if ($retailcrm_payment_list->isSuccessful()) {
foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) {
$payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name'];
}
$wc_payment = WC_Payment_Gateways::instance();
$this->form_fields[] = array(
'title' => __('Payment types', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'payment_options'
);
foreach ($wc_payment->payment_gateways() as $payment) {
$this->form_fields[$payment->id] = array(
'title' => __($payment->method_title, 'woocommerce'),
'description' => __($payment->method_description, 'woocommerce'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $payment_option_list,
'desc_tip' => true,
);
}
}
/**
* Statuses options
*/
$statuses_option_list = array();
$retailcrm_statuses_list = $this->apiClient->statusesList();
if ($retailcrm_statuses_list->isSuccessful()) {
foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) {
$statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name'];
}
$wc_statuses = wc_get_order_statuses();
$this->form_fields[] = array(
'title' => __('Statuses', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'statuses_options'
);
foreach ($wc_statuses as $idx => $name) {
$uid = str_replace('wc-', '', $idx);
$this->form_fields[$uid] = array(
'title' => __($name, 'woocommerce'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $statuses_option_list,
'desc_tip' => true,
);
}
}
/**
* Inventories options
*/
$this->form_fields[] = array(
'title' => __('Setting of the stock balance', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'invent_options'
);
$this->form_fields['sync'] = array(
'label' => __('Synchronization of the stock balance', 'retailcrm'),
'title' => __('Stock balance', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox',
'description' => __('Enable this setting if you would like to get information on leftover stocks from retailCRM to the website.', 'retailcrm')
);
/**
* UA options
*/
$this->form_fields[] = array(
'title' => __('UA settings', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'ua_options'
);
$this->form_fields['ua'] = array(
'label' => __('Activate UA', 'retailcrm'),
'title' => __('UA', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox',
'description' => __('Enable this setting for uploading data to UA', 'retailcrm')
);
$this->form_fields['ua_code'] = array(
'title' => __('UA tracking code', 'retailcrm'),
'class' => 'input',
'type' => 'input'
);
$this->form_fields['ua_custom'] = array(
'title' => __('User parameter', 'retailcrm'),
'class' => 'input',
'type' => 'input'
);
/**
* Daemon collector settings
*/
$this->form_fields[] = array(
'title' => __('Daemon Collector settings', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'invent_options'
);
$this->form_fields['daemon_collector'] = array(
'label' => __('Activate Daemon Collector', 'retailcrm'),
'title' => __('Daemon Collector', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox',
'description' => __('Enable this setting for activate Daemon Collector on site', 'retailcrm')
);
$this->form_fields['daemon_collector_key'] = array(
'title' => __('Site key', 'retailcrm'),
'class' => 'input',
'type' => 'input'
);
/**
* Uploads options
*/
$options = array_filter(get_option(static::$option_key));
if (!isset($options['uploads'])) {
$this->form_fields[] = array(
'title' => __('Settings of uploading', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'upload_options'
);
$this->form_fields['upload-button'] = array(
'label' => __('Upload', 'retailcrm'),
'title' => __('Uploading all customers and orders', 'retailcrm' ),
'type' => 'button',
'description' => __('Uploading the existing customers and orders to retailCRM', 'retailcrm' ),
'desc_tip' => true,
'id' => 'uploads-retailcrm'
);
}
/*
* Generate icml file
*/
$this->form_fields[] = array(
'title' => __('Generating ICML catalog', 'retailcrm'),
'type' => 'title',
'description' => '',
'id' => 'icml_options'
);
$this->form_fields[] = array(
'label' => __('Generate now', 'retailcrm'),
'title' => __('Generating ICML', 'retailcrm'),
'type' => 'button',
'description' => __('This functionality allows to generate ICML products catalog for uploading to retailCRM.', 'retailcrm'),
'desc_tip' => true,
'id' => 'icml-retailcrm'
);
$this->form_fields['icml'] = array(
'label' => __('Generating ICML', 'retailcrm'),
'title' => __('Generating ICML catalog by wp-cron', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
/*
* Upload single order
*/
$this->form_field[] = array(
'title' => __('Upload the order by ID', 'retailcrm'),
'type' => 'title',
'description' => '',
'id' => 'order_options'
);
$this->form_fields['single_order'] = array(
'label' => __('Order identifier', 'retailcrm'),
'title' => __('Orders identifiers', 'retailcrm'),
'type' => 'input',
'description' => __('Enter orders identifiers separated by a comma.', 'retailcrm'),
'desc_tip' => true
);
$this->form_fields[] = array(
'label' => __('Upload', 'retailcrm'),
'title' => __('Uploading orders by identifiers.', 'retailcrm'),
'type' => 'button',
'description' => __('This functionality allows to upload orders to CRM differentially.', 'retailcrm'),
'desc_tip' => true,
'id' => 'single_order_btn'
);
$this->form_fields['history'] = array(
'label' => __('Activate history uploads', 'retailcrm'),
'title' => __('Upload data from retailCRM', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
$this->form_fields['deactivate_update_order'] = array(
'label' => __('Активировать редактирование данных в retailCRM', 'retailcrm'),
'title' => __('Обновление данных в retailCRM', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
$this->form_fields['bind_by_sku'] = array(
'label' => __('Активировать связь по sku(xmlId)', 'retailcrm'),
'title' => __('Синхронизация остатков и связь товаров', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
$this->form_fields['update_number'] = array(
'label' => __('Активировать передачу номера в retailCRM', 'retailcrm'),
'title' => __('Передача номера заказа', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
}
}
}
/**
* Generate html button
*
* @param string $key
* @param array $data
*
* @return string
*/
public function generate_button_html($key, $data)
{
$field = $this->plugin_id . $this->id . '_' . $key;
$defaults = array(
'class' => 'button-secondary',
'css' => '',
'custom_attributes' => array(),
'desc_tip' => false,
'description' => '',
'title' => '',
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
<?php echo $this->get_tooltip_html( $data ); ?>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['label'] ); ?></span></legend>
<button id="<?php echo $data['id']; ?>" class="<?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo wp_kses_post( $data['label'] ); ?></button>
<?php echo $this->get_description_html( $data ); ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate html title block settings
*
* @param string $key
* @param array $data
*
* @return string
*/
public function generate_heading_html($key, $data)
{
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'class' => '',
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
</table>
<h2 class="wc-settings-sub-title retailcrm_hidden <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <span style="opacity:0.5;float: right;">&#11015;</span></h2>
<?php if ( ! empty( $data['description'] ) ) : ?>
<p><?php echo wp_kses_post( $data['description'] ); ?></p>
<?php endif; ?>
<table class="form-table" style="display: none;">
<?php
return ob_get_clean();
}
/**
* Validate API version
*
* @param string $key
* @param string $value
*
* @return string
*/
public function validate_api_version_field($key, $value)
{
$post = $this->get_post_data();
$versionMap = array(
'v4' => '4.0',
'v5' => '5.0'
);
$api = new WC_Retailcrm_Proxy(
$post[$this->plugin_id . $this->id . '_api_url'],
$post[$this->plugin_id . $this->id . '_api_key']
);
$response = $api->apiVersions();
if ($response && $response->isSuccessful()) {
if (!in_array($versionMap[$value], $response['versions'])) {
WC_Admin_Settings::add_error( esc_html__( 'The selected API version is unavailable', 'retailcrm' ) );
$value = '';
}
}
return $value;
}
/**
* Validate API url
*
* @param string $key
* @param string $value
*
* @return string
*/
public function validate_api_url_field($key, $value)
{
$post = $this->get_post_data();
$api = new WC_Retailcrm_Proxy(
$value,
$post[$this->plugin_id . $this->id . '_api_key']
);
$response = $api->apiVersions();
if ($response == null) {
WC_Admin_Settings::add_error(esc_html__( 'Enter the correct URL of CRM', 'retailcrm'));
$value = '';
}
return $value;
}
/**
* Validate API key
*
* @param string $key
* @param string $value
*
* @return string
*/
public function validate_api_key_field($key, $value)
{
$post = $this->get_post_data();
$api = new WC_Retailcrm_Proxy(
$post[$this->plugin_id . $this->id . '_api_url'],
$value
);
$response = $api->apiVersions();
if (!is_object($response)) {
$value = '';
}
if (!$response->isSuccessful()) {
WC_Admin_Settings::add_error( esc_html__( 'Enter the correct API key', 'retailcrm' ) );
$value = '';
}
return $value;
}
/**
* Scritp show|hide block settings
*/
function show_blocks()
{
?>
<script type="text/javascript">
jQuery('h2.retailcrm_hidden').hover().css({
'cursor':'pointer',
'width':'310px'
});
jQuery('h2.retailcrm_hidden').toggle(
function() {
jQuery(this).next('table.form-table').show(100);
jQuery(this).find('span').html('&#11014;');
},
function() {
jQuery(this).next('table.form-table').hide(100);
jQuery(this).find('span').html('&#11015;');
}
);
</script>
<?php
}
/**
* Add button in admin
*/
function add_retailcrm_button() {
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() || !is_admin())
return;
$wp_admin_bar->add_menu(
array(
'id' => 'retailcrm_top_menu',
'title' => __('retailCRM', 'retailcrm')
)
);
$wp_admin_bar->add_menu(
array(
'id' => 'retailcrm_ajax_generate_icml',
'title' => __('Generating ICML catalog', 'retailcrm'),
'href' => '#',
'parent' => 'retailcrm_top_menu',
'class' => 'retailcrm_ajax_generate_icml'
)
);
$wp_admin_bar->add_menu(
array(
'id' => 'retailcrm_ajax_generate_setings',
'title' => __('Settings', 'retailcrm'),
'href'=> get_site_url().'/wp-admin/admin.php?page=wc-settings&tab=integration&section=integration-retailcrm',
'parent' => 'retailcrm_top_menu',
'class' => 'retailcrm_ajax_settings'
)
);
}
}

View File

@ -12,30 +12,27 @@ if (!class_exists('WC_Retailcrm_Base')) {
/**
* Class WC_Retailcrm_Base
*/
class WC_Retailcrm_Base extends WC_Integration {
public static $option_key;
class WC_Retailcrm_Base extends WC_Retailcrm_Abstracts_Settings
{
protected $api_url;
protected $api_key;
private $apiClient;
protected $apiClient;
protected $order_item;
protected $order_address;
protected $customers;
protected $orders;
/**
* Init and hook in the integration.
* @param $retailcrm (default = false)
*/
public function __construct($retailcrm = false) {
//global $woocommerce;
parent::__construct();
if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) {
include_once( __DIR__ . '/api/class-wc-retailcrm-proxy.php' );
if (!class_exists( 'WC_Retailcrm_Proxy')) {
include_once(__DIR__ . '/api/class-wc-retailcrm-proxy.php');
}
$this->id = 'integration-retailcrm';
$this->method_title = __('retailCRM', 'retailcrm');
$this->method_description = __('Integration with retailCRM management system.', 'retailcrm');
if ($retailcrm === false) {
$this->apiClient = $this->getApiClient();
} else {
@ -43,7 +40,29 @@ if (!class_exists('WC_Retailcrm_Base')) {
$this->init_settings_fields();
}
self::$option_key = $this->get_option_key();
if (!class_exists('WC_Retailcrm_Orders')) {
include_once(static::checkCustomFile('orders'));
}
if (!class_exists('WC_Retailcrm_Customers')) {
include_once(static::checkCustomFile('customers'));
}
$this->customers = new WC_Retailcrm_Customers(
$this->apiClient,
$this->settings,
new WC_Retailcrm_Customer_Address
);
$this->orders = new WC_Retailcrm_Orders(
$this->apiClient,
$this->settings,
new WC_Retailcrm_Order_Item($this->settings),
new WC_Retailcrm_Order_Address,
$this->customers,
new WC_Retailcrm_Order($this->settings),
new WC_Retailcrm_Order_Payment($this->settings)
);
// Actions.
add_action('woocommerce_update_options_integration_' . $this->id, array($this, 'process_admin_options'));
@ -61,15 +80,14 @@ if (!class_exists('WC_Retailcrm_Base')) {
add_action('admin_print_footer_scripts', array($this, 'ajax_selected_order'), 99);
add_action('woocommerce_created_customer', array($this, 'create_customer'), 10, 1);
add_action('woocommerce_update_customer', array($this, 'update_customer'), 10, 1);
add_action('woocommerce_update_order', array($this, 'update_order'), 11, 1);
add_action('wp_print_scripts', array($this, 'initialize_analytics'), 98);
add_action('wp_print_scripts', array($this, 'initialize_daemon_collector'), 99);
add_action('wp_print_footer_scripts', array($this, 'send_analytics'), 99);
if (isset($_GET['page']) && $_GET['page'] == 'wc-settings'
&& isset($_GET['tab']) && $_GET['tab'] == 'integration'
if (!$this->get_option('deactivate_update_order')
|| $this->get_option('deactivate_update_order') == static::NO
) {
add_action('init', array($this, 'init_settings_fields'), 99);
add_action('woocommerce_update_order', array($this, 'update_order'), 11, 1);
}
// Deactivate hook
@ -92,27 +110,27 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function api_sanitized($settings)
{
if (isset($settings['sync']) && $settings['sync'] == 'yes') {
if (isset($settings['sync']) && $settings['sync'] == static::YES) {
if (!wp_next_scheduled('retailcrm_inventories')) {
wp_schedule_event(time(), 'fiveteen_minutes', 'retailcrm_inventories');
}
} elseif (isset($settings['sync']) && $settings['sync'] == 'no') {
} elseif (isset($settings['sync']) && $settings['sync'] == static::NO) {
wp_clear_scheduled_hook('retailcrm_inventories');
}
if (isset($settings['history']) && $settings['history'] == 'yes') {
if (isset($settings['history']) && $settings['history'] == static::YES) {
if (!wp_next_scheduled('retailcrm_history')) {
wp_schedule_event(time(), 'five_minutes', 'retailcrm_history');
}
} elseif (isset($settings['history']) && $settings['history'] == 'no') {
} elseif (isset($settings['history']) && $settings['history'] == static::NO) {
wp_clear_scheduled_hook('retailcrm_history');
}
if (isset($settings['icml']) && $settings['icml'] == 'yes') {
if (isset($settings['icml']) && $settings['icml'] == static::YES) {
if (!wp_next_scheduled('retailcrm_icml')) {
wp_schedule_event(time(), 'three_hours', 'retailcrm_icml');
}
} elseif (isset($settings['icml']) && $settings['icml'] == 'no') {
} elseif (isset($settings['icml']) && $settings['icml'] == static::NO) {
wp_clear_scheduled_hook('retailcrm_icml');
}
@ -140,7 +158,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function generate_icml() {
if (!class_exists('WC_Retailcrm_Icml')) {
require_once (self::checkCustomFile('icml'));
require_once (static::checkCustomFile('icml'));
}
$retailcrm_icml = new WC_Retailcrm_Icml();
@ -152,7 +170,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function retailcrm_history_get() {
if (!class_exists('WC_Retailcrm_History')) {
include_once(self::checkCustomFile('history'));
include_once(static::checkCustomFile('history'));
}
$retailcrm_history = new WC_Retailcrm_History($this->apiClient);
@ -163,12 +181,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
* @param int $order_id
*/
public function retailcrm_process_order($order_id) {
if (!class_exists('WC_Retailcrm_Orders')) {
include_once(self::checkCustomFile('orders'));
}
$retailcm_order = new WC_Retailcrm_Orders($this->apiClient);
$retailcm_order->orderCreate($order_id);
$this->orders->orderCreate($order_id);
}
/**
@ -176,7 +189,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function load_stocks() {
if (!class_exists('WC_Retailcrm_Inventories')) {
include_once(self::checkCustomFile('inventories'));
include_once(static::checkCustomFile('inventories'));
}
$inventories = new WC_Retailcrm_Inventories($this->apiClient);
@ -187,20 +200,14 @@ if (!class_exists('WC_Retailcrm_Base')) {
* Upload selected orders
*/
public function order_upload() {
if (!class_exists('WC_Retailcrm_Orders')) {
include_once(self::checkCustomFile('orders'));
}
$ids = false;
if (isset($_GET['order_ids_retailcrm'])) {
$ids = explode(',', $_GET['order_ids_retailcrm']);
}
$retailcm_order = new WC_Retailcrm_Orders($this->apiClient);
if ($ids) {
$retailcm_order->ordersUpload($ids, true);
$this->orders->ordersUpload($ids, true);
}
}
@ -209,85 +216,13 @@ if (!class_exists('WC_Retailcrm_Base')) {
*/
public function upload_to_crm()
{
if (!class_exists('WC_Retailcrm_Orders')) {
include_once(self::checkCustomFile('orders'));
}
$options = array_filter(get_option(static::$option_key));
if (!class_exists('WC_Retailcrm_Customers')) {
include_once(self::checkCustomFile('customers'));
}
$this->customers->customersUpload();
$this->orders->ordersUpload();
$options = array_filter(get_option(self::$option_key));
$retailcrm_customers = new WC_Retailcrm_Customers($this->apiClient);
$retailcrm_orders = new WC_Retailcrm_Orders($this->apiClient);
$retailcrm_customers->customersUpload();
$retailcrm_orders->ordersUpload();
$options['uploads'] = 'yes';
update_option(self::$option_key, $options);
}
public function ajax_upload()
{
$ajax_url = admin_url('admin-ajax.php');
?>
<script type="text/javascript">
jQuery('#uploads-retailcrm').bind('click', function() {
jQuery.ajax({
type: "POST",
url: '<?php echo $ajax_url; ?>?action=do_upload',
success: function (response) {
alert('<?php echo __('Customers and orders were uploaded', 'retailcrm'); ?>');
console.log('AJAX response : ',response);
}
});
});
</script>
<?php
}
public function ajax_generate_icml()
{
$ajax_url = admin_url('admin-ajax.php');
?>
<script type="text/javascript">
jQuery('#icml-retailcrm, #wp-admin-bar-retailcrm_ajax_generate_icml').bind('click', function() {
jQuery.ajax({
type: "POST",
url: '<?php echo $ajax_url; ?>?action=generate_icml',
success: function (response) {
alert('<?php echo __('Catalog was generated', 'retailcrm'); ?>');
console.log('AJAX response : ', response);
}
});
});
</script>
<?php
}
public function ajax_selected_order()
{
$ajax_url = admin_url('admin-ajax.php');
$ids = $this->plugin_id . $this->id . '_single_order';
?>
<script type="text/javascript">
jQuery('#single_order_btn').bind('click', function() {
if (jQuery('#<?php echo $ids; ?>').val() == '') {
alert('<?php echo __('The field cannot be empty, enter the order ID', 'retailcrm'); ?>');
} else {
jQuery.ajax({
type: "POST",
url: '<?php echo $ajax_url; ?>?action=order_upload&order_ids_retailcrm=' + jQuery('#<?php echo $ids; ?>').val(),
success: function (response) {
alert('<?php echo __('Orders were uploaded', 'retailcrm'); ?>');
}
});
}
});
</script>
<?php
$options['uploads'] = static::YES;
update_option(static::$option_key, $options);
}
/**
@ -300,12 +235,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
return;
}
if (!class_exists( 'WC_Retailcrm_Customers')) {
include_once(self::checkCustomFile('customers'));
}
$retailcrm_customer = new WC_Retailcrm_Customers($this->apiClient);
$retailcrm_customer->createCustomer($customer_id);
$this->customers->createCustomer($customer_id);
}
/**
@ -318,12 +248,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
return;
}
if (!class_exists('WC_Retailcrm_Customers')) {
include_once(self::checkCustomFile('customers'));
}
$retailcrm_customer = new WC_Retailcrm_Customers($this->apiClient);
$retailcrm_customer->updateCustomer($customer_id);
$this->customers->updateCustomer($customer_id);
}
/**
@ -336,12 +261,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
return;
}
if (!class_exists('WC_Retailcrm_Orders')) {
include_once(self::checkCustomFile('orders'));
}
$retailcrm_order = new WC_Retailcrm_Orders($this->apiClient);
$retailcrm_order->updateOrder($order_id);
$this->orders->updateOrder($order_id);
}
/**
@ -350,7 +270,7 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function initialize_analytics()
{
if (!class_exists('WC_Retailcrm_Google_Analytics')) {
include_once(self::checkCustomFile('ga'));
include_once(static::checkCustomFile('ga'));
}
if ($this->get_option('ua') && $this->get_option('ua_code')) {
@ -367,10 +287,10 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function send_analytics()
{
if (!class_exists('WC_Retailcrm_Google_Analytics')) {
include_once(self::checkCustomFile('ga'));
include_once(static::checkCustomFile('ga'));
}
if ($this->get_option('ua') == 'yes' && $this->get_option('ua_code') && is_checkout()) {
if ($this->get_option('ua') == static::YES && $this->get_option('ua_code') && is_checkout()) {
$retailcrm_analytics = WC_Retailcrm_Google_Analytics::getInstance($this->settings);
echo $retailcrm_analytics->send_analytics();
} else {
@ -384,10 +304,10 @@ if (!class_exists('WC_Retailcrm_Base')) {
public function initialize_daemon_collector()
{
if (!class_exists('WC_Retailcrm_Daemon_Collector')) {
include_once(self::checkCustomFile('daemon-collector'));
include_once(static::checkCustomFile('daemon-collector'));
}
if ($this->get_option('daemon_collector') == 'yes' && $this->get_option('daemon_collector_key')) {
if ($this->get_option('daemon_collector') == static::YES && $this->get_option('daemon_collector_key')) {
$retailcrm_daemon_collector = WC_Retailcrm_Daemon_Collector::getInstance($this->settings);
echo $retailcrm_daemon_collector->initialize_daemon_collector();
} else {
@ -395,556 +315,6 @@ if (!class_exists('WC_Retailcrm_Base')) {
}
}
/**
* Initialize integration settings form fields.
*/
public function init_form_fields()
{
$this->form_fields = array(
array( 'title' => __( 'Main settings', 'retailcrm' ), 'type' => 'title', 'desc' => '', 'id' => 'general_options' ),
'api_url' => array(
'title' => __( 'API of URL', 'retailcrm' ),
'type' => 'text',
'description' => __( 'Enter API of URL (https://yourdomain.retailcrm.pro).', 'retailcrm' ),
'desc_tip' => true,
'default' => ''
),
'api_key' => array(
'title' => __( 'API key', 'retailcrm' ),
'type' => 'text',
'description' => __( 'Enter your API key. You can find it in the administration section of retailCRM', 'retailcrm' ),
'desc_tip' => true,
'default' => ''
)
);
$api_version_list = array(
'v4' => 'v4',
'v5' => 'v5'
);
$this->form_fields[] = array(
'title' => __( 'API settings', 'retailcrm' ),
'type' => 'title',
'description' => '',
'id' => 'api_options'
);
$this->form_fields['api_version'] = array(
'title' => __( 'API version', 'retailcrm' ),
'description' => __( 'Select API version', 'retailcrm' ),
'css' => 'min-width:50px;',
'class' => 'select',
'type' => 'select',
'options' => $api_version_list,
'desc_tip' => true,
);
$this->form_fields[] = array(
'title' => __( 'Catalog settings', 'retailcrm' ),
'type' => 'title',
'description' => '',
'id' => 'catalog_options'
);
foreach (get_post_statuses() as $status_key => $status_value) {
$this->form_fields['p_' . $status_key] = array(
'title' => $status_value,
'label' => ' ',
'description' => '',
'class' => 'checkbox',
'type' => 'checkbox',
'desc_tip' => true,
);
}
if ($this->apiClient) {
if (isset($_GET['page']) && $_GET['page'] == 'wc-settings'
&& isset($_GET['tab']) && $_GET['tab'] == 'integration'
) {
add_action('admin_print_footer_scripts', array($this, 'show_blocks'), 99);
/**
* Order methods options
*/
$order_methods_option = array();
$order_methods_list = $this->apiClient->orderMethodsList();
if ($order_methods_list->isSuccessful()) {
foreach ($order_methods_list['orderMethods'] as $order_method) {
if ($order_method['active'] == false) {
continue;
}
$order_methods_option[$order_method['code']] = $order_method['name'];
}
$this->form_fields[] = array(
'title' => __('Order methods', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'order_methods_options'
);
$this->form_fields['order_methods'] = array(
'label' => ' ',
'title' => __('Order methods available for uploading from retailCRM', 'retailcrm'),
'class' => '',
'type' => 'multiselect',
'description' => __('Select order methods which will be uploaded from retailCRM to the website', 'retailcrm'),
'options' => $order_methods_option,
'css' => 'min-height:100px;',
'select_buttons' => true
);
}
/**
* Shipping options
*/
$shipping_option_list = array();
$retailcrm_shipping_list = $this->apiClient->deliveryTypesList();
if ($retailcrm_shipping_list->isSuccessful()) {
foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) {
$shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name'];
}
$wc_shipping_list = get_wc_shipping_methods();
$this->form_fields[] = array(
'title' => __('Delivery types', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'shipping_options'
);
foreach ($wc_shipping_list as $shipping_code => $shipping) {
if (isset($shipping['enabled']) && $shipping['enabled'] == 'yes') {
$this->form_fields[$shipping_code] = array(
'title' => __($shipping['title'], 'woocommerce'),
'description' => __($shipping['description'], 'woocommerce'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $shipping_option_list,
'desc_tip' => true,
);
}
}
}
/**
* Payment options
*/
$payment_option_list = array();
$retailcrm_payment_list = $this->apiClient->paymentTypesList();
if ($retailcrm_payment_list->isSuccessful()) {
foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) {
$payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name'];
}
$wc_payment = WC_Payment_Gateways::instance();
$this->form_fields[] = array(
'title' => __('Payment types', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'payment_options'
);
foreach ($wc_payment->payment_gateways() as $payment) {
$this->form_fields[$payment->id] = array(
'title' => __($payment->method_title, 'woocommerce'),
'description' => __($payment->method_description, 'woocommerce'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $payment_option_list,
'desc_tip' => true,
);
}
}
/**
* Statuses options
*/
$statuses_option_list = array();
$retailcrm_statuses_list = $this->apiClient->statusesList();
if ($retailcrm_statuses_list->isSuccessful()) {
foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) {
$statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name'];
}
$wc_statuses = wc_get_order_statuses();
$this->form_fields[] = array(
'title' => __('Statuses', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'statuses_options'
);
foreach ($wc_statuses as $idx => $name) {
$uid = str_replace('wc-', '', $idx);
$this->form_fields[$uid] = array(
'title' => __($name, 'woocommerce'),
'css' => 'min-width:350px;',
'class' => 'select',
'type' => 'select',
'options' => $statuses_option_list,
'desc_tip' => true,
);
}
}
/**
* Inventories options
*/
$this->form_fields[] = array(
'title' => __('Setting of the stock balance', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'invent_options'
);
$this->form_fields['sync'] = array(
'label' => __('Synchronization of the stock balance', 'retailcrm'),
'title' => __('Stock balance', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox',
'description' => __('Enable this setting if you would like to get information on leftover stocks from retailCRM to the website.', 'retailcrm')
);
/**
* UA options
*/
$this->form_fields[] = array(
'title' => __('UA settings', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'ua_options'
);
$this->form_fields['ua'] = array(
'label' => __('Activate UA', 'retailcrm'),
'title' => __('UA', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox',
'description' => __('Enable this setting for uploading data to UA', 'retailcrm')
);
$this->form_fields['ua_code'] = array(
'title' => __('UA tracking code', 'retailcrm'),
'class' => 'input',
'type' => 'input'
);
$this->form_fields['ua_custom'] = array(
'title' => __('User parameter', 'retailcrm'),
'class' => 'input',
'type' => 'input'
);
/**
* Daemon collector settings
*/
$this->form_fields[] = array(
'title' => __('Daemon Collector settings', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'invent_options'
);
$this->form_fields['daemon_collector'] = array(
'label' => __('Activate Daemon Collector', 'retailcrm'),
'title' => __('Daemon Collector', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox',
'description' => __('Enable this setting for activate Daemon Collector on site', 'retailcrm')
);
$this->form_fields['daemon_collector_key'] = array(
'title' => __('Site key', 'retailcrm'),
'class' => 'input',
'type' => 'input'
);
/**
* Uploads options
*/
$options = array_filter(get_option(self::$option_key));
if (!isset($options['uploads'])) {
$this->form_fields[] = array(
'title' => __('Settings of uploading', 'retailcrm'),
'type' => 'heading',
'description' => '',
'id' => 'upload_options'
);
$this->form_fields['upload-button'] = array(
'label' => __('Upload', 'retailcrm'),
'title' => __('Uploading all customers and orders', 'retailcrm' ),
'type' => 'button',
'description' => __('Uploading the existing customers and orders to retailCRM', 'retailcrm' ),
'desc_tip' => true,
'id' => 'uploads-retailcrm'
);
}
/*
* Generate icml file
*/
$this->form_fields[] = array(
'title' => __('Generating ICML catalog', 'retailcrm'),
'type' => 'title',
'description' => '',
'id' => 'icml_options'
);
$this->form_fields[] = array(
'label' => __('Generate now', 'retailcrm'),
'title' => __('Generating ICML', 'retailcrm'),
'type' => 'button',
'description' => __('This functionality allows to generate ICML products catalog for uploading to retailCRM.', 'retailcrm'),
'desc_tip' => true,
'id' => 'icml-retailcrm'
);
$this->form_fields['icml'] = array(
'label' => __('Generating ICML', 'retailcrm'),
'title' => __('Generating ICML catalog by wp-cron', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
/*
* Upload single order
*/
$this->form_field[] = array(
'title' => __('Upload the order by ID', 'retailcrm'),
'type' => 'title',
'description' => '',
'id' => 'order_options'
);
$this->form_fields['single_order'] = array(
'label' => __('Order identifier', 'retailcrm'),
'title' => __('Orders identifiers', 'retailcrm'),
'type' => 'input',
'description' => __('Enter orders identifiers separated by a comma.', 'retailcrm'),
'desc_tip' => true
);
$this->form_fields[] = array(
'label' => __('Upload', 'retailcrm'),
'title' => __('Uploading orders by identifiers.', 'retailcrm'),
'type' => 'button',
'description' => __('This functionality allows to upload orders to CRM differentially.', 'retailcrm'),
'desc_tip' => true,
'id' => 'single_order_btn'
);
$this->form_fields['history'] = array(
'label' => __('Activate history uploads', 'retailcrm'),
'title' => __('Upload data from retailCRM', 'retailcrm'),
'class' => 'checkbox',
'type' => 'checkbox'
);
}
}
}
/**
* Generate html button
*
* @param string $key
* @param array $data
*
* @return string
*/
public function generate_button_html($key, $data)
{
$field = $this->plugin_id . $this->id . '_' . $key;
$defaults = array(
'class' => 'button-secondary',
'css' => '',
'custom_attributes' => array(),
'desc_tip' => false,
'description' => '',
'title' => '',
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $field ); ?>"><?php echo wp_kses_post( $data['title'] ); ?></label>
<?php echo $this->get_tooltip_html( $data ); ?>
</th>
<td class="forminp">
<fieldset>
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['label'] ); ?></span></legend>
<button id="<?php echo $data['id']; ?>" class="<?php echo esc_attr( $data['class'] ); ?>" type="button" name="<?php echo esc_attr( $field ); ?>" id="<?php echo esc_attr( $field ); ?>" style="<?php echo esc_attr( $data['css'] ); ?>" <?php echo $this->get_custom_attribute_html( $data ); ?>><?php echo wp_kses_post( $data['label'] ); ?></button>
<?php echo $this->get_description_html( $data ); ?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Generate html title block settings
*
* @param string $key
* @param array $data
*
* @return string
*/
public function generate_heading_html($key, $data)
{
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'class' => '',
);
$data = wp_parse_args( $data, $defaults );
ob_start();
?>
</table>
<h2 class="wc-settings-sub-title retailcrm_hidden <?php echo esc_attr( $data['class'] ); ?>" id="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <span style="opacity:0.5;float: right;">&#11015;</span></h2>
<?php if ( ! empty( $data['description'] ) ) : ?>
<p><?php echo wp_kses_post( $data['description'] ); ?></p>
<?php endif; ?>
<table class="form-table" style="display: none;">
<?php
return ob_get_clean();
}
/**
* Validate API version
*
* @param string $key
* @param string $value
*
* @return string
*/
public function validate_api_version_field($key, $value)
{
$post = $this->get_post_data();
$versionMap = array(
'v4' => '4.0',
'v5' => '5.0'
);
$api = new WC_Retailcrm_Proxy(
$post[$this->plugin_id . $this->id . '_api_url'],
$post[$this->plugin_id . $this->id . '_api_key']
);
$response = $api->apiVersions();
if ($response && $response->isSuccessful()) {
if (!in_array($versionMap[$value], $response['versions'])) {
WC_Admin_Settings::add_error( esc_html__( 'The selected API version is unavailable', 'retailcrm' ) );
$value = '';
}
}
return $value;
}
/**
* Validate API url
*
* @param string $key
* @param string $value
*
* @return string
*/
public function validate_api_url_field($key, $value)
{
$post = $this->get_post_data();
$api = new WC_Retailcrm_Proxy(
$value,
$post[$this->plugin_id . $this->id . '_api_key']
);
$response = $api->apiVersions();
if ($response == null) {
WC_Admin_Settings::add_error(esc_html__( 'Enter the correct URL of CRM', 'retailcrm'));
$value = '';
}
return $value;
}
/**
* Validate API key
*
* @param string $key
* @param string $value
*
* @return string
*/
public function validate_api_key_field($key, $value)
{
$post = $this->get_post_data();
$api = new WC_Retailcrm_Proxy(
$post[$this->plugin_id . $this->id . '_api_url'],
$value
);
$response = $api->apiVersions();
if (!is_object($response)) {
$value = '';
}
if (!$response->isSuccessful()) {
WC_Admin_Settings::add_error( esc_html__( 'Enter the correct API key', 'retailcrm' ) );
$value = '';
}
return $value;
}
/**
* Scritp show|hide block settings
*/
function show_blocks()
{
?>
<script type="text/javascript">
jQuery('h2.retailcrm_hidden').hover().css({
'cursor':'pointer',
'width':'310px'
});
jQuery('h2.retailcrm_hidden').toggle(
function() {
jQuery(this).next('table.form-table').show(100);
jQuery(this).find('span').html('&#11014;');
},
function() {
jQuery(this).next('table.form-table').hide(100);
jQuery(this).find('span').html('&#11015;');
}
);
</script>
<?php
}
/**
* Get retailcrm api client
*
@ -963,41 +333,6 @@ if (!class_exists('WC_Retailcrm_Base')) {
return false;
}
/**
* Add button in admin
*/
function add_retailcrm_button() {
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() || !is_admin())
return;
$wp_admin_bar->add_menu(
array(
'id' => 'retailcrm_top_menu',
'title' => __('retailCRM', 'retailcrm')
)
);
$wp_admin_bar->add_menu(
array(
'id' => 'retailcrm_ajax_generate_icml',
'title' => __('Generating ICML catalog', 'retailcrm'),
'href' => '#',
'parent' => 'retailcrm_top_menu',
'class' => 'retailcrm_ajax_generate_icml'
)
);
$wp_admin_bar->add_menu(
array(
'id' => 'retailcrm_ajax_generate_setings',
'title' => __('Settings', 'retailcrm'),
'href'=> get_site_url().'/wp-admin/admin.php?page=wc-settings&tab=integration&section=integration-retailcrm',
'parent' => 'retailcrm_top_menu',
'class' => 'retailcrm_ajax_settings'
)
);
}
/**
* Deactivate module in marketplace retailCRM
*

View File

@ -16,20 +16,30 @@ if (!class_exists('WC_Retailcrm_Customers')) :
const CUSTOMER_ROLE = 'customer';
/** @var bool | WC_Retailcrm_Proxy */
protected $retailcrm;
protected $retailcrm_settings;
/** @var array */
protected $retailcrm_settings = array();
/** @var WC_Retailcrm_Customer_Address */
protected $customer_address;
/** @var array */
private $customer = array();
/**
* WC_Retailcrm_Customers constructor.
*
* @param $retailcrm
* @param bool | WC_Retailcrm_Proxy $retailcrm
* @param array $retailcrm_settings
* @param WC_Retailcrm_Customer_Address $customer_address
*/
public function __construct($retailcrm = false)
public function __construct($retailcrm = false, $retailcrm_settings, $customer_address)
{
$this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key);
$this->retailcrm = $retailcrm;
$this->retailcrm_settings = $retailcrm_settings;
$this->customer_address = $customer_address;
}
/**
@ -139,13 +149,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
'firstName' => $firstName ? $firstName : $customer->get_username(),
'lastName' => $customer->get_last_name(),
'email' => $customer->get_email(),
'address' => array(
'index' => $customer->get_billing_postcode(),
'countryIso' => $customer->get_billing_country(),
'region' => $customer->get_billing_state(),
'city' => $customer->get_billing_city(),
'text' => $customer->get_billing_address_1() . ',' . $customer->get_billing_address_2()
)
'address' => $this->customer_address->build($customer)->get_data()
);
if ($customer->get_id() > 0) {

View File

@ -20,6 +20,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
protected $retailcrm_settings;
protected $retailcrm;
protected $order_methods = array();
protected $bind_field = 'externalId';
/**
* WC_Retailcrm_History constructor.
@ -29,6 +30,12 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
{
$this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key);
if (isset($this->retailcrm_settings['bind_by_sku'])
&& $this->retailcrm_settings['bind_by_sku'] == WC_Retailcrm_Base::YES
) {
$this->bind_field = 'xmlId';
}
if (isset($this->retailcrm_settings['order_methods'])) {
$this->order_methods = $this->retailcrm_settings['order_methods'];
unset($this->retailcrm_settings['order_methods']);
@ -322,12 +329,16 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
if (array_key_exists('items', $order)) {
foreach ($order['items'] as $item) {
if (!isset($item['offer']['externalId'])) {
if (!isset($item['offer'][$this->bind_field])) {
continue;
}
if (isset($item['create']) && $item['create'] == true) {
$product = wc_get_product($item['offer']['externalId']);
$product = retailcrm_get_wc_product(
$item['offer'][$this->bind_field],
$this->retailcrm_settings
);
$wc_order->add_product($product, $item['quantity']);
} else {
foreach ($wc_order->get_items() as $order_item_id => $order_item) {
@ -337,7 +348,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$offer_id = $order_item['product_id'];
}
if ($offer_id == $item['offer']['externalId']) {
if ($offer_id == $item['offer'][$this->bind_field]) {
$this->deleteOrUpdateOrderItem($item, $order_item, $order_item_id);
}
}
@ -428,7 +439,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
} else {
if (isset($item['quantity']) && $item['quantity']) {
$order_item->set_quantity($item['quantity']);
$product = wc_get_product($item['offer']['externalId']);
$product = retailcrm_get_wc_product($item['offer'][$this->bind_field], $this->retailcrm_settings);
$order_item->set_total($product->get_price() * $item['quantity']);
$order_item->set_subtotal($product->get_price());
$data_store = $order_item->get_data_store();
@ -526,7 +537,10 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
if ($product_data) {
foreach ($product_data as $product) {
$wc_order->add_product(wc_get_product($product['offer']['externalId']), $product['quantity']);
$wc_order->add_product(
retailcrm_get_wc_product($product['offer'][$this->bind_field], $this->retailcrm_settings),
$product['quantity']
);
}
}

View File

@ -41,12 +41,16 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
protected $chunk = 500;
protected $fileLifeTime = 3600;
/** @var array */
protected $settings;
/**
* WC_Retailcrm_Icml constructor.
*
*/
public function __construct()
{
$this->settings = get_option(WC_Retailcrm_Base::$option_key);
$this->shop = get_bloginfo( 'name' );
$this->file = ABSPATH . 'retailcrm.xml';
$this->tmpFile = sprintf('%s.tmp', $this->file);
@ -355,7 +359,6 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
);
foreach ($products as $offer) {
if ($offer->get_type() == 'simple') {
$this->setOffer($full_product_list, $product_attributes, $offer);
} elseif ($offer->get_type() == 'variable') {
@ -424,8 +427,8 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
*
* @param array $full_product_list
* @param array $product_attributes
* @param object WC_Product_Simple | WC_Product_Variation $product
* @param mixed WC_Product_Variable | bool $parent default false
* @param WC_Product $product
* @param bool | WC_Product_Variable $parent
*
* @return void
*/
@ -462,10 +465,6 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
}
}
if ($product->get_sku() != '') {
$params[] = array('code' => 'article', 'name' => 'Артикул', 'value' => $product->get_sku());
}
$dimension = '';
if ($product->get_length() != '') {
@ -506,6 +505,14 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
'tax' => isset($tax) ? $tax['rate'] : 'none'
);
if ($product->get_sku() != '') {
$params[] = array('code' => 'article', 'name' => 'Артикул', 'value' => $product->get_sku());
if (isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] == WC_Retailcrm_Base::YES) {
$product_data['xmlId'] = $product->get_sku();
}
}
if (!empty($params)) {
$product_data['params'] = $params;
}
@ -523,11 +530,10 @@ if ( ! class_exists( 'WC_Retailcrm_Icml' ) ) :
* @return array
*/
private function checkPostStatuses() {
$options = get_option(WC_Retailcrm_Base::$option_key);
$status_args = array();
foreach (get_post_statuses() as $key => $value) {
if (isset($options['p_' . $key]) && $options['p_' . $key] == 'yes') {
if (isset($this->settings['p_' . $key]) && $this->settings['p_' . $key] == WC_Retailcrm_Base::YES) {
$status_args[] = $key;
}
}

View File

@ -14,9 +14,15 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
*/
class WC_Retailcrm_Inventories
{
/** @var WC_Retailcrm_Client_V5 */
protected $retailcrm;
/** @var array */
protected $retailcrm_settings;
/** @var string */
protected $bind_field = 'externalId';
/**
* WC_Retailcrm_Inventories constructor.
* @param bool $retailcrm
@ -25,6 +31,12 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
{
$this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key);
$this->retailcrm = $retailcrm;
if (isset($this->retailcrm_settings['bind_by_sky'])
&& $this->retailcrm_settings['bind_by_sky'] == WC_Retailcrm_Base::YES
) {
$this->bind_field = 'xmlId';
}
}
/**
@ -43,6 +55,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
$page = 1;
do {
/** @var WC_Retailcrm_Response $result */
$result = $this->retailcrm->storeInventories(array(), $page, 250);
if (!$result->isSuccessful()) {
@ -53,8 +66,8 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
$page++;
foreach ($result['offers'] as $offer) {
if (isset($offer['externalId'])) {
$product = wc_get_product($offer['externalId']);
if (isset($offer[$this->bind_field])) {
$product = retailcrm_get_wc_product($offer[$this->bind_field], $this->retailcrm_settings);
if ($product instanceof WC_Product) {
if ($product->get_type() == 'variable') {
@ -79,7 +92,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
*/
public function updateQuantity()
{
if ($this->retailcrm_settings['sync'] == 'yes') {
if ($this->retailcrm_settings['sync'] == WC_Retailcrm_Base::YES) {
return $this->load_stocks();
}

View File

@ -14,16 +14,49 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
*/
class WC_Retailcrm_Orders
{
protected $retailcrm_settings;
/** @var bool|WC_Retailcrm_Proxy */
protected $retailcrm;
/** @var array */
protected $retailcrm_settings;
/** @var WC_Retailcrm_Order_Item */
protected $order_item;
/** @var WC_Retailcrm_Order_Address */
protected $order_address;
/** @var WC_Retailcrm_Order_Payment */
protected $order_payment;
/** @var WC_Retailcrm_Customers */
protected $customers;
/** @var WC_Retailcrm_Order */
protected $orders;
/** @var array */
private $order = array();
/** @var array */
private $payment = array();
public function __construct($retailcrm = false)
{
$this->retailcrm_settings = get_option(WC_Retailcrm_Base::$option_key);
public function __construct(
$retailcrm = false,
$retailcrm_settings,
$order_item,
$order_address,
$customers,
$orders,
$order_payment
) {
$this->retailcrm = $retailcrm;
$this->retailcrm_settings = $retailcrm_settings;
$this->order_item = $order_item;
$this->order_address = $order_address;
$this->customers = $customers;
$this->orders = $orders;
$this->order_payment = $order_payment;
}
/**
@ -66,12 +99,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
}
if ($withCustomers === true && !empty($customers)) {
if (!class_exists('WC_Retailcrm_Customers')) {
include_once(WC_Retailcrm_Base::checkCustomFile('customers'));
}
$retailcrmCustomer = new WC_Retailcrm_Customers($this->retailcrm);
$retailcrmCustomer->customersUpload($customers);
$this->customers->customersUpload($customers);
}
$uploadOrders = array_chunk($orders_data, 50);
@ -101,26 +129,20 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
$this->processOrder($order);
$customer = $order->get_user();
if (!class_exists('WC_Retailcrm_Customers')) {
include_once(WC_Retailcrm_Base::checkCustomFile('customers'));
}
$retailcrm_customers = new WC_Retailcrm_Customers($this->retailcrm);
if ($customer != false) {
$search = $retailcrm_customers->searchCustomer(array('id' => $customer->get('ID')));
$search = $this->customers->searchCustomer(array('id' => $customer->get('ID')));
if (!$search) {
$retailcrm_customers->createCustomer($customer);
$this->customers->createCustomer($customer);
} else {
$this->order['customer']['externalId'] = $search['externalId'];
}
} else {
$search = $retailcrm_customers->searchCustomer(array('email' => $order->get_billing_email()));
$search = $this->customers->searchCustomer(array('email' => $order->get_billing_email()));
if (!$search) {
$new_customer = $retailcrm_customers->buildCustomerFromOrderData($order);
$id = $retailcrm_customers->createCustomer($new_customer);
$new_customer = $this->customers->buildCustomerFromOrderData($order);
$id = $this->customers->createCustomer($new_customer);
if ($id !== null) {
$this->order['customer']['id'] = $id;
@ -212,28 +234,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
return null;
}
/**
* Get order data
*
* @param WC_Order $order
*
* @return array $order_data_arr
*/
protected function getOrderData($order)
{
$order_data_arr = array();
$order_info = $order->get_data();
$order_data_arr['id'] = $order_info['id'];
$order_data_arr['payment_method'] = $order->get_payment_method();
$order_data_arr['date'] = $order_info['date_created']->date('Y-m-d H:i:s');
$order_data_arr['discount_total'] = $order_info['discount_total'];
$order_data_arr['discount_tax'] = $order_info['discount_tax'];
$order_data_arr['customer_comment'] = $order->get_customer_note();
return $order_data_arr;
}
/**
* process to combine order data
*
@ -248,21 +248,12 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
return;
}
$order_data_info = $this->getOrderData($order);
$order_data = array();
$order_data['externalId'] = $order_data_info['id'];
$order_data['number'] = $order->get_order_number();
$order_data['createdAt'] = trim($order_data_info['date']);
$order_data['customerComment'] = $order_data_info['customer_comment'];
if (!empty($order_data_info['payment_method'])
&& !empty($this->retailcrm_settings[$order_data_info['payment_method']])
&& $this->retailcrm_settings['api_version'] != 'v5'
) {
$order_data['paymentType'] = $this->retailcrm_settings[$order_data_info['payment_method']];
if ($update === true) {
$this->orders->is_new = false;
}
$order_data = $this->orders->build($order)->get_data();
if ($order->get_items('shipping')) {
$shippings = $order->get_items( 'shipping' );
$shipping = reset($shippings);
@ -300,95 +291,20 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
}
}
if ($this->retailcrm_settings['api_version'] != 'v5' && $order->is_paid()) {
$order_data['paymentStatus'] = 'paid';
}
$status = $order->get_status();
$order_data['status'] = $this->retailcrm_settings[$status];
$user_data_billing = $order->get_address('billing');
if (!empty($user_data_billing)) {
$order_data['phone'] = $user_data_billing['phone'];
$order_data['email'] = $user_data_billing['email'];
}
$user_data_shipping = $order->get_address('shipping');
if (!empty($user_data_shipping)) {
$order_data['firstName'] = $user_data_shipping['first_name'];
$order_data['lastName'] = $user_data_shipping['last_name'];
$order_data['delivery']['address']['index'] = $user_data_shipping['postcode'];
$order_data['delivery']['address']['city'] = $user_data_shipping['city'];
$order_data['delivery']['address']['region'] = $user_data_shipping['state'];
$order_data['countryIso'] = $user_data_shipping['country'];
}
$order_data['delivery']['address']['text'] = sprintf(
"%s %s %s %s %s",
$user_data_shipping['postcode'],
$user_data_shipping['state'],
$user_data_shipping['city'],
$user_data_shipping['address_1'],
$user_data_shipping['address_2']
);
$order_data['delivery']['address'] = $this->order_address->build($order)->get_data();
$order_items = array();
/** @var WC_Order_Item_Product $item */
foreach ($order->get_items() as $item) {
$uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ;
$price = round(($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity()), 2);
$product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
$product_tax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;
$price_item = $product_price + $product_tax;
$discount_price = $price - $price_item;
$order_item = array(
'offer' => array('externalId' => $uid),
'productName' => $item['name'],
'initialPrice' => (float)$price,
'quantity' => $item['qty'],
);
if ($this->retailcrm_settings['api_version'] == 'v5' && round($discount_price, 2)) {
$order_item['discountManualAmount'] = round($discount_price, 2);
} elseif ($this->retailcrm_settings['api_version'] == 'v4' && round($discount_price, 2)) {
$order_item['discount'] = round($discount_price, 2);
}
$order_items[] = $order_item;
$order_items[] = $this->order_item->build($item)->get_data();
$this->order_item->reset_data();
}
$order_data['items'] = $order_items;
if ($this->retailcrm_settings['api_version'] == 'v5') {
$payment = array(
'amount' => $order->get_total(),
'externalId' => $order->get_id() . uniqid('-')
);
$payment['order'] = array(
'externalId' => $order->get_id()
);
if (!empty($order_data_info['payment_method']) && !empty($this->retailcrm_settings[$order_data_info['payment_method']])) {
$payment['type'] = $this->retailcrm_settings[$order_data_info['payment_method']];
}
if ($order->is_paid()) {
$payment['status'] = 'paid';
}
if ($order->get_date_paid()) {
$pay_date = $order->get_date_paid();
$payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s'));
}
if (!$update) {
$order_data['payments'][] = $payment;
}
if ($this->retailcrm_settings['api_version'] == 'v5' && !$update) {
$this->order_payment->is_new = true;
$order_data['payments'][] = $this->order_payment->build($order)->get_data();
}
$this->order = apply_filters('retailcrm_process_order', $order_data, $order);
@ -399,39 +315,16 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
*
* @param WC_Order $order
* @param boolean $update
* @param mixed $externalId
*
* @return array $payment
*/
protected function sendPayment($order, $update = false, $externalId = false)
{
$payment = array(
'amount' => $order->get_total()
);
if ($update) {
$payment['externalId'] = $externalId;
} else {
$payment['externalId'] = $order->get_id() . uniqid('-');
}
$payment['order'] = array(
'externalId' => $order->get_id()
);
if ($order->is_paid()) {
$payment['status'] = 'paid';
}
if ($order->get_date_paid()) {
$pay_date = $order->get_date_paid();
$payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s'));
}
$this->order_payment->is_new = !$update;
$payment = $this->order_payment->build($order, $externalId)->get_data();
if ($update === false) {
if (isset($this->retailcrm_settings[$order->get_payment_method()])) {
$payment['type'] = $this->retailcrm_settings[$order->get_payment_method()];
}
$this->retailcrm->ordersPaymentCreate($payment);
} else {
$this->retailcrm->ordersPaymentEdit($payment);

View File

@ -0,0 +1,38 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
/**
* Class WC_Retailcrm_Customer_Address
*/
class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
{
protected $filter_name = 'customer_address';
/**
* @param WC_Customer $customer
*
* @return self
*/
public function build($customer)
{
$data = array(
'index' => $customer->get_billing_postcode(),
'countryIso' => $customer->get_billing_country(),
'region' => $customer->get_billing_state(),
'city' => $customer->get_billing_city(),
'text' => $customer->get_billing_address_1() . ', ' . $customer->get_billing_address_2()
);
$this->set_data_fields($data);
return $this;
}
}

View File

@ -82,3 +82,19 @@ function retailcrm_get_delivery_service($method_id, $instance_id) {
return false;
}
/**
* @param $id
* @param $settings
*
* @return false|WC_Product|null
*/
function retailcrm_get_wc_product($id, $settings) {
if (isset($settings['bind_by_sky'])
&& $settings['bind_by_sky'] == WC_Retailcrm_Base::YES
) {
$id = wc_get_product_id_by_sku($id);
}
return wc_get_product($id);
}

View File

@ -0,0 +1,46 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
{
protected $filter_name = 'order_address';
/**
* @param WC_Order $order
*
* @return self
*/
public function build($order)
{
$address = $order->get_address('shipping');
if (!empty($address)) {
$data = array(
'index' => $address['postcode'],
'city' => $address['city'],
'region' => $address['state']
);
$this->set_data_fields($data);
}
$this->set_data_field('text', sprintf(
"%s %s %s %s %s",
$address['postcode'],
$address['state'],
$address['city'],
$address['address_1'],
$address['address_2']
));
return $this;
}
}

View File

@ -0,0 +1,127 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
/**
* Class WC_Retailcrm_Order_Item
*/
class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
{
protected $filter_name = 'order_item';
/**
* @var array order item
*/
protected $data = array(
'offer' => array(),
'productName' => '',
'initialPrice' => 0.00,
'quantity' => 0
);
/**
* @var array
*/
protected $settings = array();
/**
* WC_Retailcrm_Order_Item constructor.
*
* @param array $settings
*/
public function __construct($settings)
{
$this->settings = $settings;
}
/**
* @param WC_Order_Item_Product $item
*
* @return self
*/
public function build($item)
{
$price = $this->calculate_price($item);
$discount_price = $this->calculate_discount($item, $price);
$data['productName'] = $item['name'];
$data['initialPrice'] = (float)$price;
$data['quantity'] = $item['qty'];
$this->set_data_fields($data);
$this->set_offer($item);
if ($this->settings['api_version'] == 'v5' && round($discount_price, 2)) {
$this->set_data_field('discountManualAmount',round($discount_price, 2));
} elseif ($this->settings['api_version'] == 'v4' && round($discount_price, 2)) {
$this->set_data_field('discount', round($discount_price, 2));
}
return $this;
}
/**
* @param WC_Order_Item_Product $item
*
* @return void
*/
private function set_offer(WC_Order_Item_Product $item)
{
$uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ;
$offer = array('externalId' => $uid);
if (isset($this->settings['bind_by_sku']) && $this->settings['bind_by_sku'] == WC_Retailcrm_Base::YES) {
$offer['xmlId'] = $item->get_product()->get_sku();
}
$this->set_data_field('offer', $offer);
}
/**
* @param WC_Order_Item_Product $item
*
* @return float
*/
private function calculate_price(WC_Order_Item_Product $item)
{
$price = ($item['line_subtotal'] / $item->get_quantity()) + ($item['line_subtotal_tax'] / $item->get_quantity());
return round($price, 2);
}
/**
* @param WC_Order_Item_Product $item
* @param $price
*
* @return float|int
*/
private function calculate_discount(WC_Order_Item_Product $item, $price)
{
$product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0;
$product_tax = $item->get_total_tax() ? $item->get_total_tax() / $item->get_quantity() : 0;
$price_item = $product_price + $product_tax;
$discount_price = $price - $price_item;
return $discount_price;
}
/**
* Reset data for object
*/
public function reset_data()
{
$this->data = array(
'offer' => array(),
'productName' => '',
'initialPrice' => 0.00,
'quantity' => 0
);
}
}

View File

@ -0,0 +1,98 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
/**
* Class WC_Retailcrm_Order_Payment
*/
class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data
{
/** @var string */
protected $filter_name = 'order_payment';
/** @var array */
protected $data = array(
'externalId' => '',
'amount' => 0.00,
'type' => '',
'order' => array()
);
/** @var bool */
public $is_new = true;
/**
* @var array
*/
protected $settings = array();
/**
* WC_Retailcrm_Order_Item constructor.
*
* @param array $settings
*/
public function __construct($settings)
{
$this->settings = $settings;
}
/**
* @param WC_Order $order
* @param mixed $externalId
*
* @return self
*/
public function build($order, $externalId = false)
{
$data = array(
'amount' => $order->get_total()
);
if (!$this->is_new) {
$data['externalId'] = $externalId;
} else {
$data['externalId'] = uniqid($order->get_id());
}
$data['order'] = array(
'externalId' => $order->get_id()
);
if ($order->is_paid()) {
$data['status'] = 'paid';
}
if ($order->get_date_paid()) {
$data['paidAt'] = $order->get_date_paid()->date('Y-m-d H:i:s');
}
if ($this->is_new) {
if (isset($this->settings[$order->get_payment_method()])) {
$data['type'] = $this->settings[$order->get_payment_method()];
}
}
$this->set_data_fields($data);
return $this;
}
public function reset_data()
{
$this->data = array(
'externalId' => '',
'amount' => 0.00,
'type' => '',
'status' => '',
'paidAt' => '',
'order' => array()
);
}
}

View File

@ -0,0 +1,125 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data
{
/** @var bool */
public $is_new = true;
protected $filter_name = 'order';
protected $data = array(
'externalId' => 0,
'status' => '',
'number' => '',
'createdAt' => '',
'firstName' => '',
'lastName' => '',
'email' => '',
'paymentType' => '',
'customerComment' => '',
'paymentStatus' => '',
'phone' => '',
'countryIso' => ''
);
/**
* @var array
*/
protected $settings = array();
/**
* WC_Retailcrm_Order constructor.
*
* @param array $settings
*/
public function __construct($settings)
{
$this->settings = $settings;
}
/**
* @param WC_Order $order
*
* @return self
*/
public function build($order)
{
$data = array(
'externalId' => $order->get_id(),
'createdAt' => $order->get_date_created()->date('Y-m-d H:i:s'),
'firstName' => $order->get_shipping_first_name(),
'lastName' => $order->get_shipping_last_name(),
'email' => $order->get_billing_email(),
'customerComment' => $order->get_customer_note(),
'phone' => $order->get_billing_phone(),
'countryIso' => $order->get_shipping_country()
);
$this->set_data_fields($data);
$this->set_number($order);
if ($this->settings['api_version'] != 'v5') {
$this->set_payment_data($order);
}
if (isset($this->settings[$order->get_status()])) {
$this->set_data_field('status', $this->settings[$order->get_status()]);
}
return $this;
}
/**
* @param WC_Order $order
*/
protected function set_payment_data($order)
{
if ($order->get_payment_method() && isset($this->settings[$order->get_payment_method()])) {
$this->set_data_field('paymentType', $this->settings[$order->get_payment_method()]);
}
if ($order->is_paid()) {
$this->set_data_field('paymentStatus', 'paid');
}
}
/**
* @param WC_Order $order
*/
protected function set_number($order)
{
if (isset($this->settings['update_number'])
&& $this->settings['update_number'] == WC_Retailcrm_Base::YES
&& !$this->is_new
) {
$this->set_data_field('number', $order->get_order_number());
}
}
public function reset_data()
{
$this->data = array(
'externalId' => '',
'status' => '',
'number' => '',
'createdAt' => '',
'firstName' => '',
'lastName' => '',
'email' => '',
'paymentType' => '',
'customerComment' => '',
'paymentStatus' => '',
'phone' => '',
'countryIso' => ''
);
}
}

View File

@ -39,6 +39,14 @@ if (!class_exists( 'WC_Integration_Retailcrm')) :
$this->load_plugin_textdomain();
if (class_exists( 'WC_Integration' ) ) {
require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-settings.php');
require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-data.php');
require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-address.php');
require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order.php');
require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-payment.php');
require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-item.php');
require_once(dirname(__FILE__ ) . '/include/order/class-wc-retailcrm-order-address.php');
require_once(dirname(__FILE__ ) . '/include/customer/class-wc-retailcrm-customer-address.php');
require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-base.php');
require_once(dirname(__FILE__ ) . '/include/functions.php');
add_filter('woocommerce_integrations', array( $this, 'add_integration'));

View File

@ -1,7 +1,15 @@
<?php
/**
* Class WC_Retailcrm_Test_Case_Helper
*/
class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case
{
/**
* @param string $apiVersion
*
* @return array
*/
protected function setOptions($apiVersion = 'v5')
{
$options = array(
@ -39,4 +47,12 @@ class WC_Retailcrm_Test_Case_Helper extends WC_Unit_Test_Case
return $options;
}
/**
* @return array
*/
protected function getOptions()
{
return get_option(WC_Retailcrm_Base::$option_key);
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $customer;
public function setUp()
{
parent::setUp();
$this->customer = WC_Helper_Customer::create_customer();
}
public function test_build()
{
$customer_address = new WC_Retailcrm_Customer_Address;
$data = $customer_address->build($this->customer)->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertArrayHasKey('countryIso', $data);
$this->assertNotEmpty($data['index']);
$this->assertNotEmpty($data['city']);
$this->assertNotEmpty($data['region']);
$this->assertNotEmpty($data['text']);
$this->assertNotEmpty($data['countryIso']);
}
}

View File

@ -0,0 +1,37 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
/**
* Class WC_Retailcrm_Order_Address_Test
*/
class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper
{
/** @var WC_Order */
protected $order;
public function setUp()
{
parent::setUp();
$this->order = WC_Helper_Order::create_order();
}
public function test_build()
{
$order_address = new WC_Retailcrm_Order_Address;
$data = $order_address->build($this->order)->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper
{
/** @var WC_Order */
protected $order;
public function setUp()
{
parent::setUp();
$this->order = WC_Helper_Order::create_order();
}
public function test_build()
{
$order_item = new WC_Retailcrm_Order_Item($this->getOptions());
/** @var WC_Order_Item_Product $item */
foreach ($this->order->get_items() as $item) {
$data = $order_item->build($item)->get_data();
$this->assertArrayHasKey('productName', $data);
$this->assertArrayHasKey('initialPrice', $data);
$this->assertArrayHasKey('quantity', $data);
$this->assertArrayHasKey('offer', $data);
}
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper
{
/** @var WC_Order */
protected $order;
public function setUp()
{
parent::setUp();
$this->order = WC_Helper_Order::create_order();
}
/**
* @param mixed $externalId
*
* @dataProvider dataProvider
*/
public function test_build($externalId)
{
$order_payment = new WC_Retailcrm_Order_Payment($this->getOptions());
$data = $order_payment->build($this->order, $externalId)->get_data();
$this->assertArrayHasKey('externalId', $data);
$this->assertArrayHasKey('type', $data);
$this->assertArrayHasKey('amount', $data);
$this->assertArrayHasKey('order', $data);
}
/**
* @return array
*/
public function dataProvider()
{
return array(
array(
'externalId' => false
),
array(
'externalId' => uniqid()
)
);
}
}

View File

@ -51,7 +51,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
public function test_wc_customer_get($retailcrm)
{
$wc_customer = new WC_Customer($this->customer->get_id());
$retailcrm_customer = new WC_Retailcrm_Customers($retailcrm);
$retailcrm_customer = $this->getRetailcrmCustomer($retailcrm);
$this->assertEquals($wc_customer, $retailcrm_customer->wcCustomerGet($this->customer->get_id()));
}
@ -61,7 +61,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
*/
public function test_customers_upload($retailcrm)
{
$retailcrm_customer = new WC_Retailcrm_Customers($retailcrm);
$retailcrm_customer = $this->getRetailcrmCustomer($retailcrm);
$data = $retailcrm_customer->customersUpload();
if ($retailcrm) {
@ -79,7 +79,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
*/
public function test_create_customer($retailcrm)
{
$retailcrm_customer = new WC_Retailcrm_Customers($retailcrm);
$retailcrm_customer = $this->getRetailcrmCustomer($retailcrm);
$id = $retailcrm_customer->createCustomer($this->customer->get_id());
$customer_send = $retailcrm_customer->getCustomer();
@ -102,7 +102,7 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
*/
public function test_update_customer($retailcrm)
{
$retailcrm_customer = new WC_Retailcrm_Customers($retailcrm);
$retailcrm_customer = $this->getRetailcrmCustomer($retailcrm);
$customer = $retailcrm_customer->updateCustomer($this->customer->get_id());
$customer_send = $retailcrm_customer->getCustomer();
@ -134,4 +134,18 @@ class WC_Retailcrm_Customers_Test extends WC_Retailcrm_Test_Case_Helper
)
);
}
/**
* @param $retailcrm
*
* @return WC_Retailcrm_Customers
*/
private function getRetailcrmCustomer($retailcrm)
{
return new WC_Retailcrm_Customers(
$retailcrm,
$this->getOptions(),
new WC_Retailcrm_Customer_Address()
);
}
}

View File

@ -34,7 +34,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
public function test_order_upload($retailcrm, $apiVersion)
{
$this->options = $this->setOptions($apiVersion);
$retailcrm_orders = new WC_Retailcrm_Orders($retailcrm);
$retailcrm_orders = $this->getRetailcrmOrders($retailcrm);
$upload_orders = $retailcrm_orders->ordersUpload();
if ($retailcrm) {
@ -83,7 +83,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
$this->createTestOrder();
$this->options = $this->setOptions($apiVersion);
$retailcrm_orders = new WC_Retailcrm_Orders($retailcrm);
$retailcrm_orders = $this->getRetailcrmOrders($retailcrm);
$order = $retailcrm_orders->orderCreate($this->order->get_id());
$order_send = $retailcrm_orders->getOrder();
@ -164,7 +164,7 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
->willReturn($responseMock);
}
$retailcrm_orders = new WC_Retailcrm_Orders($retailcrm);
$retailcrm_orders = $this->getRetailcrmOrders($retailcrm);
$order = $retailcrm_orders->updateOrder($this->order->get_id());
$order_send = $retailcrm_orders->getOrder();
@ -312,4 +312,24 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper
)
);
}
/**
* @param $retailcrm
*
* @return WC_Retailcrm_Orders
*/
private function getRetailcrmOrders($retailcrm)
{
return new WC_Retailcrm_Orders(
$retailcrm,
$this->getOptions(),
new WC_Retailcrm_Order_Item($this->getOptions()),
new WC_Retailcrm_Order_Address,
new WC_Retailcrm_Customers(
$retailcrm, $this->getOptions(), new WC_Retailcrm_Customer_Address
),
new WC_Retailcrm_Order($this->getOptions()),
new WC_Retailcrm_Order_Payment($this->getOptions())
);
}
}