Skip to content

Commit

Permalink
fix: order rest api not created sub order fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbir1991 committed Jan 2, 2020
1 parent da941d3 commit 88e9508
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 66 deletions.
72 changes: 6 additions & 66 deletions includes/Order/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct() {
add_action( 'woocommerce_order_status_changed', array( $this, 'on_sub_order_change' ), 99, 3 );

// create sub-orders
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'maybe_split_orders' ) );
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'split_vendor_orders' ) );

// order table synced for WooCommerce update order meta
add_action( 'woocommerce_checkout_update_order_meta', 'dokan_sync_insert_order', 20 );
Expand Down Expand Up @@ -61,7 +61,7 @@ public function on_order_status_change( $order_id, $old_status, $new_status, $or
remove_action( 'woocommerce_order_status_changed', array( $this, 'on_order_status_change' ), 10 );

// Split the order.
$this->maybe_split_orders( $order_id );
dokan()->order->maybe_split_orders( $order_id );

// Add the hook back.
add_action( 'woocommerce_order_status_changed', array( $this, 'on_order_status_change' ), 10, 4 );
Expand Down Expand Up @@ -142,74 +142,14 @@ public function on_sub_order_change( $order_id, $old_status, $new_status ) {
}

/**
* Monitors a new order and attempts to create sub-orders
* Split order for vendor
*
* If an order contains products from multiple vendor, we can't show the order
* to each seller dashboard. That's why we need to divide the main order to
* some sub-orders based on the number of sellers.
*
* @param int $parent_order_id
* @since DOKAN_LITE_SINCE
*
* @return void
*/
public function maybe_split_orders( $parent_order_id ) {
$parent_order = dokan()->order->get( $parent_order_id );

dokan_log( sprintf( 'New Order #%d created. Init sub order.', $parent_order_id ) );

if ( $parent_order->get_meta( 'has_sub_order' ) == true ) {

$args = array(
'post_parent' => $parent_order_id,
'post_type' => 'shop_order',
'numberposts' => -1,
'post_status' => 'any'
);

$child_orders = get_children( $args );

foreach ( $child_orders as $child ) {
wp_delete_post( $child->ID, true );
}
}

$vendors = dokan_get_sellers_by( $parent_order_id );

// return if we've only ONE seller
if ( count( $vendors ) == 1 ) {
dokan_log( '1 vendor only, skipping sub order.');

$temp = array_keys( $vendors );
$seller_id = reset( $temp );

do_action( 'dokan_create_parent_order', $parent_order, $seller_id );

// record admin commision
$admin_fee = dokan_get_admin_commission_by( $parent_order, $seller_id );
$parent_order->update_meta_data( '_dokan_admin_fee', $admin_fee );
$parent_order->update_meta_data( '_dokan_vendor_id', $seller_id );
$parent_order->save();

// if the request is made from rest api then insert the order data to the sync table
if ( defined( 'REST_REQUEST' ) ) {
do_action( 'dokan_checkout_update_order_meta', $parent_order_id, $seller_id );
}

return;
}

// flag it as it has a suborder
$parent_order->update_meta_data( 'has_sub_order', true );
$parent_order->save();

dokan_log( sprintf( 'Got %s vendors, starting sub order.', count( $vendors ) ) );

// seems like we've got multiple sellers
foreach ( $vendors as $seller_id => $seller_products ) {
dokan()->order->create_sub_order( $parent_order, $seller_id, $seller_products );
}

dokan_log( sprintf( 'Completed sub order for #%d.', $parent_order_id ) );
public function split_vendor_orders( $parent_order_id ) {
dokan()->order->maybe_split_orders( $parent_order_id );
}

/**
Expand Down
72 changes: 72 additions & 0 deletions includes/Order/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,76 @@ public function create_coupons( $order, $parent_order, $products ) {

$order->save();
}

/**
* Monitors a new order and attempts to create sub-orders
*
* If an order contains products from multiple vendor, we can't show the order
* to each seller dashboard. That's why we need to divide the main order to
* some sub-orders based on the number of sellers.
*
* @param int $parent_order_id
*
* @return void
*/
public function maybe_split_orders( $parent_order_id ) {
$parent_order = dokan()->order->get( $parent_order_id );

dokan_log( sprintf( 'New Order #%d created. Init sub order.', $parent_order_id ) );

if ( $parent_order->get_meta( 'has_sub_order' ) == true ) {

$args = array(
'post_parent' => $parent_order_id,
'post_type' => 'shop_order',
'numberposts' => -1,
'post_status' => 'any'
);

$child_orders = get_children( $args );

foreach ( $child_orders as $child ) {
wp_delete_post( $child->ID, true );
}
}

$vendors = dokan_get_sellers_by( $parent_order_id );

// return if we've only ONE seller
if ( count( $vendors ) == 1 ) {
dokan_log( '1 vendor only, skipping sub order.');

$temp = array_keys( $vendors );
$seller_id = reset( $temp );

do_action( 'dokan_create_parent_order', $parent_order, $seller_id );

// record admin commision
$admin_fee = dokan_get_admin_commission_by( $parent_order, $seller_id );
$parent_order->update_meta_data( '_dokan_admin_fee', $admin_fee );
$parent_order->update_meta_data( '_dokan_vendor_id', $seller_id );
$parent_order->save();

// if the request is made from rest api then insert the order data to the sync table
if ( defined( 'REST_REQUEST' ) ) {
do_action( 'dokan_checkout_update_order_meta', $parent_order_id, $seller_id );
}

return;
}

// flag it as it has a suborder
$parent_order->update_meta_data( 'has_sub_order', true );
$parent_order->save();

dokan_log( sprintf( 'Got %s vendors, starting sub order.', count( $vendors ) ) );

// seems like we've got multiple sellers
foreach ( $vendors as $seller_id => $seller_products ) {
dokan()->order->create_sub_order( $parent_order, $seller_id, $seller_products );
}

dokan_log( sprintf( 'Completed sub order for #%d.', $parent_order_id ) );
}

}

0 comments on commit 88e9508

Please sign in to comment.