APIv1 > Orders Controller > Avoid deprecated means of accessing coupon data (#44741)

Update the (REST API v1) orders controller to use the currently supported means of accessing coupon information.

This avoids the generation of deprecation notices, which are problematic for some users who cannot yet update to API v2 or higher.

Fixes #39006
This commit is contained in:
Barry Hughes 2024-02-20 13:08:04 -08:00 committed by GitHub
parent ddbbccf5a3
commit 171043f079
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 3 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Updates REST API V1 (though it generally should no longer be used) to prevent the emission of various deprecation notices.

View File

@ -321,9 +321,9 @@ class WC_REST_Orders_V1_Controller extends WC_REST_Posts_Controller {
foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) {
$coupon_line = array(
'id' => $coupon_item_id,
'code' => $coupon_item['name'],
'discount' => wc_format_decimal( $coupon_item['discount_amount'], $dp ),
'discount_tax' => wc_format_decimal( $coupon_item['discount_amount_tax'], $dp ),
'code' => $coupon_item->get_name(),
'discount' => wc_format_decimal( $coupon_item->get_discount(), $dp ),
'discount_tax' => wc_format_decimal( $coupon_item->get_discount_tax(), $dp ),
);
$data['coupon_lines'][] = $coupon_line;

View File

@ -0,0 +1,44 @@
<?php
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
/**
* Tests relating to WC_REST_Product_Reviews_V1_Controller.
*/
class WC_REST_Orders_V1_Controller_Tests extends WC_Unit_Test_Case {
/**
* Test that an order can be fetched via REST API V1 without triggering a deprecation notice.
*
* @see https://github.com/woocommerce/woocommerce/issues/39006
*
* @return void
*/
public function test_orders_with_coupons_can_be_fetched(): void {
// Create a legacy order (APIv1 does not work with HPOS).
if ( get_option( CustomOrdersTableController::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION ) !== 'no' ) {
$this->markTestSkipped( 'This test only runs when HPOS is not enabled.' );
}
// Create an order and apply a coupon.
CouponHelper::create_coupon( 'savebig' );
$coupon_line_item = new WC_Order_Item_Coupon();
$coupon_line_item->set_code( 'savebig' );
$order = OrderHelper::create_order();
$order->add_item( $coupon_line_item );
$order->save();
$api_request = new WP_REST_Request( 'GET', '/wc/v1/orders/' . $order->get_id() );
$controller = new WC_REST_Orders_V1_Controller();
$api_response = $controller->prepare_item_for_response( get_post( $order->get_id() ), $api_request );
$this->assertInstanceOf(
WP_REST_Response::class,
$api_response,
'API response was generated successfully, and without triggering a deprecation notice.'
);
}
}