Skip to content

Commit

Permalink
Merge branch 'develop' into feat/yithmultivendor-to-dokan
Browse files Browse the repository at this point in the history
# Conflicts:
#	includes/Migrator/Manager.php
  • Loading branch information
Aunshon committed Aug 28, 2023
2 parents fe86af6 + c402689 commit 5333644
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 55 deletions.
20 changes: 19 additions & 1 deletion dokan-migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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' ) );

Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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 );
}
}
}

/**
Expand Down
28 changes: 12 additions & 16 deletions includes/Abstracts/OrderMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace WeDevs\DokanMigrator\Abstracts;

// don't call the file directly
use stdClass;
use WC_Order;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}
Expand All @@ -16,7 +19,7 @@ abstract class OrderMigration {
/**
* Current order object instance.
*
* @var \WC_Order
* @var WC_Order
*/
public $order = null;

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -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() );
}
}
}
Expand Down Expand Up @@ -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
*/
Expand Down
29 changes: 15 additions & 14 deletions includes/Integrations/Wcfm/OrderMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand All @@ -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;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions includes/Integrations/Wcfm/VendorMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : '';
Expand Down
17 changes: 10 additions & 7 deletions includes/Migrator/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) {
Expand All @@ -177,20 +180,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 \WeDevs\DokanMigrator\Abstracts\VendorMigration|\WeDevs\DokanMigrator\Abstracts\OrderMigration|\WeDevs\DokanMigrator\Abstracts\WithdrawMigration
* @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;
Expand Down
21 changes: 9 additions & 12 deletions includes/Processors/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,27 @@ 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', 'parent' => 0, ] );

Check failure on line 31 in includes/Processors/Order.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Comma not allowed after last value in single-line array declaration

Check warning on line 31 in includes/Processors/Order.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

When a multi-item array uses associative keys, each value should start on a new line.
}

/**
* Returns array of items vendor.
*
* @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();
Expand All @@ -68,7 +65,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 ) {
Expand Down

0 comments on commit 5333644

Please sign in to comment.