From a0d681509d74231f9790105840ab006b615d2fbc Mon Sep 17 00:00:00 2001 From: Uryvskiy Dima Date: Thu, 7 Dec 2023 12:54:40 +0300 Subject: [PATCH] Added support WooCommerce 8.2 (HPOS) (#319) --- CHANGELOG.md | 3 + VERSION | 2 +- .../class-wc-retailcrm-abstracts-settings.php | 7 +- .../api/class-wc-retailcrm-response.php | 50 +++-- src/include/class-wc-retailcrm-base.php | 6 +- src/include/class-wc-retailcrm-history.php | 3 +- src/include/class-wc-retailcrm-orders.php | 65 ++++-- src/include/class-wc-retailcrm-uploader.php | 77 ++++--- src/include/functions.php | 11 + src/readme.txt | 7 +- src/retailcrm.php | 12 +- src/uninstall.php | 25 ++- .../class-wc-retailcrm-test-case-helper.php | 17 +- tests/test-wc-retailcrm-history.php | 210 +++--------------- tests/test-wc-retailcrm-orders.php | 22 +- tests/test-wc-retailcrm-uploader.php | 3 +- 16 files changed, 219 insertions(+), 301 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6807b85..de357dc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2023-12-07 4.7.0 +* Added support WooCommerce 8.2 (HPOS) + ## 2023-11-20 4.6.14 * Fix module activation/deactivation diff --git a/VERSION b/VERSION index 8af4c0d3..1163055e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.6.14 \ No newline at end of file +4.7.0 \ No newline at end of file diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php index cadf1eff..3c0c4465 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-settings.php @@ -144,11 +144,10 @@ public function init_form_fields() ), ]; - foreach (get_post_statuses() as $status_key => $status_value) { - $this->form_fields['p_' . $status_key] = [ - 'title' => $status_value, + foreach (get_post_statuses() as $statusKey => $statusValue) { + $this->form_fields['p_' . $statusKey] = [ + 'title' => $statusValue, 'label' => ' ', - 'description' => '', 'class' => 'checkbox', 'type' => 'checkbox', 'desc_tip' => true, diff --git a/src/include/api/class-wc-retailcrm-response.php b/src/include/api/class-wc-retailcrm-response.php index 48e4c9a8..96ea4792 100644 --- a/src/include/api/class-wc-retailcrm-response.php +++ b/src/include/api/class-wc-retailcrm-response.php @@ -170,30 +170,32 @@ public function offsetGet($offset) return $this->response[$offset]; } - /** - * Returns error string. If there's multiple errors present - they will be squashed into single string. - * - * @return string - */ - public function getErrorString() - { - if ($this->offsetExists('error')) { - return (string) $this->response['error']; - } elseif ($this->offsetExists('errors') && is_array($this->response['errors'])) { - $errorMessage = ''; - - foreach ($this->response['errors'] as $error) { - $errorMessage .= $error . ' >'; - } - - if (strlen($errorMessage) > 2) { - return (string) substr($errorMessage, 0, strlen($errorMessage) - 2); - } - - return $errorMessage; - } - - return ''; + /** + * Returns error string. If there's multiple errors present - they will be squashed into single string. + * + * @return string + */ + public function getErrorString() + { + if ($this->offsetExists('errorMsg')) { + return (string) $this->response['errorMsg']; + } + + if (is_array($this->response['errors']) && $this->offsetExists('errors')) { + $errorMessage = ''; + + foreach ($this->response['errors'] as $error) { + $errorMessage .= $error . ' >'; + } + + if (strlen($errorMessage) > 2) { + return (string) substr($errorMessage, 0, strlen($errorMessage) - 2); + } + + return $errorMessage; + } + + return ''; } /** diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 9f34b3c9..254e5c03 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -821,7 +821,11 @@ private function getMetaData($entity) { global $wpdb; - $table = $entity === 'order' ? $wpdb->postmeta : $wpdb->usermeta; + if ('user' === $entity) { + $table = $wpdb->usermeta; + } else { + $table = useHpos() ? $wpdb->prefix . 'wc_orders_meta' : $wpdb->postmeta; + } $metaData = ['default_retailcrm' => __('Select value', 'retailcrm')]; $sqlQuery = "SELECT DISTINCT `meta_key` FROM $table ORDER BY `meta_key`"; diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 5cd89c6e..b6b84718 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -1354,7 +1354,8 @@ private function updateMetaData($customFields, $crmData, $wcObject) } if ($wcObject instanceof WC_Order) { - update_post_meta($wcObject->get_id(), $metaKey, $crmData['customFields'][$customKey]); + $wcObject->update_meta_data($metaKey, $crmData['customFields'][$customKey]); + $wcObject->save_meta_data(); } else { update_user_meta($wcObject->get_id(), $metaKey, $crmData['customFields'][$customKey]); } diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index d7261d3c..7786d965 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -72,34 +72,40 @@ public function __construct( /** * Create order. Returns wc_get_order data or error string. * - * @param $order_id + * @param $orderId * - * @return bool|WC_Order|WC_Order_Refund|string + * @return bool|WC_Order|string * @throws \Exception */ - public function orderCreate($order_id) + public function orderCreate($orderId) { if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } - $this->order_payment->resetData(); + try { + $this->order_payment->resetData(); - $wcOrder = wc_get_order($order_id); - $this->processOrder($wcOrder); + $wcOrder = wc_get_order($orderId); - try { - $response = $this->retailcrm->ordersCreate($this->order); + $this->processOrder($wcOrder); - if ($response instanceof WC_Retailcrm_Proxy) { - if ($response->isSuccessful()) { - return $wcOrder; - } + $response = $this->retailcrm->ordersCreate($this->order); + if (!$response instanceof WC_Retailcrm_Response || !$response->isSuccessful()) { return $response->getErrorString(); } - } catch (InvalidArgumentException $exception) { - return $exception->getMessage(); + } catch (Throwable $exception) { + writeBaseLogs( + sprintf( + 'Error message: %s, file: %s on line: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine() + ) + ); + + return null; } return $wcOrder; @@ -252,25 +258,38 @@ protected function fillOrderCreate($wcCustomerId, $wcCustomerEmail, $wcOrder) /** * Edit order in CRM * - * @param int $order_id + * @param int $orderId * * @return WC_Order $order | null * @throws \Exception */ - public function updateOrder($order_id) + public function updateOrder($orderId) { if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } - $wcOrder = wc_get_order($order_id); + try { + $wcOrder = wc_get_order($orderId); - $this->processOrder($wcOrder, true); + $this->processOrder($wcOrder, true); - $response = $this->retailcrm->ordersEdit($this->order); + $response = $this->retailcrm->ordersEdit($this->order); - if (!empty($response) && $response->isSuccessful()) { - $this->payment = $this->orderUpdatePaymentType($wcOrder); + if ($response instanceof WC_Retailcrm_Response && $response->isSuccessful()) { + $this->payment = $this->orderUpdatePaymentType($wcOrder); + } + } catch (Throwable $exception) { + writeBaseLogs( + sprintf( + 'Error message: %s, file: %s on line: %s', + $exception->getMessage(), + $exception->getFile(), + $exception->getLine() + ) + ); + + return null; } return $wcOrder; @@ -337,11 +356,11 @@ protected function processOrder($order, $update = false) return; } - if ($order->get_status() == 'auto-draft') { + if ('auto-draft' === $order->get_status()) { return; } - if ($update === true) { + if ($update) { $this->orders->is_new = false; } diff --git a/src/include/class-wc-retailcrm-uploader.php b/src/include/class-wc-retailcrm-uploader.php index b4822be9..2c9241ad 100644 --- a/src/include/class-wc-retailcrm-uploader.php +++ b/src/include/class-wc-retailcrm-uploader.php @@ -59,11 +59,13 @@ public function __construct($retailcrm, $orders, $customers) */ public function uploadSelectedOrders() { - if (!empty($_GET['order_ids_retailcrm'])) { - $ids = array_unique(explode(',', $_GET['order_ids_retailcrm'])); - - if (!empty($ids)) { - $this->uploadArchiveOrders(0, $ids); + $ids = $_GET['order_ids_retailcrm']; + + if (!empty($ids)) { + preg_match_all('/\d+/', $ids, $matches); + + if (!empty($matches[0])) { + $this->uploadArchiveOrders(null, $matches[0]); } } } @@ -71,29 +73,31 @@ public function uploadSelectedOrders() /** * Uploads archive order in CRM * - * @param int $page Number page uploads. - * @param array $ids Ids orders upload. + * @param null|int $page Number page uploads. + * @param array $ids Ids orders upload. * * @return void|null * @throws Exception Invalid argument exception. */ - public function uploadArchiveOrders($page, $ids = array()) + public function uploadArchiveOrders($page, $ids = []) { if (!$this->retailcrm instanceof WC_Retailcrm_Proxy) { return null; } - $uploadErrors = array(); - $ordersCms = $this->getCmsOrders($page, $ids); + $orderIds = []; + $uploadErrors = []; - foreach ($ordersCms as $dataOrder) { - $orderId = $dataOrder->ID; + if (null !== $page) { + $orderIds = $this->getCmsOrders($page); + } elseif ([] !== $ids) { + $orderIds = $ids; + } + + foreach ($orderIds as $orderId) { $errorMessage = $this->orders->orderCreate($orderId); - if (true === is_string($errorMessage)) { - $errorMessage = empty($errorMessage) === true - ? 'Order exist. External id: ' . $orderId - : $errorMessage; + if (is_string($errorMessage)) { $uploadErrors[$orderId] = $errorMessage; } } @@ -118,7 +122,7 @@ public function uploadArchiveCustomers($page) $users = $this->getCmsUsers($page); if (false === empty($users)) { - $dataCustomers = array(); + $dataCustomers = []; foreach ($users as $user) { if ($this->customers->isCustomer($user) === false) { @@ -140,20 +144,19 @@ public function uploadArchiveCustomers($page) * Return orders ids * * @param integer $page Number page uploads. - * @param array $ids Ids orders upload. * * @return mixed */ - private function getCmsOrders($page, $ids = array()) + private function getCmsOrders($page) { - return get_posts( - array( - 'numberposts' => self::RETAILCRM_COUNT_OBJECT_UPLOAD, - 'offset' => self::RETAILCRM_COUNT_OBJECT_UPLOAD * $page, - 'post_type' => wc_get_order_types('view-orders'), - 'post_status' => array_keys(wc_get_order_statuses()), - 'include' => $ids, - ) + return wc_get_orders( + [ + 'type' => wc_get_order_types('view-orders'), + 'limit' => self::RETAILCRM_COUNT_OBJECT_UPLOAD, + 'status' => array_keys(wc_get_order_statuses()), + 'offset' => self::RETAILCRM_COUNT_OBJECT_UPLOAD * $page, + 'return' => 'ids', + ] ); } @@ -166,12 +169,16 @@ public function getCountOrders() { global $wpdb; - $result = $wpdb->get_results("SELECT COUNT(ID) as `count` FROM $wpdb->posts WHERE post_type = 'shop_order'"); + if (useHpos()) { + // Use {$wpdb->prefix}, because wp_wc_orders not standard WP table + $result = $wpdb->get_results("SELECT COUNT(ID) as `count` FROM {$wpdb->prefix}wc_orders"); + } else { + $result = $wpdb->get_results("SELECT COUNT(ID) as `count` FROM $wpdb->posts WHERE post_type = 'shop_order'"); + } - return empty($result[0]->count) === false ? (int) $result[0]->count : 0; + return $result[0]->count ? (int) $result[0]->count : 0; } - /** * Return users ids * @@ -179,17 +186,16 @@ public function getCountOrders() * * @return mixed */ - private function getCmsUsers($page) + private function getCmsUsers(int $page) { return get_users( - array( + [ 'number' => self::RETAILCRM_COUNT_OBJECT_UPLOAD, 'offset' => self::RETAILCRM_COUNT_OBJECT_UPLOAD * $page, - ) + ] ); } - /** * Return count users * @@ -199,10 +205,9 @@ public function getCountUsers() { $userCount = count_users(); - return empty($userCount['total_users']) === false ? $userCount['total_users'] : 0; + return $userCount['total_users'] ? (int) $userCount['total_users'] : 0; } - /** * Array keys must be orders ID's in WooCommerce, values must be strings (error messages). * diff --git a/src/include/functions.php b/src/include/functions.php index b04493f0..4f82f5bf 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -191,3 +191,14 @@ function writeBaseLogs($message) { WC_Retailcrm_Logger::addCaller(__METHOD__, $message); } + +/** + * Checking the use of HPOS. + * + * @codeCoverageIgnore + */ +function useHpos() +{ + return class_exists(Automattic\WooCommerce\Utilities\OrderUtil::class) + && Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); +} diff --git a/src/readme.txt b/src/readme.txt index eb9d7f38..1383d050 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -4,8 +4,8 @@ Donate link: https://www.simla.com Tags: Интеграция, Simla.com, simla Requires PHP: 7.0 Requires at least: 5.3 -Tested up to: 6.2 -Stable tag: 4.6.14 +Tested up to: 6.4 +Stable tag: 4.7.0 License: GPLv1 or later License URI: http://www.gnu.org/licenses/gpl-1.0.html @@ -82,6 +82,9 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i == Changelog == += 4.7.0 = +* Added support WooCommerce 8.2 (HPOS) + = 4.6.14 = * Fix module activation/deactivation diff --git a/src/retailcrm.php b/src/retailcrm.php index 54edbbbb..a18b7c69 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -5,10 +5,10 @@ * Description: Integration plugin for WooCommerce & Simla.com * Author: RetailDriver LLC * Author URI: http://retailcrm.pro/ - * Version: 4.6.14 - * Tested up to: 6.2 + * Version: 4.7.0 + * Tested up to: 6.4 * WC requires at least: 5.4 - * WC tested up to: 7.8 + * WC tested up to: 8.3 * Text Domain: retailcrm */ @@ -211,4 +211,10 @@ private function generatePluginInstallationUrl($pluginSlug) $plugin->register_deactivation_hook(); add_action('plugins_loaded', ['WC_Integration_Retailcrm', 'get_instance'], 0); + + add_action('before_woocommerce_init', function() { + if (class_exists( Automattic\WooCommerce\Utilities\FeaturesUtil::class)) { + Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true); + } + }); endif; diff --git a/src/uninstall.php b/src/uninstall.php index b1b99f86..372ffb5f 100644 --- a/src/uninstall.php +++ b/src/uninstall.php @@ -16,14 +16,14 @@ * * @link https://wordpress.org/plugins/woo-retailcrm/ * - * @version 4.6.14 + * @version 4.7.0 * * @package RetailCRM */ // @codeCoverageIgnoreStart if (!defined('ABSPATH')) { - exit; // Exit if accessed directly + exit; } // If uninstall not called from WordPress, then exit. @@ -31,18 +31,23 @@ exit; } -global $wpdb; - wp_clear_scheduled_hook('retailcrm_icml'); wp_clear_scheduled_hook('retailcrm_history'); wp_clear_scheduled_hook('retailcrm_inventories'); -// Delete options. -$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'woocommerce_integration-retailcrm_settings';"); -$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_customers_history_since_id';"); -$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_orders_history_since_id';"); -$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_active_in_crm';"); -$wpdb->query("DELETE FROM $wpdb->options WHERE option_name = 'retailcrm_client_id';"); +global $wpdb; + +$options = [ + 'retailcrm_client_id', + 'retailcrm_active_in_crm', + 'retailcrm_orders_history_since_id', + 'retailcrm_customers_history_since_id', + 'woocommerce_integration-retailcrm_settings', +]; + +foreach ($options as $option) { + $wpdb->query("DELETE FROM $wpdb->options WHERE option_name = {$option}"); +} // Clear any cached data that has been removed wp_cache_flush(); diff --git a/tests/helpers/class-wc-retailcrm-test-case-helper.php b/tests/helpers/class-wc-retailcrm-test-case-helper.php index 4b465191..a099ac27 100644 --- a/tests/helpers/class-wc-retailcrm-test-case-helper.php +++ b/tests/helpers/class-wc-retailcrm-test-case-helper.php @@ -100,27 +100,30 @@ protected function deleteAllData() } else { global $wpdb; - foreach ( - [ + $tables = [ + useHpos() ? $wpdb->prefix . 'wc_orders' : '', + useHpos() ? $wpdb->prefix . 'wc_orders_meta' : '', $wpdb->posts, $wpdb->postmeta, $wpdb->comments, $wpdb->commentmeta, $wpdb->term_relationships, $wpdb->termmeta, - ] as $table - ) { - //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + ]; + + foreach ($tables as $table) { + if ('' === $table) { + continue; + } + $wpdb->query("DELETE FROM {$table}"); } foreach ([$wpdb->terms, $wpdb->term_taxonomy] as $table) { - //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query("DELETE FROM {$table} WHERE term_id != 1"); } $wpdb->query("UPDATE {$wpdb->term_taxonomy} SET count = 0"); - $wpdb->query("DELETE FROM {$wpdb->users} WHERE ID != 1"); $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE user_id != 1"); } diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index 2e85defe..e48e6adc 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -327,26 +327,28 @@ public function test_history_switch_customer_tests() { $this->deleteAllData(); $this->regenerateMocks(); - $order_id = $this->history_order_create_for_changing_customer(); - $this->assertNotEmpty($order_id); + + $orderId = $this->history_order_create_for_changing_customer(); + + $this->assertNotEmpty($orderId); $this->regenerateMocks(); - $this->history_order_switch_customer($order_id); + $this->history_order_switch_customer($orderId); $this->regenerateMocks(); - $this->history_order_switch_customer_to_corporate($order_id); + $this->history_order_switch_customer_to_corporate($orderId); $this->regenerateMocks(); - $this->history_order_switch_customer_to_another_corporate($order_id); + $this->history_order_switch_customer_to_another_corporate($orderId); $this->regenerateMocks(); - $this->history_order_switch_only_company($order_id); + $this->history_order_switch_only_company($orderId); $this->regenerateMocks(); - $this->history_order_switch_only_contact($order_id); + $this->history_order_switch_only_contact($orderId); $this->regenerateMocks(); - $this->history_order_switch_back_to_individual($order_id); + $this->history_order_switch_back_to_individual($orderId); } public function history_order_create_for_changing_customer() @@ -376,16 +378,16 @@ public function history_order_create_for_changing_customer() } /** - * @param int $order_id + * @param int $orderId * * @throws \Exception */ - public function history_order_switch_customer($order_id) + public function history_order_switch_customer(int $orderId) { $this->mockHistory( true, DataHistoryRetailCrm::empty_history(), - DataHistoryRetailCrm::get_history_change_to_another_individual($order_id) + DataHistoryRetailCrm::get_history_change_to_another_individual($orderId) ); $this->ordersGetMock( @@ -396,31 +398,7 @@ public function history_order_switch_customer($order_id) $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - try { - $order = new WC_Order($order_id); - } catch (\Exception $exception) { - $post = get_post($order_id); - - if (!$post instanceof WP_Post) { - $this->fail(sprintf('Cannot find order with id=%d', $order_id)); - } - - if (!in_array($post->post_type, wc_get_order_types())) { - $this->fail(sprintf( - 'Invalid order post type `%s`. Should be one of these: %s', - $post->post_type, - implode(', ', wc_get_order_types()) - )); - } else { - $this->fail(sprintf( - 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', - $order_id, - $exception->getMessage() - )); - } - - return; - } + $order = new WC_Order($orderId); $this->assertEquals('tester002', $order->get_billing_first_name()); $this->assertEquals('tester002', $order->get_billing_last_name()); @@ -437,16 +415,16 @@ public function history_order_switch_customer($order_id) } /** - * @param int $order_id + * @param int $orderId * * @throws \Exception */ - public function history_order_switch_customer_to_corporate($order_id) + public function history_order_switch_customer_to_corporate(int $orderId) { $this->mockHistory( true, DataHistoryRetailCrm::empty_history(), - DataHistoryRetailCrm::get_history_change_to_corporate($order_id) + DataHistoryRetailCrm::get_history_change_to_corporate($orderId) ); $this->ordersGetMock( @@ -462,31 +440,7 @@ public function history_order_switch_customer_to_corporate($order_id) $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - try { - $order = new WC_Order($order_id); - } catch (\Exception $exception) { - $post = get_post($order_id); - - if (!$post instanceof WP_Post) { - $this->fail(sprintf('Cannot find order with id=%d', $order_id)); - } - - if (!in_array($post->post_type, wc_get_order_types())) { - $this->fail(sprintf( - 'Invalid order post type `%s`. Should be one of these: %s', - $post->post_type, - implode(', ', wc_get_order_types()) - )); - } else { - $this->fail(sprintf( - 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', - $order_id, - $exception->getMessage() - )); - } - - return; - } + $order = new WC_Order($orderId); $this->assertEquals('psycho913', $order->get_billing_first_name()); $this->assertEquals('psycho913', $order->get_billing_last_name()); @@ -500,16 +454,16 @@ public function history_order_switch_customer_to_corporate($order_id) } /** - * @param int $order_id + * @param int $orderId * * @throws \Exception */ - public function history_order_switch_customer_to_another_corporate($order_id) + public function history_order_switch_customer_to_another_corporate($orderId) { $this->mockHistory( true, DataHistoryRetailCrm::empty_history(), - DataHistoryRetailCrm::get_history_change_to_another_corporate($order_id) + DataHistoryRetailCrm::get_history_change_to_another_corporate($orderId) ); $this->ordersGetMock( @@ -525,31 +479,7 @@ public function history_order_switch_customer_to_another_corporate($order_id) $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - try { - $order = new WC_Order($order_id); - } catch (\Exception $exception) { - $post = get_post($order_id); - - if (!$post instanceof WP_Post) { - $this->fail(sprintf('Cannot find order with id=%d', $order_id)); - } - - if (!in_array($post->post_type, wc_get_order_types())) { - $this->fail(sprintf( - 'Invalid order post type `%s`. Should be one of these: %s', - $post->post_type, - implode(', ', wc_get_order_types()) - )); - } else { - $this->fail(sprintf( - 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', - $order_id, - $exception->getMessage() - )); - } - - return; - } + $order = new WC_Order($orderId); $this->assertEquals('Tester4867', $order->get_billing_first_name()); $this->assertEquals('Tester4867', $order->get_billing_last_name()); @@ -564,16 +494,16 @@ public function history_order_switch_customer_to_another_corporate($order_id) } /** - * @param int $order_id + * @param int $orderId * * @throws \Exception */ - public function history_order_switch_only_company($order_id) + public function history_order_switch_only_company(int $orderId) { $this->mockHistory( true, DataHistoryRetailCrm::empty_history(), - DataHistoryRetailCrm::get_history_change_only_company($order_id) + DataHistoryRetailCrm::get_history_change_only_company($orderId) ); $this->ordersGetMock( @@ -589,31 +519,7 @@ public function history_order_switch_only_company($order_id) $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - try { - $order = new WC_Order($order_id); - } catch (\Exception $exception) { - $post = get_post($order_id); - - if (!$post instanceof WP_Post) { - $this->fail(sprintf('Cannot find order with id=%d', $order_id)); - } - - if (!in_array($post->post_type, wc_get_order_types())) { - $this->fail(sprintf( - 'Invalid order post type `%s`. Should be one of these: %s', - $post->post_type, - implode(', ', wc_get_order_types()) - )); - } else { - $this->fail(sprintf( - 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', - $order_id, - $exception->getMessage() - )); - } - - return; - } + $order = new WC_Order($orderId); $this->assertEquals('Tester4867', $order->get_billing_first_name()); $this->assertEquals('Tester4867', $order->get_billing_last_name()); @@ -628,16 +534,16 @@ public function history_order_switch_only_company($order_id) } /** - * @param int $order_id + * @param int $orderId * * @throws \Exception */ - public function history_order_switch_only_contact($order_id) + public function history_order_switch_only_contact(int $orderId) { $this->mockHistory( true, DataHistoryRetailCrm::empty_history(), - DataHistoryRetailCrm::get_history_change_only_contact($order_id) + DataHistoryRetailCrm::get_history_change_only_contact($orderId) ); $this->ordersGetMock( @@ -653,31 +559,7 @@ public function history_order_switch_only_contact($order_id) $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - try { - $order = new WC_Order($order_id); - } catch (\Exception $exception) { - $post = get_post($order_id); - - if (!$post instanceof WP_Post) { - $this->fail(sprintf('Cannot find order with id=%d', $order_id)); - } - - if (!in_array($post->post_type, wc_get_order_types())) { - $this->fail(sprintf( - 'Invalid order post type `%s`. Should be one of these: %s', - $post->post_type, - implode(', ', wc_get_order_types()) - )); - } else { - $this->fail(sprintf( - 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', - $order_id, - $exception->getMessage() - )); - } - - return; - } + $order = new WC_Order($orderId); $this->assertEquals('Tester2890', $order->get_billing_first_name()); $this->assertEquals('Tester2890', $order->get_billing_last_name()); @@ -692,16 +574,16 @@ public function history_order_switch_only_contact($order_id) } /** - * @param int $order_id + * @param int $orderId * * @throws \Exception */ - public function history_order_switch_back_to_individual($order_id) + public function history_order_switch_back_to_individual(int $orderId) { $this->mockHistory( true, DataHistoryRetailCrm::empty_history(), - DataHistoryRetailCrm::get_history_change_from_corporate_to_individual($order_id) + DataHistoryRetailCrm::get_history_change_from_corporate_to_individual($orderId) ); $this->ordersGetMock( @@ -712,31 +594,7 @@ public function history_order_switch_back_to_individual($order_id) $retailcrm_history = new \WC_Retailcrm_History($this->apiMock); $retailcrm_history->getHistory(); - try { - $order = new WC_Order($order_id); - } catch (\Exception $exception) { - $post = get_post($order_id); - - if (!$post instanceof WP_Post) { - $this->fail(sprintf('Cannot find order with id=%d', $order_id)); - } - - if (!in_array($post->post_type, wc_get_order_types())) { - $this->fail(sprintf( - 'Invalid order post type `%s`. Should be one of these: %s', - $post->post_type, - implode(', ', wc_get_order_types()) - )); - } else { - $this->fail(sprintf( - 'Cannot determine what\'s wrong with order id=%d. Message from WooCommerce: %s', - $order_id, - $exception->getMessage() - )); - } - - return; - } + $order = new WC_Order($orderId); $this->assertEquals('tester001', $order->get_billing_first_name()); $this->assertEquals('tester001', $order->get_billing_last_name()); diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index 4aba7a90..d8527f14 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -55,6 +55,8 @@ public function setUp() */ public function test_order_create($retailcrm) { + $this->createTestOrder(); + if ($retailcrm) { $responseMock = $this->createResponseMock(); $responseMockCustomers = $this->createResponseMock(); @@ -68,13 +70,12 @@ public function test_order_create($retailcrm) ] ); + $this->setMockResponse($responseMock, 'isSuccessful', true); $this->setMockResponse($retailcrm, 'ordersCreate', $responseMock); $this->setMockResponse($retailcrm, 'customersCreate', $responseMock); $this->setMockResponse($retailcrm, 'customersList', $responseMockCustomers); } - $this->createTestOrder(); - $retailcrmOrders = $this->getRetailcrmOrders($retailcrm); $order = $retailcrmOrders->orderCreate($this->order->get_id()); $orderData = $retailcrmOrders->getOrder(); @@ -132,6 +133,8 @@ public function test_order_create($retailcrm) */ public function test_order_create_with_corporate_customer($retailcrm) { + $this->createTestOrder(); + if ($retailcrm) { $responseMock = $this->createResponseMock(); @@ -170,6 +173,7 @@ public function test_order_create_with_corporate_customer($retailcrm) ] ); + $this->setMockResponse($responseMock, 'isSuccessful', true); $this->setMockResponse($retailcrm, 'ordersCreate', $responseMock); $this->setMockResponse($retailcrm, 'getSingleSiteForKey', 'woo'); $this->setMockResponse($retailcrm, 'customersCorporateCreate', $responseMockCustomerCorporate); @@ -180,8 +184,6 @@ public function test_order_create_with_corporate_customer($retailcrm) $this->setMockResponse($retailcrm, 'customersCorporateCompanies', $responseMockCompany); } - $this->createTestOrder(); - $retailcrmOrders = $this->getRetailcrmOrders($retailcrm); $order = $retailcrmOrders->orderCreate($this->order->get_id()); $orderData = $retailcrmOrders->getOrder(); @@ -609,14 +611,12 @@ private function createTestOrder() } } - $this->order->save(); + $this->order->add_meta_data('woo_order', 'test_custom_fields'); + $this->order->add_meta_data('crm_phone', '1111122222'); + $this->order->add_meta_data('crm_address_text', 'crm_address_text_test'); + $this->order->add_meta_data('crm_customer_comment', 'crm_customer_comment_test'); - $orderId = $this->order->get_id(); - - update_post_meta($orderId, 'woo_order', 'test_custom_fields'); - update_post_meta($orderId, 'crm_phone', '1111122222'); - update_post_meta($orderId, 'crm_address_text', 'crm_address_text_test'); - update_post_meta($orderId, 'crm_customer_comment', 'crm_customer_comment_test'); + $this->order->save(); } private function getResponseData($externalId) diff --git a/tests/test-wc-retailcrm-uploader.php b/tests/test-wc-retailcrm-uploader.php index 11f631ef..eec6ff31 100644 --- a/tests/test-wc-retailcrm-uploader.php +++ b/tests/test-wc-retailcrm-uploader.php @@ -87,7 +87,7 @@ public function test_customers_upload($retailcrm) public function test_order_upload($retailcrm) { $retailcrm_uploader = $this->getRetailcrmUploader($retailcrm); - $data = $retailcrm_uploader->uploadArchiveOrders(0); + $data = $retailcrm_uploader->uploadArchiveOrders(null); $this->assertEquals(null, $data); } @@ -109,7 +109,6 @@ public function test_get_count_orders_upload() { $retailcrm_uploader = $this->getRetailcrmUploader($this->apiMock); $count_orders = $retailcrm_uploader->getCountOrders(); - $this->assertInternalType('int', $count_orders); }