diff --git a/retailcrm/include/class-wc-retailcrm-customers.php b/retailcrm/include/class-wc-retailcrm-customers.php index 98ad01c..de1289f 100644 --- a/retailcrm/include/class-wc-retailcrm-customers.php +++ b/retailcrm/include/class-wc-retailcrm-customers.php @@ -69,50 +69,69 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : } } + /** + * crearte crm customer + * + * @param int $customer_id + * + * @return void + */ public function createCustomer($customer_id) { - $customer = new WC_Customer($customer_id); + if (get_userdata($customer_id)->roles[0] == 'customer'){ - if ($customer->get_role() == 'customer'){ + $data_customer = $this->processCustomer($customer_id); - $data_customer = $this->processCustomer($customer); - - $this->retailcrm->customersCreate($data_customer); + $res = $this->retailcrm->customersCreate($data_customer); } } + /** + * update crm customer + * + * @param int $customer_id + * + * @return void + */ public function updateCustomer($customer_id) { - $customer = new WC_Customer($customer_id); + if (get_userdata($customer_id)->roles[0] == 'customer'){ - if ($customer->get_role() == 'customer'){ + $data_customer = $this->processCustomer($customer_id); - $data_customer = $this->processCustomer($customer); - - $this->retailcrm->customersEdit($data_customer); + $res = $this->retailcrm->customersEdit($data_customer); } } - protected function processCustomer($customer) + /** + * get customer data + * + * @param int $customer_id + * + * @return void + */ + protected function processCustomer($customer_id) { - $createdAt = $customer->get_date_created(); + $customer_data = get_userdata( $customer_id ); + $customer_meta = get_user_meta( $customer_id ); + $data_customer = array( - 'createdAt' => $createdAt->date('Y-m-d H:i:s '), - 'externalId' => $customer_id, - 'firstName' => !empty($customer->get_first_name()) ? $customer->get_first_name() : $customer->get_username(), - 'lastName' => $customer->get_last_name(), - 'email' => $customer->get_email(), + 'createdAt' => $customer_data->user_registered, + 'externalId' => $customer_data->ID, + 'firstName' => !empty($customer_meta['first_name'][0]) ? $customer_meta['first_name'][0] : $customer_meta['billing_first_name'][0], + 'lastName' => $customer_meta['last_name'][0], + 'email' => $customer_data->user_email, 'phones' => array( array( - 'number' => $customer->get_billing_phone() + 'number' => $customer_meta['billing_phone'][0] ) ), '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() + 'index' => $customer_meta['billing_postcode'][0], + 'countryIso' => $customer_meta['billing_country'][0], + 'region' => $customer_meta['billing_state'][0], + 'city' => $customer_meta['billing_city'][0], + 'text' => $customer_meta['billing_address_1'][0] . ',' . $customer_meta['billing_address_2'][0] ) ); @@ -120,3 +139,4 @@ if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) : } } endif; +1 \ No newline at end of file diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index 2d17995..7b51339 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -225,6 +225,13 @@ function retailcrm_history_get() $history_class->getHistory(); } +/** + * create custormer + * + * @param int $customer_id + * + * @return void + */ function create_customer($customer_id) { if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) { include_once( __DIR__ . '/include/class-wc-retailcrm-customers.php' ); @@ -234,7 +241,14 @@ function create_customer($customer_id) { $customer_class->createCustomer($customer_id); } -function update_customer($customer_id, $data) { +/** + * update custormer + * + * @param int $customer_id + * + * @return void + */ +function update_customer($customer_id) { if ( ! class_exists( 'WC_Retailcrm_Customers' ) ) { include_once( __DIR__ . '/include/class-wc-retailcrm-customers.php' ); } @@ -321,6 +335,13 @@ function ajax_upload() { updateOrder($order_id); } +/** + * create order + * + * @param int $order_id + * + * @return void + */ +function create_order($order_id) { + if ( get_post_type($order_id) == 'shop_order' && get_post_status( $order_id ) != 'auto-draft' ) { + if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) { + include_once( __DIR__ . check_custom_order() ); + } + + $order_class = new WC_Retailcrm_Orders(); + $order_class->orderCreate($order_id); + } +} + register_activation_hook( __FILE__, 'retailcrm_install' ); 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_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'); - add_action( 'init', 'check_inventories'); + add_action( 'init', 'register_icml_generation'); + add_action('retailcrm_icml', 'generate_icml'); + + add_action( 'init', 'check_inventories'); + add_action('retailcrm_inventories', 'load_stocks'); + add_action( 'init', 'register_retailcrm_history'); + add_action('retailcrm_history', 'retailcrm_history_get'); + add_action( 'wp_ajax_do_upload', 'upload_to_crm' ); add_action('admin_print_footer_scripts', 'ajax_upload', 99); + + // create customer frontend area add_action( 'woocommerce_created_customer', 'create_customer', 10, 1 ); - add_action( 'woocommerce_checkout_update_user_meta', 10, 2 ); + // update customer frontend area + add_action( 'woocommerce_customer_save_address', 'update_customer' ); + // update customer admin area + add_action( 'profile_update', 'update_customer' ); + + // add_action( 'woocommerce_checkout_update_user_meta', 10, 2 ); if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { + add_action('woocommerce_checkout_order_processed', 'retailcrm_process_order', 10, 1); add_action('woocommerce_order_status_changed', 'retailcrm_update_order_status', 11, 1); add_action('woocommerce_saved_order_items', 'retailcrm_update_order_items', 10, 2); add_action('update_post_meta', 'retailcrm_update_order', 11, 4); add_action('woocommerce_payment_complete', 'retailcrm_update_order_payment', 11, 1); } else { add_action('woocommerce_update_order', 'update_order', 11, 1); + add_action('wp_insert_post', 'create_order' ); } }