Skip to content

Commit

Permalink
Reduce stock once and record in _order_stock_reduced meta
Browse files Browse the repository at this point in the history
Also clears up the payment_complete method
  • Loading branch information
mikejolley committed Sep 15, 2015
1 parent 96814b9 commit 0164584
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions includes/abstracts/abstract-wc-order.php
Expand Up @@ -2285,8 +2285,7 @@ public function cancel_order( $note = '' ) {
*
* @param $transaction_id string Optional transaction id to store in post meta
*/
public function payment_complete( $transaction_id = '' ) {

public function payment_complete( $transaction_id = '', $reduce_stock = true ) {
do_action( 'woocommerce_pre_payment_complete', $this->id );

if ( null !== WC()->session ) {
Expand All @@ -2296,57 +2295,43 @@ public function payment_complete( $transaction_id = '' ) {
$valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', array( 'on-hold', 'pending', 'failed', 'cancelled' ), $this );

if ( $this->id && $this->has_status( $valid_order_statuses ) ) {

$order_needs_processing = true;
$order_needs_processing = false;

if ( sizeof( $this->get_items() ) > 0 ) {

foreach ( $this->get_items() as $item ) {
if ( $_product = $this->get_product_from_item( $item ) ) {
$virtual_downloadable_item = $_product->is_downloadable() && $_product->is_virtual();

if ( $item['product_id'] > 0 ) {

$_product = $this->get_product_from_item( $item );

if ( false !== $_product && ! apply_filters( 'woocommerce_order_item_needs_processing', ! ( $_product->is_downloadable() && $_product->is_virtual() ), $_product, $this->id ) ) {
$order_needs_processing = false;
continue;
if ( apply_filters( 'woocommerce_order_item_needs_processing', ! $virtual_downloadable_item, $_product, $this->id ) ) {
$order_needs_processing = true;
break;
}
}

$order_needs_processing = true;
break;
}
}

$new_order_status = $order_needs_processing ? 'processing' : 'completed';

$new_order_status = apply_filters( 'woocommerce_payment_complete_order_status', $new_order_status, $this->id );

$this->update_status( $new_order_status );
$this->update_status( apply_filters( 'woocommerce_payment_complete_order_status', $order_needs_processing ? 'processing' : 'completed', $this->id ) );

add_post_meta( $this->id, '_paid_date', current_time('mysql'), true );
add_post_meta( $this->id, '_paid_date', current_time( 'mysql' ), true );

if ( ! empty( $transaction_id ) ) {
add_post_meta( $this->id, '_transaction_id', $transaction_id, true );
}

$this_order = array(
'ID' => $this->id,
'post_date' => current_time( 'mysql', 0 ),
wp_update_post( array(
'ID' => $this->id,
'post_date' => current_time( 'mysql', 0 ),
'post_date_gmt' => current_time( 'mysql', 1 )
);
wp_update_post( $this_order );
) );

if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', true, $this->id ) ) {
$this->reduce_order_stock(); // Payment is complete so reduce stock levels
// Payment is complete so reduce stock levels
if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', ! get_post_meta( $this->id, '_order_stock_reduced', true ), $this->id ) ) {
$this->reduce_order_stock();
}

do_action( 'woocommerce_payment_complete', $this->id );

} else {

do_action( 'woocommerce_payment_complete_order_status_' . $this->get_status(), $this->id );

}
}

Expand All @@ -2355,8 +2340,7 @@ public function payment_complete( $transaction_id = '' ) {
* Record sales
*/
public function record_product_sales() {

if ( 'yes' == get_post_meta( $this->id, '_recorded_sales', true ) ) {
if ( 'yes' === get_post_meta( $this->id, '_recorded_sales', true ) ) {
return;
}

Expand Down Expand Up @@ -2401,7 +2385,6 @@ public function get_used_coupons() {
* Increase applied coupon counts
*/
public function increase_coupon_usage_counts() {

if ( 'yes' == get_post_meta( $this->id, '_recorded_coupon_usage_counts', true ) ) {
return;
}
Expand Down Expand Up @@ -2484,6 +2467,8 @@ public function reduce_order_stock() {
}
}

add_post_meta( $this->id, '_order_stock_reduced', '1', true );

do_action( 'woocommerce_reduce_order_stock', $this );
}
}
Expand Down

0 comments on commit 0164584

Please sign in to comment.