Skip to content

Commit

Permalink
Hpos support and order vendor id fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Aunshon committed Aug 29, 2023
1 parent 2ac6d32 commit a676b13
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions includes/Integrations/YithMultiVendor/OrderMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class OrderMigrator extends OrderMigration {
*
* @since DOKAN_MIG_SINCE
*
* @param \WP_Post $order
* @param \WC_Order $order
*/
public function __construct( \WP_Post $order ) {
public function __construct( \WC_Order $order ) {
$this->order_id = $order->ID;
$this->order = wc_get_order( $this->order_id );
$this->order = $order;
}

/**
Expand All @@ -34,26 +34,24 @@ public function __construct( \WP_Post $order ) {
* @return \WC_Order
*/
public function create_sub_order_if_needed( $seller_id, $seller_products, $parent_order_id ) {
$args = array(
'posts_per_page' => -1,
'order' => 'DESC',
'post_parent' => $parent_order_id,
'post_type' => 'shop_order',
);
$orders = dokan()->order->get_child_orders( $parent_order_id );
$parent_order = dokan()->order->get( $parent_order_id );

$sub_orders = get_children( $args, ARRAY_A );
$child_order = null;

$current_order = $parent_order_id;
foreach ( $orders as $order ) {
$order_items = $order->get_items();
foreach ( $order_items as $product_item ) {
$post = get_post( $product_item->get_product_id(), ARRAY_A );
$author = $post['post_author'];

foreach ( $sub_orders as $id => $sub_order ) {
if ( absint( $sub_order['post_author'] ) === $seller_id ) {
$current_order = $id;
if ( absint( $author ) === absint( $seller_id ) ) {
$child_order = $order;
break;
}
}
}

$child_order = wc_get_order( $current_order );
$parent_order = wc_get_order( $parent_order_id );

$this->add_splited_shipping( $child_order, $parent_order );

return $child_order;
Expand Down Expand Up @@ -135,7 +133,7 @@ public function reset_sub_orders_if_needed() {
}

/**
* Gets order data from wcfm order table for dokan.
* Gets order data from yith order table for dokan.
*
* @since DOKAN_MIG_SINCE
*
Expand All @@ -146,7 +144,7 @@ public function reset_sub_orders_if_needed() {
*/
public function get_dokan_order_data( $parent_order_id, $seller_id ) {
global $wpdb;
$wc_order = wc_get_order( $parent_order_id );
$wc_order = dokan()->order->get( $parent_order_id );

$net_amount = 0;
$order_total = $wc_order->get_total();
Expand All @@ -157,17 +155,10 @@ public function get_dokan_order_data( $parent_order_id, $seller_id ) {
$order_total = $order_total - $wc_order->get_total_refunded();
}

$args = array(
'posts_per_page' => -1,
'order' => 'DESC',
'post_parent' => $parent_order_id,
'post_type' => 'shop_order',
);

$sub_orders = get_children( $args, ARRAY_A );
$sub_orders = dokan()->order->get_child_orders( $parent_order_id );

foreach ( $sub_orders as $id => $sub_order ) {
$orders = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}yith_vendors_commissions WHERE user_id = %d AND order_id=%d", $seller_id, $id ) );
foreach ( $sub_orders as $sub_order ) {
$orders = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}yith_vendors_commissions WHERE user_id = %d AND order_id=%d", $seller_id, $sub_order->get_id() ) );

foreach ( $orders as $order ) {
$net_amount += $order->amount - abs( $order->amount_refunded );
Expand Down Expand Up @@ -195,7 +186,27 @@ public function get_dokan_order_data( $parent_order_id, $seller_id ) {
if ( count( $sub_orders ) === 1 ) {
// update post type
$sub_order = reset( $sub_orders );
set_post_type( $sub_order['ID'], 'dep_yith_order' );
set_post_type( $sub_order->get_id(), 'dep_yith_order' );

// If HPOS enabled
if ( class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() ) {
global $wpdb;

$wpdb->update(
$wpdb->prefix . 'wc_orders',
[ 'type' => 'dep_yith_order' ],
[ 'id' => $sub_order->get_id() ]
);
}

$wc_order->update_meta_data( 'has_sub_order', 0 );
$wc_order->save();
} else {
foreach ( $sub_orders as $sub_order ) {
$sub_order_seller_id = dokan_get_seller_id_by_order( $sub_order->get_id() );
$sub_order->update_meta_data( '_dokan_vendor_id', $sub_order_seller_id );
$sub_order->save();
}
}

foreach ( $commissions as $com ) {
Expand Down Expand Up @@ -225,7 +236,7 @@ public function get_dokan_order_data( $parent_order_id, $seller_id ) {
*/
public function process_refund( $child_order, $seller_id, $from_suborder = true ) {
global $wpdb;
$order = wc_get_order( $child_order->get_id() );
$order = dokan()->order->get( $child_order->get_id() );
$new_total_amount = $order->get_total() - $order->get_total_refunded();

// insert on dokan sync table
Expand Down

0 comments on commit a676b13

Please sign in to comment.