From 1e114cd22ee1d31ebe973c9a85e54cfe63eb9a3e Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 7 Aug 2017 12:52:38 +0200 Subject: [PATCH] Edit order create hook, added check correct data in base class (#6) --- .../include/api/class-wc-retailcrm-proxy.php | 31 ++-- retailcrm/include/class-wc-retailcrm-base.php | 150 ++++++++++-------- retailcrm/retailcrm.php | 2 +- 3 files changed, 97 insertions(+), 86 deletions(-) diff --git a/retailcrm/include/api/class-wc-retailcrm-proxy.php b/retailcrm/include/api/class-wc-retailcrm-proxy.php index d871666..569929b 100644 --- a/retailcrm/include/api/class-wc-retailcrm-proxy.php +++ b/retailcrm/include/api/class-wc-retailcrm-proxy.php @@ -49,24 +49,25 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : public function __call($method, $arguments) { - try { - $response = call_user_func_array(array($this->retailcrm, $method), $arguments); - - if ($response->isSuccessful()) { - $result = ' Ok'; - } else { - $result = sprintf( - $method ." : Error: [HTTP-code %s] %s", - $response->getStatusCode(), - $response->getErrorMsg() - ); + if (!isset($this->retailcrm)) return; + try { + $response = call_user_func_array(array($this->retailcrm, $method), $arguments); + + if ($response->isSuccessful()) { + $result = ' Ok'; + } else { + $result = sprintf( + $method ." : Error: [HTTP-code %s] %s", + $response->getStatusCode(), + $response->getErrorMsg() + ); - if (isset($response['errors'])) { - foreach ($response['errors'] as $error) { - $result .= " $error"; + if (isset($response['errors'])) { + foreach ($response['errors'] as $error) { + $result .= " $error"; + } } } - } $this->logger->add('retailcrm', sprintf("[%s] %s", $method, $result)); } catch (WC_Retailcrm_Exception_Curl $exception) { diff --git a/retailcrm/include/class-wc-retailcrm-base.php b/retailcrm/include/class-wc-retailcrm-base.php index e8adae0..f4edd6c 100644 --- a/retailcrm/include/class-wc-retailcrm-base.php +++ b/retailcrm/include/class-wc-retailcrm-base.php @@ -94,74 +94,76 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : /** * Shipping options */ - $shipping_option_list = array(); $retailcrm_shipping_list = $retailcrm->deliveryTypesList(); - foreach ($retailcrm_shipping_list['deliveryTypes'] as $retailcrm_shipping_type) { - $shipping_option_list[$retailcrm_shipping_type['code']] = $retailcrm_shipping_type['name']; - } + 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 = new WC_Shipping(); - $wc_shipping_list = $wc_shipping->get_shipping_methods(); + $wc_shipping = new WC_Shipping(); + $wc_shipping_list = $wc_shipping->get_shipping_methods(); - $this->form_fields[] = array( - 'title' => __( 'Способы доставки', 'woocommerce' ), - 'type' => 'title', - 'description' => '', - 'id' => 'shipping_options' - ); + $this->form_fields[] = array( + 'title' => __( 'Способы доставки', 'woocommerce' ), + 'type' => 'title', + 'description' => '', + 'id' => 'shipping_options' + ); - foreach ( $wc_shipping_list as $shipping ) { - if ( isset( $shipping->enabled ) && $shipping->enabled == 'yes' ) { - $key = $shipping->id; - $name = $key; - $this->form_fields[$name] = array( - 'title' => __( $shipping->method_title, 'textdomain' ), - 'description' => __( $shipping->method_description, 'textdomain' ), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $shipping_option_list, - 'desc_tip' => true, - ); + foreach ( $wc_shipping_list as $shipping ) { + if ( isset( $shipping->enabled ) && $shipping->enabled == 'yes' ) { + $key = $shipping->id; + $name = $key; + $this->form_fields[$name] = array( + 'title' => __( $shipping->method_title, 'textdomain' ), + 'description' => __( $shipping->method_description, 'textdomain' ), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $shipping_option_list, + 'desc_tip' => true, + ); + } } } /** * Payment options */ - $payment_option_list = array(); $retailcrm_payment_list = $retailcrm->paymentTypesList(); - foreach ($retailcrm_payment_list['paymentTypes'] as $retailcrm_payment_type) { - $payment_option_list[$retailcrm_payment_type['code']] = $retailcrm_payment_type['name']; - } + 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 = new WC_Payment_Gateways(); - $wc_payment_list = $wc_payment->get_available_payment_gateways(); + $wc_payment = new WC_Payment_Gateways(); + $wc_payment_list = $wc_payment->get_available_payment_gateways(); - $this->form_fields[] = array( - 'title' => __( 'Способы оплаты', 'woocommerce' ), - 'type' => 'title', - 'description' => '', - 'id' => 'payment_options' - ); + $this->form_fields[] = array( + 'title' => __( 'Способы оплаты', 'woocommerce' ), + 'type' => 'title', + 'description' => '', + 'id' => 'payment_options' + ); - foreach ( $wc_payment_list as $payment ) { - if ( isset( $payment->enabled ) && $payment->enabled == 'yes' ) { - $key = $payment->id; - $name = $key; - $this->form_fields[$name] = array( - 'title' => __( $payment->method_title, 'textdomain' ), - 'description' => __( $payment->method_description, 'textdomain' ), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $payment_option_list, - 'desc_tip' => true, - ); + foreach ( $wc_payment_list as $payment ) { + if ( isset( $payment->enabled ) && $payment->enabled == 'yes' ) { + $key = $payment->id; + $name = $key; + $this->form_fields[$name] = array( + 'title' => __( $payment->method_title, 'textdomain' ), + 'description' => __( $payment->method_description, 'textdomain' ), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $payment_option_list, + 'desc_tip' => true, + ); + } } } @@ -171,31 +173,36 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : $statuses_option_list = array(); $retailcrm_statuses_list = $retailcrm->statusesList(); - foreach ($retailcrm_statuses_list['statuses'] as $retailcrm_status) { - $statuses_option_list[$retailcrm_status['code']] = $retailcrm_status['name']; - } + 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(); + $wc_statuses = wc_get_order_statuses(); - $this->form_fields[] = array( - 'title' => __( 'Статусы', 'woocommerce' ), - 'type' => 'title', - 'description' => '', - 'id' => 'statuses_options' - ); - - foreach ( $wc_statuses as $idx => $name ) { - $uid = str_replace('wc-', '', $idx); - $this->form_fields[$uid] = array( - 'title' => __( $name, 'textdomain' ), - 'css' => 'min-width:350px;', - 'class' => 'select', - 'type' => 'select', - 'options' => $statuses_option_list, - 'desc_tip' => true, + $this->form_fields[] = array( + 'title' => __( 'Статусы', 'woocommerce' ), + 'type' => 'title', + 'description' => '', + 'id' => 'statuses_options' ); + + foreach ( $wc_statuses as $idx => $name ) { + $uid = str_replace('wc-', '', $idx); + $this->form_fields[$uid] = array( + 'title' => __( $name, 'textdomain' ), + 'css' => 'min-width:350px;', + 'class' => 'select', + 'type' => 'select', + 'options' => $statuses_option_list, + 'desc_tip' => true, + ); + } } + /** + * Inventories options + */ $this->form_fields[] = array( 'title' => __( 'Настройки остатков', 'woocommerce' ), 'type' => 'title', @@ -211,6 +218,9 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : 'description' => 'Отметьте данный пункт, если хотите выгружать остатки товаров из CRM в магазин.' ); + /** + * Uploads options + */ $options = array_filter(get_option( 'woocommerce_integration-retailcrm_settings' )); if (!isset($options['uploads'])) { diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 564cff6..ed37f22 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -335,7 +335,7 @@ register_deactivation_hook( __FILE__, 'retailcrm_deactivation' ); if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option( 'active_plugins')))) { load_plugin_textdomain('wc_retailcrm', false, dirname(plugin_basename( __FILE__ )) . '/'); add_filter('cron_schedules', 'filter_cron_schedules', 10, 1); - add_action('woocommerce_thankyou', 'retailcrm_process_order', 10, 1); + add_action('woocommerce_checkout_order_processed', 'retailcrm_process_order', 10, 1); add_action('retailcrm_history', 'retailcrm_history_get'); add_action('retailcrm_icml', 'generate_icml'); add_action('retailcrm_inventories', 'load_stocks');