Skip to content

Commit

Permalink
fix: restore order stock if its been reduced by twice is fixed #428 (#…
Browse files Browse the repository at this point in the history
…439)

* fix: restore order stock if its been reduced by twice is fixed #428

* fix: use correct text domain
  • Loading branch information
saimonh3 authored and sabbir1991 committed Nov 26, 2018
1 parent 5137957 commit c366d26
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions includes/class-order-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function __construct() {
if ( is_admin() ) {
add_action( 'woocommerce_process_shop_order_meta', 'dokan_sync_insert_order' );
}

// restore order stock if it's been reduced by twice
add_action( 'woocommerce_reduce_order_stock', array( $this, 'restore_reduced_order_stock' ) );
}

/**
Expand Down Expand Up @@ -486,4 +489,48 @@ public function ensure_vendor_coupon( $valid, $coupon ) {

return $valid;
}


/**
* Restore order stock if it's been reduced by twice
*
* @param object $order
*
* @return void
*/
public function restore_reduced_order_stock( $order ) {
$has_sub_order = wp_get_post_parent_id( $order->get_id() );

// seems it's not a parent order so return early
if ( ! $has_sub_order ) {
return;
}

// Loop over all items.
foreach ( $order->get_items() as $item ) {
if ( ! $item->is_type( 'line_item' ) ) {
continue;
}

// Only reduce stock once for each item.
$product = $item->get_product();
$item_stock_reduced = $item->get_meta( '_reduced_stock', true );

if ( ! $item_stock_reduced || ! $product || ! $product->managing_stock() ) {
continue;
}

$item_name = $product->get_formatted_name();
$new_stock = wc_update_product_stock( $product, $item_stock_reduced, 'increase' );

if ( is_wp_error( $new_stock ) ) {
/* translators: %s item name. */
$order->add_order_note( sprintf( __( 'Unable to restore stock for item %s.', 'dokan-lite' ), $item_name ) );
continue;
}

$item->delete_meta_data( '_reduced_stock' );
$item->save();
}
}
}

0 comments on commit c366d26

Please sign in to comment.