Skip to content

Commit

Permalink
Migrate Wc vendors to dokan (#5)
Browse files Browse the repository at this point in the history
* mπŸ›   Update wc-vendors as updated wcfm

* ✍️ Fix location array to string

* πŸ”„ Uninstall wc-vendors plugins

* πŸ‘ code standard fix

* πŸ‘Œ re-create dokan pages

* Update: code refactoring

* πŸ› Fix: Resolve bug of ABSPATH

* Fix: wcvendors withdraw plugin select switch case

* Fix: wcvendors withdraw plugin select switch case

* Fix: wcvendors withdraw plugin select switch case

* Fix: wcvendors withdraw plugin select switch case

* Fix: version

* ⚑️Fix: exclude dokan orders, migrate wcfm orders only.

* ⚑️Fix: exclude dokan orders, migrate wcfm orders only.

* ⚑️Fix: exclude dokan orders, migrate wcfm orders only.

* ⚑️Fix: exclude dokan orders, migrate wcfm orders only.

* ⚑️Fix: exclude dokan orders, migrate wcfm orders only.

* ⚑️Fix: exclude dokan orders, migrate wcfm orders only.

* ⚑️HPOS support

* ⚑️HPOS support

* ⚑️HPOS support

* ⚑️HPOS support

* ⚑️HPOS support

* Fix order count

* Remove alert message of migrate to dokan in dokan migration page

* Add paged key support in api

* Fix phpcs

* Add get seller by order abstract method

* Update zip maker

* add get seller by order support

* Set order id
  • Loading branch information
Aunshon authored Dec 12, 2023
1 parent b32e964 commit 6b13db0
Show file tree
Hide file tree
Showing 21 changed files with 1,019 additions and 125 deletions.
5 changes: 1 addition & 4 deletions dokan-migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
* **********************************************************************
*/

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;

/**
* Dokan_Migrator class
Expand Down
10 changes: 7 additions & 3 deletions includes/Abstracts/OrderMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ public function clear_dokan_refund_table( $order_id ) {
* @return void
*/
public function sync_dokan_order_table( $dokan_order_data, $sub_order_id, $seller_id, $order_obj ) {
$order_total = $order_obj->get_total();
$net_amount = $dokan_order_data['net_sale'];
$created_date = reset( $dokan_order_data['commission_data'] )['created'];
$order_total = $order_obj->get_total();
$net_amount = $dokan_order_data['net_sale'];
$created_date = ! empty( $dokan_order_data['commission_data'] ) ? reset( $dokan_order_data['commission_data'] )['created'] : dokan_current_datetime()->format( 'Y-m-d H:i:s' );

global $wpdb;

Expand Down Expand Up @@ -305,6 +305,10 @@ public function process_migration() {
* @return void
*/
public function update_commission_applied_data_in_order( $order, $commission_data ) {
if ( empty( $commission_data['commission_data'] ) ) {
return;
}

$commission = reset( $commission_data['commission_data'] );

foreach ( $order->get_items() as $item_id => $item ) {
Expand Down
5 changes: 1 addition & 4 deletions includes/Abstracts/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace WeDevs\DokanMigrator\Abstracts;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;

/**
* This class defines the methods for vendor, order and withdraw handler.
Expand Down
5 changes: 1 addition & 4 deletions includes/Abstracts/VendorMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace WeDevs\DokanMigrator\Abstracts;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;

abstract class VendorMigration {

Expand Down
5 changes: 1 addition & 4 deletions includes/Abstracts/WithdrawMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace WeDevs\DokanMigrator\Abstracts;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;

/**
* Withdraw abstract class.
Expand Down
8 changes: 3 additions & 5 deletions includes/Admin/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

use WeDevs\DokanMigrator\Helpers\MigrationHelper;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;

/**
* Dokan Migrator Manager Class
Expand Down Expand Up @@ -68,7 +65,8 @@ public function show_dokan_dashboard_activate_notice() {

$data = MigrationHelper::get_last_migrated();

if ( 'yes' !== $data['migration_success'] ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( 'yes' !== $data['migration_success'] && ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'dokan-migrator' ) ) {
require_once DOKAN_MIGRATOR_TEMPLATE_PATH . 'template-alert-migrate-to-dokan.php';
}
}
Expand Down
5 changes: 1 addition & 4 deletions includes/Admin/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace WeDevs\DokanMigrator\Admin;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;

/**
* Dokan Migrator Menu Class
Expand Down
132 changes: 127 additions & 5 deletions includes/Helpers/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace WeDevs\DokanMigrator\Helpers;

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use WC_Order;

defined( 'ABSPATH' ) || exit;

/**
* Dokan migrator helper class
Expand Down Expand Up @@ -45,14 +44,28 @@ public static function get_last_migrated() {
public static function active_vendor_dashboard() {
$all_plugins_to_deactivate = [];

// Wcfm plugins
// Wcfm plugins.
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-frontend-manager/wc_frontend_manager.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-multivendor-marketplace/wc-multivendor-marketplace.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-multivendor-membership/wc-multivendor-membership.php';

// Wc vendors plugins.
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-vendors-pro-simple-auctions/class-wcv-simple-auctions.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-vendors-pro/wcvendors-pro.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-vendors-woocommerce-bookings/wcv-woocommerce-bookings.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-vendors-woocommerce-subscriptions/wcv-wc-subscriptions.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-vendors/class-wc-vendors.php';
$all_plugins_to_deactivate[] = WP_PLUGIN_DIR . '/wc-vendors-membership/wc-vendors-membership.php';

deactivate_plugins( $all_plugins_to_deactivate );

delete_option( 'dokan_migration_completed' );
/**
* Deleting this option, because sometimes after migrating to dokan from other marketplace
* dokan dashboard page conflicts with their pages so deleting this option will enable user
* to re-create dokan pages from dokan tools page.
*/
delete_option( 'dokan_pages_created' );

wp_send_json_success( __( 'Dokan vendor dashboard activated.', 'dokan-migrator' ) );
}
Expand Down Expand Up @@ -95,6 +108,10 @@ public static function get_migration_title( $plugin ) {
$title = __( 'Migrate Wcfm To Dokan', 'dokan-migrator' );
break;

case 'wcvendors':
$title = __( 'Migrate Wc Vendors To Dokan.', 'dokan-migrator' );
break;

default:
break;
}
Expand All @@ -118,6 +135,111 @@ public static function get_migratable_plugin() {
// WCfM Multivendor Marketplace Check
$is_marketplace = ( in_array( 'wc-multivendor-marketplace/wc-multivendor-marketplace.php', $active_plugins, true ) || array_key_exists( 'wc-multivendor-marketplace/wc-multivendor-marketplace.php', $active_plugins ) || class_exists( 'WCFMmp' ) ) ? 'wcfmmarketplace' : false;

// WC Vendors Check
if ( ! $is_marketplace ) {
$is_marketplace = ( in_array( 'wc-vendors/class-wc-vendors.php', $active_plugins, true ) || array_key_exists( 'wc-vendors/class-wc-vendors.php', $active_plugins ) || class_exists( 'WC_Vendors' ) ) ? 'wcvendors' : false;
}

return $is_marketplace;
}


/**
* Converts the order item meta key according to
* Dokan meta key as those data will be parsed
* while creating sub orders.
*
* @param WC_Order $order
* @param string $map_by
*
* @since DOKAN_MIG_SINCE
*
* @return void
*/
public static function map_shipping_method_item_meta( WC_Order $order, $map_by = 'vendor_id' ) {
if ( ! $order instanceof WC_Order ) {
return;
}

$shipping_methods = $order->get_shipping_methods();
if ( empty( $shipping_methods ) ) {
return;
}

foreach ( $shipping_methods as $method_item_id => $shipping_object ) {
$seller_id = wc_get_order_item_meta( $method_item_id, $map_by, true );

if ( ! $seller_id ) {
continue;
}

wc_update_order_item_meta(
$method_item_id,
'seller_id',
wc_get_order_item_meta( $method_item_id, $map_by, true )
);
}
}

/**
* Split shipping amount for all vendors if wcfm processing an order as admin shipping.
*
* @since DOKAN_MIG_SINCE
*
* @param WC_Order_Item_Shipping $applied_shipping_method
* @param int $order_id
* @param WC_Order $parent_order
*
* @return WC_Order_Item_Shipping
*/
public static function split_parent_order_shipping( $applied_shipping_method, $order_id, $parent_order ) {
/**
* Not empty means parent order has sub-order and this order is processed as vendor wise shipping in wcfm.
* Or if parent order has no shipping methods the return it.
*/
if ( ! empty( $applied_shipping_method ) || count( $parent_order->get_shipping_methods() ) < 1 ) {
return $applied_shipping_method;
}

$applied_shipping_method = reset( $parent_order->get_shipping_methods() );
$vendors = dokan_get_sellers_by( $parent_order->get_id() );

// Here we are dividing the shipping and shipping-tax amount of parent order into the vendors suborders.
$shipping_tax_amount = [
'total' => [ $applied_shipping_method->get_total_tax() / count( $vendors ) ],
];
$shipping_amount = $applied_shipping_method->get_total() / count( $vendors );

// Generating the shipping for vendor.
$item = new WC_Order_Item_Shipping();
$item->set_props(
array(
'method_title' => $applied_shipping_method->get_name(),
'method_id' => $applied_shipping_method->get_method_id(),
'total' => $shipping_amount,
'taxes' => $shipping_tax_amount,
)
);

$child_order = new WC_order( $order_id );
$products = $child_order->get_items();

// Generating the shipping Item text.
$text = '';
foreach ( $products as $key => $product ) {
$current_item = $product->get_data();
$product_text = $current_item['name'] . ' &times; ' . $current_item['quantity'];

if ( $key !== array_key_first( $products ) ) {
$text .= ', ' . $product_text;
}

$text .= $product_text;
}

// Adding shipping item meta data.
$item->add_meta_data( 'Items', $text );

return $item;
}
}
Loading

0 comments on commit 6b13db0

Please sign in to comment.