From 977342cba05f4c0640e7023046b0ae97e3cc2ef7 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 28 Aug 2023 08:55:49 +0600 Subject: [PATCH 1/2] Hpos support - remove post query and get it from WC_Order (#16) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ⚡️Hpos support - remove post query and get it from WC_Order * ⚡️Add plugin file to hpos supported plugin list. * ⚡️HPOS support * ⚡️HPOS support * ⚡️HPOS support * ⚡️HPOS support --- dokan-migrator.php | 20 ++++++++++++- includes/Abstracts/OrderMigration.php | 28 ++++++++---------- includes/Integrations/Wcfm/OrderMigrator.php | 29 ++++++++++--------- includes/Integrations/Wcfm/VendorMigrator.php | 10 +++---- includes/Migrator/Manager.php | 18 ++++++++---- includes/Processors/Order.php | 21 ++++++-------- 6 files changed, 72 insertions(+), 54 deletions(-) diff --git a/dokan-migrator.php b/dokan-migrator.php index 0be382d..75ab4ee 100644 --- a/dokan-migrator.php +++ b/dokan-migrator.php @@ -48,6 +48,8 @@ * * @class Dokan_Migrator The class that holds the entire Dokan_Migrator plugin * + * @property WeDevs\DokanMigrator\Migrator\Manager $migrator Instance of migrator class. + * * @since 1.0.0 */ final class Dokan_Migrator { @@ -88,6 +90,9 @@ private function __construct() { register_activation_hook( __FILE__, [ $this, 'activate' ] ); + // Add woocommerce HPOS support. + add_action( 'before_woocommerce_init', [ $this, 'add_plugin_hpos_support' ] ); + // load the addon add_action( 'dokan_loaded', array( $this, 'plugin_init' ) ); @@ -166,7 +171,7 @@ private function init_classes() { } $this->container['migrator'] = new \WeDevs\DokanMigrator\Migrator\Manager(); - $this->container = apply_filters( 'dokan_migrator_get_class_container', $this->container ); + $this->container = apply_filters( 'dokan_migrator_get_class_container', $this->container ); } /** @@ -209,6 +214,19 @@ function() { $insights->init(); } + + /** + * Make dokan migrator plugin HPOS supported. + * + * @since DOKAN_MIG_SINCE + * + * @return void + */ + public function add_plugin_hpos_support() { + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); + } + } } /** diff --git a/includes/Abstracts/OrderMigration.php b/includes/Abstracts/OrderMigration.php index e5edcd5..4c27de8 100644 --- a/includes/Abstracts/OrderMigration.php +++ b/includes/Abstracts/OrderMigration.php @@ -2,6 +2,9 @@ namespace WeDevs\DokanMigrator\Abstracts; // don't call the file directly +use stdClass; +use WC_Order; + if ( ! defined( 'ABSPATH' ) ) { exit; } @@ -16,7 +19,7 @@ abstract class OrderMigration { /** * Current order object instance. * - * @var \WC_Order + * @var WC_Order */ public $order = null; @@ -60,7 +63,7 @@ abstract public function get_dokan_order_data( $parent_order_id, $seller_id ); * @param int $seller_id * @param array $seller_products * - * @return \WC_Order + * @return WC_Order */ abstract public function create_sub_order_if_needed( $seller_id, $seller_products, $parent_order_id ); @@ -89,17 +92,10 @@ public function has_sub_order() { * * @since 1.0.0 * - * @return array + * @return stdClass|WC_Order[] */ public function get_sub_orders() { - $args = array( - 'post_parent' => $this->order_id, - 'post_type' => 'shop_order', - 'numberposts' => -1, - 'post_status' => 'any', - ); - - return get_children( $args ); + return dokan()->order->get_child_orders( $this->order_id ); } /** @@ -112,10 +108,10 @@ public function get_sub_orders() { public function reset_sub_orders() { if ( $this->has_sub_order() ) { foreach ( $this->get_sub_orders() as $child ) { - wp_delete_post( $child->ID, true ); - $this->clear_dokan_vendor_balance_table( $child->ID ); - $this->clear_dokan_order_table( $child->ID, $child->post_author ); - $this->clear_dokan_refund_table( $child->ID ); + $child->delete( true ); + $this->clear_dokan_vendor_balance_table( $child->get_id() ); + $this->clear_dokan_order_table( $child->get_id(), $child->get_user()->ID ); + $this->clear_dokan_refund_table( $child->get_id() ); } } } @@ -397,7 +393,7 @@ public function dokan_sync_refund_table( $data ) { * * @since 1.0.0 * - * @param \WC_Order $order + * @param WC_Order $order * * @return boolean */ diff --git a/includes/Integrations/Wcfm/OrderMigrator.php b/includes/Integrations/Wcfm/OrderMigrator.php index faab6b0..02b5b44 100644 --- a/includes/Integrations/Wcfm/OrderMigrator.php +++ b/includes/Integrations/Wcfm/OrderMigrator.php @@ -24,11 +24,11 @@ class OrderMigrator extends OrderMigration { * * @since DOKAN_PRO_SINCE * - * @param \WP_Post $order + * @param \WC_Order $order */ - public function __construct( \WP_Post $order ) { - $this->order_id = $order->ID; - $this->order = wc_get_order( $this->order_id ); + public function __construct( \WC_Order $order ) { + $this->order_id = $order->get_id(); + $this->order = $order; add_filter( 'dokan_shipping_method', [ $this, 'split_parent_order_shipping' ], 10, 3 ); } @@ -52,19 +52,20 @@ public function create_sub_order_if_needed( $seller_id, $seller_products, $paren $this->map_shipping_method_item_meta(); dokan()->order->create_sub_order( $this->order, $seller_id, $seller_products ); - $res = get_posts( - array( - 'numberposts' => 1, - 'post_status' => 'any', - 'post_type' => 'shop_order', - 'post_parent' => $this->order->get_id(), - 'meta_key' => '_dokan_vendor_id', // phpcs:ignore WordPress.DB.SlowDBQuery - 'meta_value' => $seller_id, // phpcs:ignore WordPress.DB.SlowDBQuery - ) + $res = dokan()->order->all( + [ + 'limit' => 1, + 'parent' => $this->order->get_id(), + 'seller_id' => $seller_id, + ] ); + + /** + * @var $created_suborder WC_Order|\WC_Order_Refund + */ $created_suborder = reset( $res ); - return wc_get_order( $created_suborder->ID ); + return $created_suborder; } /** diff --git a/includes/Integrations/Wcfm/VendorMigrator.php b/includes/Integrations/Wcfm/VendorMigrator.php index 7639df2..efcffdc 100644 --- a/includes/Integrations/Wcfm/VendorMigrator.php +++ b/includes/Integrations/Wcfm/VendorMigrator.php @@ -316,13 +316,13 @@ public function get_store_seo( $default ) { */ public function get_commission() { $accepted_commission_types = array( 'percent', 'fixed' ); - $dokan_commission = [ - 'dokan_admin_percentage' => '', // WCFM fixed value, commission_fixed + $dokan_commission = [ + 'dokan_admin_percentage' => '', // WCFM fixed value, commission_fixed 'dokan_admin_percentage_type' => '', // WCFM commission type, commission_mode - 'dokan_admin_additional_fee' => '', // WCFM percentage value, commission_percent + 'dokan_admin_additional_fee' => '', // WCFM percentage value, commission_percent ]; - $commission = $this->get_profile_settings_val( 'commission', [] ); - $commission_type = isset( $commission['commission_mode'] ) ? $commission['commission_mode'] : 'global'; + $commission = $this->get_profile_settings_val( 'commission', [] ); + $commission_type = isset( $commission['commission_mode'] ) ? $commission['commission_mode'] : 'global'; if ( in_array( $commission_type, $accepted_commission_types, true ) ) { $dokan_commission['dokan_admin_percentage'] = isset( $commission['commission_fixed'] ) ? $commission['commission_fixed'] : ''; diff --git a/includes/Migrator/Manager.php b/includes/Migrator/Manager.php index 3a67d07..4e48169 100644 --- a/includes/Migrator/Manager.php +++ b/includes/Migrator/Manager.php @@ -7,6 +7,9 @@ exit; } +use WeDevs\DokanMigrator\Integrations\Wcfm\OrderMigrator as WcfmOrderMigrator; +use WeDevs\DokanMigrator\Integrations\Wcfm\VendorMigrator as WcfmVendorMigrator; +use WeDevs\DokanMigrator\Integrations\Wcfm\WithdrawMigrator as WcfmWithdrawMigrator; use WeDevs\DokanMigrator\Migrator\Ajax; use WeDevs\DokanMigrator\Migrator\Assets; @@ -164,7 +167,7 @@ public function get_total( $import_type, $migratable ) { * @param string $plugin Handle of the plugin which is being migrated * @param array{number:int,offset:int,total_count:int,total_migrated:int} $data * - * @return void + * @return array * @throws \Exception */ public function migrate( $import_type, $plugin, $data ) { @@ -174,17 +177,20 @@ public function migrate( $import_type, $plugin, $data ) { $processor = $this->processor_class( $import_type ); - $data = call_user_func( [ $processor, 'get_items' ], $plugin, $this->number, $this->offset ); + $data_to_migrate = call_user_func( [ $processor, 'get_items' ], $plugin, $this->number, $this->offset ); - foreach ( $data as $value ) { + foreach ( $data_to_migrate as $value ) { + /** + * @var $migrator WcfmOrderMigrator|WcfmVendorMigrator|WcfmWithdrawMigrator + */ $migrator = call_user_func( [ $processor, 'get_migration_class' ], $plugin, $value ); $migrator->process_migration(); } $args = [ - 'migrated' => count( $data ), - 'next' => count( $data ) + $this->offset, - 'total_migrated' => count( $data ) + $this->total_migrated, + 'migrated' => count( $data_to_migrate ), + 'next' => count( $data_to_migrate ) + $this->offset, + 'total_migrated' => count( $data_to_migrate ) + $this->total_migrated, ]; $progress = ( $args['total_migrated'] * 100 ) / $this->total_count; diff --git a/includes/Processors/Order.php b/includes/Processors/Order.php index 8e98f5d..6090e21 100644 --- a/includes/Processors/Order.php +++ b/includes/Processors/Order.php @@ -27,9 +27,7 @@ class Order extends Processor { * @return integer */ public static function get_total( $plugin ) { - global $wpdb; - - return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}posts WHERE post_type='shop_order' AND post_parent=0" ); + return (int) dokan()->order->all( [ 'return' => 'count' ] ); } /** @@ -37,20 +35,19 @@ public static function get_total( $plugin ) { * * @since 1.0.0 * - * @return array + * @return \WC_Order[] + * * @throws \Exception */ public static function get_items( $plugin, $number, $offset ) { $args = array( - 'post_type' => 'shop_order', - 'orderby' => 'ASC', - 'post_status' => 'any', - 'offset' => $offset, - 'posts_per_page' => $number, - 'post_parent' => 0, + 'order' => 'ASC', + 'paged' => $offset + 1, + 'limit' => $number, + 'parent' => 0, ); - $orders = get_posts( $args ); + $orders = dokan()->order->all( $args ); if ( empty( $orders ) ) { self::throw_error(); @@ -67,7 +64,7 @@ public static function get_items( $plugin, $number, $offset ) { * @param string $plugin * @param object $payload * - * @return object + * @return object|WcfmOrderMigrator * @throws \Exception */ public static function get_migration_class( $plugin, $payload ) { From c402689733b4cd307f7a3e8fec6797f227af48bf Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 28 Aug 2023 15:05:14 +0600 Subject: [PATCH 2/2] Fix order count (#18) --- includes/Processors/Order.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Processors/Order.php b/includes/Processors/Order.php index 6090e21..66327c1 100644 --- a/includes/Processors/Order.php +++ b/includes/Processors/Order.php @@ -27,7 +27,7 @@ class Order extends Processor { * @return integer */ public static function get_total( $plugin ) { - return (int) dokan()->order->all( [ 'return' => 'count' ] ); + return (int) dokan()->order->all( [ 'return' => 'count', 'parent' => 0, ] ); } /**