Initialize _restock_refunded_items meta on order update
This commit is contained in:
parent
2e3156d436
commit
8c3e90b3ce
|
@ -218,15 +218,9 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
|
|||
$order = $item->get_order();
|
||||
$order_version = $order->get_version();
|
||||
$refunded_item_quantity = $order->get_qty_refunded_for_item( $item->get_id() );
|
||||
$_new_reduced_stock = $item_quantity - $restock_refunded_items;
|
||||
|
||||
$diff = $item_quantity - $restock_refunded_items - $already_reduced_stock;
|
||||
|
||||
if ( version_compare( $order_version, '5.4', '<' ) ) {
|
||||
$diff = $item_quantity + $refunded_item_quantity - $already_reduced_stock;
|
||||
$_new_reduced_stock = $item_quantity + $refunded_item_quantity;
|
||||
}
|
||||
|
||||
/*
|
||||
* 0 as $item_quantity usually indicates we're deleting the order item.
|
||||
* Let's restore back the reduced count.
|
||||
|
@ -247,7 +241,7 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
|
|||
return $new_stock;
|
||||
}
|
||||
|
||||
$item->update_meta_data( '_reduced_stock', $_new_reduced_stock );
|
||||
$item->update_meta_data( '_reduced_stock', $item_quantity - $restock_refunded_items );
|
||||
$item->save();
|
||||
|
||||
if ( $item_quantity > 0 ) {
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use Automattic\WooCommerce\Internal\DownloadPermissionsAdjuster;
|
||||
use Automattic\WooCommerce\Internal\RestockRefundedItemsAdjuster;
|
||||
use Automattic\WooCommerce\Internal\AssignDefaultCategory;
|
||||
use Automattic\WooCommerce\Proxies\LegacyProxy;
|
||||
|
||||
|
@ -209,6 +210,7 @@ final class WooCommerce {
|
|||
// These classes set up hooks on instantiation.
|
||||
wc_get_container()->get( DownloadPermissionsAdjuster::class );
|
||||
wc_get_container()->get( AssignDefaultCategory::class );
|
||||
wc_get_container()->get( RestockRefundedItemsAdjuster::class );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -8,6 +8,7 @@ namespace Automattic\WooCommerce;
|
|||
use Automattic\WooCommerce\Internal\DependencyManagement\ExtendedContainer;
|
||||
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\DownloadPermissionsAdjusterServiceProvider;
|
||||
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\AssignDefaultCategoryServiceProvider;
|
||||
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\RestockRefundedItemsAdjusterServiceProvider;
|
||||
use Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders\ProxiesServiceProvider;
|
||||
|
||||
/**
|
||||
|
@ -37,6 +38,7 @@ final class Container implements \Psr\Container\ContainerInterface {
|
|||
ProxiesServiceProvider::class,
|
||||
DownloadPermissionsAdjusterServiceProvider::class,
|
||||
AssignDefaultCategoryServiceProvider::class,
|
||||
RestockRefundedItemsAdjusterServiceProvider::class,
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* RestockRefundedItemsAdjusterServiceProvider class file.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Internal\DependencyManagement\ServiceProviders;
|
||||
|
||||
use Automattic\WooCommerce\Internal\DependencyManagement\AbstractServiceProvider;
|
||||
use Automattic\WooCommerce\Internal\RestockRefundedItemsAdjuster;
|
||||
|
||||
/**
|
||||
* Service provider for the RestockRefundedItemsAdjuster class.
|
||||
*/
|
||||
class RestockRefundedItemsAdjusterServiceProvider extends AbstractServiceProvider {
|
||||
|
||||
/**
|
||||
* The classes/interfaces that are serviced by this service provider.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = array(
|
||||
RestockRefundedItemsAdjuster::class,
|
||||
);
|
||||
|
||||
/**
|
||||
* Register the classes.
|
||||
*/
|
||||
public function register() {
|
||||
$this->share( RestockRefundedItemsAdjuster::class );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
/**
|
||||
* RestockRefundedItemsAdjuster class file.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Internal;
|
||||
|
||||
use Automattic\WooCommerce\Proxies\LegacyProxy;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class to adjust or initialize the restock refunded items.
|
||||
*/
|
||||
class RestockRefundedItemsAdjuster {
|
||||
/**
|
||||
* The order factory to use.
|
||||
*
|
||||
* @var WC_Order_Factory
|
||||
*/
|
||||
private $order_factory;
|
||||
|
||||
/**
|
||||
* Class initialization, to be executed when the class is resolved by the container.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final public function init() {
|
||||
$this->order_factory = wc_get_container()->get( LegacyProxy::class )->get_instance_of( \WC_Order_Factory::class );
|
||||
add_action( 'woocommerce_before_save_order_items', array( $this, 'initialize_restock_refunded_items' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the restock refunded items meta for order version less than 5.4.
|
||||
*
|
||||
* @see https://github.com/woocommerce/woocommerce/issues/29502
|
||||
*
|
||||
* @param int $order_id Order ID.
|
||||
* @param array $items Order items to save.
|
||||
*/
|
||||
public function initialize_restock_refunded_items( $order_id, $items ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
$order_version = $order->get_version();
|
||||
|
||||
if ( version_compare( $order_version, '5.4', '>=' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $items['order_item_id'] ) ) {
|
||||
foreach ( $items['order_item_id'] as $item_id ) {
|
||||
$item = $this->order_factory::get_order_item( absint( $item_id ) );
|
||||
|
||||
if ( ! $item ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'line_item' !== $item->get_type() ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$refunded_item_quantity = abs( $order->get_qty_refunded_for_item( $item->get_id() ) );
|
||||
$item->add_meta_data( '_restock_refunded_items', $refunded_item_quantity, false );
|
||||
$item->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -289,7 +289,6 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test adjust line item function when order item is refunded with restock and then update order.
|
||||
* This only works with orders placed after WC 5.4.
|
||||
*
|
||||
* @link https://github.com/woocommerce/woocommerce/issues/29502.
|
||||
*/
|
||||
|
@ -303,10 +302,6 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
|
|||
|
||||
$order = WC_Helper_Order::create_order();
|
||||
|
||||
if ( version_compare( $order->get_version(), '5.4', '<' ) ) {
|
||||
$this->markTestSkipped( 'Requires WC 5.4+' );
|
||||
}
|
||||
|
||||
$order->set_status( 'on-hold' );
|
||||
$order_item_id = $order->add_product( $product, 10 );
|
||||
$order_item = new WC_Order_Item_Product( $order_item_id );
|
||||
|
@ -353,7 +348,6 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
|
|||
|
||||
/**
|
||||
* Test adjust line item function when order item is refunded without restock and then update order.
|
||||
* This only works with orders placed after WC 5.4.
|
||||
*
|
||||
* @link https://github.com/woocommerce/woocommerce/issues/29502.
|
||||
*/
|
||||
|
@ -367,10 +361,6 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
|
|||
|
||||
$order = WC_Helper_Order::create_order();
|
||||
|
||||
if ( version_compare( $order->get_version(), '5.4', '<' ) ) {
|
||||
$this->markTestSkipped( 'Requires WC 5.4+' );
|
||||
}
|
||||
|
||||
$order->set_status( 'on-hold' );
|
||||
$order_item_id = $order->add_product( $product, 10 );
|
||||
$order_item = new WC_Order_Item_Product( $order_item_id );
|
||||
|
|
Loading…
Reference in New Issue