Commit 8841e46945b for woocommerce
commit 8841e46945b7ab13a4f21df89b0da6d4682593ba
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date: Fri Dec 4 20:14:07 2026 +0000
Let extensions add fields to coupon abilities (#71347)
* Register coupon as a resource of the extensible abilities
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Add the coupon resource to the prime test
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---------
Co-authored-by: zaguiini <luis.felipe.zaguini@automattic.com>
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
Co-authored-by: Luis Felipe Zaguini <luisfelipezaguini@gmail.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/add-coupon-extensible-abilities-resource b/plugins/woocommerce/changelog/add-coupon-extensible-abilities-resource
new file mode 100644
index 00000000000..2e614d53612
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-coupon-extensible-abilities-resource
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Coupon abilities: return and filter by the coupon fields that extensions add, with the extensible abilities feature on.
diff --git a/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php b/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php
index 2bb87c56626..3cdd7c23a61 100644
--- a/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php
+++ b/plugins/woocommerce/src/Internal/Abilities/AbilitiesLoader.php
@@ -232,6 +232,7 @@ class AbilitiesLoader {
$coupon->delete_meta_cache();
clean_post_cache( $id );
},
+ 'query_clauses' => array( 'meta_query', 'include_ids', 'exclude_ids' ),
)
);
ExtensibleAbilities::register_resource(
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponCreate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponCreate.php
index 285970f832f..54462b8ac2f 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponCreate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponCreate.php
@@ -93,10 +93,11 @@ class CouponCreate extends ActionableAbility {
* Return saved coupon fields.
*
* @param \WC_Coupon $subject Coupon.
- * @return array
+ * @return array|\WP_Error
*/
- protected function prepare_response( $subject ): array {
- return array( 'coupon' => self::format_coupon_for_response( new \WC_Coupon( $subject ) ) );
+ protected function prepare_response( $subject ) {
+ $coupon = self::format_coupon_for_response( new \WC_Coupon( $subject ) );
+ return is_wp_error( $coupon ) ? $coupon : array( 'coupon' => $coupon );
}
/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
index 2afedc7807e..12c1411a0fa 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponUpdate.php
@@ -144,11 +144,12 @@ class CouponUpdate extends ActionableAbility {
* Return saved coupon fields.
*
* @param \WC_Coupon $subject Coupon.
- * @return array
+ * @return array|\WP_Error
*/
- protected function prepare_response( $subject ): array {
+ protected function prepare_response( $subject ) {
// Reload because saving can retain removed array entries in the in-memory object.
- return array( 'coupon' => self::format_coupon_for_response( new \WC_Coupon( $subject ) ) );
+ $coupon = self::format_coupon_for_response( new \WC_Coupon( $subject ) );
+ return is_wp_error( $coupon ) ? $coupon : array( 'coupon' => $coupon );
}
/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponsQuery.php
index 241f7dc41d6..d7bb277615d 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/CouponsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/CouponsQuery.php
@@ -8,6 +8,7 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\Abilities\Domain;
use Automattic\WooCommerce\Abilities\AbilityDefinition;
+use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
use Automattic\WooCommerce\Internal\Abilities\Domain\Traits\CouponAbilityTrait;
defined( 'ABSPATH' ) || exit;
@@ -107,6 +108,27 @@ class CouponsQuery extends AbstractDomainAbility implements AbilityDefinition {
),
);
}
+
+ $extension_args = ExtensibleAbilities::get_query_args( 'coupon', $input );
+ if ( is_wp_error( $extension_args ) ) {
+ return $extension_args;
+ }
+ $excluded = $extension_args['exclude_ids'] ?? array();
+ $include = $args['post__in'] ?? $extension_args['include_ids'] ?? null;
+ if ( isset( $args['post__in'], $extension_args['include_ids'] ) ) {
+ $include = array_intersect( $include, $extension_args['include_ids'] );
+ }
+ if ( null !== $include ) {
+ // WP_Query ignores post__not_in next to post__in, and an empty post__in does not filter.
+ $include = array_values( array_diff( $include, $excluded ) );
+ $args['post__in'] = empty( $include ) ? array( 0 ) : $include;
+ } elseif ( ! empty( $excluded ) ) {
+ $args['post__not_in'] = $excluded;
+ }
+ if ( isset( $extension_args['meta_query'] ) ) {
+ $args['meta_query'] = array_merge( $args['meta_query'] ?? array(), $extension_args['meta_query'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- The filters of extensions read coupon meta.
+ }
+
$args['orderby'] = array(
'date' => 'DESC',
'ID' => 'DESC',
@@ -121,13 +143,24 @@ class CouponsQuery extends AbstractDomainAbility implements AbilityDefinition {
$total = (int) $count_query->found_posts;
}
+ $coupons = array_map(
+ static function ( $coupon_id ) {
+ return new \WC_Coupon( $coupon_id instanceof \WP_Post ? $coupon_id->ID : $coupon_id );
+ },
+ $query->posts
+ );
+ ExtensibleAbilities::prime( 'coupon', $coupons );
+ $formatted = array();
+ foreach ( $coupons as $coupon ) {
+ $data = self::format_coupon_for_response( $coupon );
+ if ( is_wp_error( $data ) ) {
+ return $data;
+ }
+ $formatted[] = $data;
+ }
+
return array(
- 'coupons' => array_map(
- static function ( $coupon_id ) {
- return self::format_coupon_for_response( new \WC_Coupon( $coupon_id instanceof \WP_Post ? $coupon_id->ID : $coupon_id ) );
- },
- $query->posts
- ),
+ 'coupons' => $formatted,
'total' => $total,
'total_pages' => (int) ceil( $total / $per_page ),
'page' => $page,
@@ -153,7 +186,7 @@ class CouponsQuery extends AbstractDomainAbility implements AbilityDefinition {
* @return array
*/
private static function get_input_schema(): array {
- return array(
+ $schema = array(
'type' => 'object',
'properties' => array(
'include' => array(
@@ -196,5 +229,6 @@ class CouponsQuery extends AbstractDomainAbility implements AbilityDefinition {
'additionalProperties' => false,
'default' => array(),
);
+ return ExtensibleAbilities::add_query_filters_schema( $schema, 'coupon' );
}
}
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php
index 664c57a0dd7..bddb48af4fe 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/CouponAbilityTrait.php
@@ -7,6 +7,8 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Internal\Abilities\Domain\Traits;
+use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
+
defined( 'ABSPATH' ) || exit;
/**
@@ -24,12 +26,12 @@ trait CouponAbilityTrait {
}
/**
- * Get the schema for a single coupon in a response.
+ * Get the schema for a single coupon in a response, with the fields extensions register on coupons.
*
* @return array
*/
protected static function get_coupon_output_schema(): array {
- return array(
+ $schema = array(
'type' => 'object',
'properties' => array(
'id' => array( 'type' => 'integer' ),
@@ -105,6 +107,7 @@ trait CouponAbilityTrait {
),
'additionalProperties' => false,
);
+ return ExtensibleAbilities::add_fields_schema( $schema, 'coupon' );
}
/**
@@ -311,9 +314,9 @@ trait CouponAbilityTrait {
* Format a coupon for ability output.
*
* @param \WC_Coupon $coupon Coupon object.
- * @return array
+ * @return array|\WP_Error The error of an extension field that failed.
*/
- protected static function format_coupon_for_response( \WC_Coupon $coupon ): array {
+ protected static function format_coupon_for_response( \WC_Coupon $coupon ) {
$usage_limit = $coupon->get_usage_limit();
$usage_limit_per_user = $coupon->get_usage_limit_per_user();
@@ -371,11 +374,12 @@ trait CouponAbilityTrait {
$output['expiry']['last_valid_day'] = $local->modify( '-1 day' )->format( 'Y-m-d' );
}
}
- return array_filter(
+ $output = array_filter(
$output,
static function ( $value ) {
return null !== $value;
}
);
+ return ExtensibleAbilities::add_fields_to_object( $output, 'coupon', $coupon );
}
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php
index 1ce3c63327b..126eb9c5b5c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/CouponsQueryTest.php
@@ -8,6 +8,7 @@ declare( strict_types=1 );
namespace Automattic\WooCommerce\Tests\Internal\Abilities\Domain;
use Automattic\WooCommerce\Abilities\ExtensibleAbilities;
+use Automattic\WooCommerce\Internal\Abilities\Domain\CouponCreate;
use Automattic\WooCommerce\Internal\Abilities\Domain\CouponsQuery;
use Automattic\WooCommerce\Internal\Abilities\Domain\CouponUpdate;
use Automattic\WooCommerce\Tests\Internal\Abilities\ExtensibleAbilitiesTestTrait;
@@ -713,4 +714,125 @@ class CouponsQueryTest extends \WC_Unit_Test_Case {
$this->assertSame( 'saved-real-coupon', get_post_field( 'post_title', $saved_id ) );
$this->assertSame( '15', get_post_meta( $saved_id, 'coupon_amount', true ) );
}
+
+ /**
+ * @testdox Should read, write, validate, sync and filter extension fields on coupons while the extensible abilities feature is on.
+ */
+ public function test_extension_fields_and_filters(): void {
+ wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
+ $synced = array();
+ $this->enable_extensible_abilities(
+ static function () use ( &$synced ) {
+ ExtensibleAbilities::register_field(
+ array(
+ 'resource' => 'coupon',
+ 'namespace' => 'test-ext',
+ 'field' => 'credit',
+ 'output_schema' => array( 'type' => 'integer' ),
+ 'get_callback' => static fn( \WC_Coupon $coupon ) => '' === $coupon->get_meta( 'test_credit' ) ? null : $coupon->get_meta( 'test_credit' ),
+ 'input_schema' => array( 'type' => 'integer' ),
+ 'set_callback' => static fn( \WC_Coupon $coupon, int $value ) => $coupon->update_meta_data( 'test_credit', $value ),
+ )
+ );
+ ExtensibleAbilities::register_validator(
+ array(
+ 'resource' => 'coupon',
+ 'namespace' => 'test-ext',
+ 'callback' => static fn( \WC_Coupon $coupon ) => (int) $coupon->get_meta( 'test_credit' ) > 100 ? new \WP_Error( 'test_credit_too_high', 'Too much credit.' ) : null,
+ )
+ );
+ ExtensibleAbilities::register_sync(
+ array(
+ 'resource' => 'coupon',
+ 'namespace' => 'test-ext',
+ 'callback' => static function ( array $objects ) use ( &$synced ) {
+ $synced[] = $objects[0]->get_id();
+ },
+ )
+ );
+ $filters = array(
+ 'has_credit' => static fn( bool $value ) => array(
+ 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
+ array(
+ 'key' => 'test_credit',
+ 'compare' => $value ? 'EXISTS' : 'NOT EXISTS',
+ ),
+ ),
+ ),
+ 'only' => static fn( array $ids ) => array( 'include_ids' => $ids ),
+ 'not' => static fn( array $ids ) => array( 'exclude_ids' => $ids ),
+ );
+ foreach ( $filters as $filter => $callback ) {
+ ExtensibleAbilities::register_query_filter(
+ array(
+ 'resource' => 'coupon',
+ 'namespace' => 'test-ext',
+ 'filter' => $filter,
+ 'schema' => 'has_credit' === $filter ? array( 'type' => 'boolean' ) : array(
+ 'type' => 'array',
+ 'items' => array( 'type' => 'integer' ),
+ ),
+ 'callback' => $callback,
+ )
+ );
+ }
+ }
+ );
+ $create = new CouponCreate( CouponCreate::ABILITY_NAME, CouponCreate::get_registration_args() );
+ $update = new CouponUpdate( CouponUpdate::ABILITY_NAME, CouponUpdate::get_registration_args() );
+ $query = new \WP_Ability( CouponsQuery::get_name(), CouponsQuery::get_registration_args() );
+
+ $result = $create->execute(
+ array(
+ 'code' => 'credit-coupon',
+ 'extensions' => array( 'test-ext' => array( 'credit' => 50 ) ),
+ )
+ );
+ $this->assertNotWPError( $result );
+ $id = $result['coupon']['id'];
+ $this->assertSame( 50, $result['coupon']['extensions']['test-ext']['credit'] );
+ $this->assertSame( array( $id ), $synced );
+
+ $refused = $update->execute(
+ array(
+ 'id' => $id,
+ 'extensions' => array( 'test-ext' => array( 'credit' => 500 ) ),
+ )
+ );
+ $this->assertSame( 'test-ext:test_credit_too_high', $refused->get_error_code() );
+ $this->assertSame( '50', ( new \WC_Coupon( $id ) )->get_meta( 'test_credit' ), 'A refused update saves nothing.' );
+
+ $result = $update->execute(
+ array(
+ 'id' => $id,
+ 'extensions' => array( 'test-ext' => array( 'credit' => 75 ) ),
+ )
+ );
+ $this->assertSame( 75, $result['coupon']['extensions']['test-ext']['credit'] );
+
+ $plain = new \WC_Coupon();
+ $plain->set_code( 'plain-coupon' );
+ $plain_id = $plain->save();
+ $other = new \WC_Coupon();
+ $other->set_code( 'other-coupon' );
+ $other_id = $other->save();
+ $ids = static fn( array $filters, array $input = array() ) => array_column( $query->execute( $input + array( 'filters' => array( 'extensions' => array( 'test-ext' => $filters ) ) ) )['coupons'], 'id' );
+
+ $result = $query->execute( array( 'filters' => array( 'extensions' => array( 'test-ext' => array( 'has_credit' => true ) ) ) ) );
+ $this->assertSame( array( $id ), array_column( $result['coupons'], 'id' ) );
+ $this->assertSame( 75, $result['coupons'][0]['extensions']['test-ext']['credit'] );
+ $this->assertSame( 'integer', $query->get_output_schema()['properties']['coupons']['items']['properties']['extensions']['properties']['test-ext']['properties']['credit']['type'] );
+ $this->assertSame(
+ array( $plain_id ),
+ $ids(
+ array(
+ 'has_credit' => false,
+ 'not' => array( $other_id ),
+ )
+ )
+ );
+ $this->assertSame( array(), $ids( array( 'has_credit' => false ), array( 'include' => array( $id ) ) ), 'An extension filter applies together with include.' );
+ $this->assertSame( array( $plain_id ), $ids( array( 'only' => array( $plain_id, $other_id ) ), array( 'include' => array( $id, $plain_id ) ) ), 'The include of a filter only narrows the request include.' );
+ $this->assertSame( array(), $ids( array( 'not' => array( $id ) ), array( 'include' => array( $id ) ) ), 'The exclude of a filter applies to the request include.' );
+ }
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index b34ce428bca..f4628cd447b 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -1306,6 +1306,20 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
return array( 'product_ids' => array( $product ) );
},
),
+ 'coupon' => array(
+ 'coupon',
+ 'woocommerce/coupons-query',
+ 'coupons',
+ static function (): array {
+ $ids = array();
+ foreach ( array( 'prime-one', 'prime-two' ) as $code ) {
+ $coupon = new \WC_Coupon();
+ $coupon->set_code( $code );
+ $ids[] = $coupon->save();
+ }
+ return array( 'include' => $ids );
+ },
+ ),
'order_refund' => array(
'order_refund',
'woocommerce/order-refunds-query',
@@ -1336,7 +1350,7 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
$resources = new \ReflectionProperty( ExtensibleAbilities::class, 'resources' );
$resources->setAccessible( true );
- $covered = array_merge( array_keys( $this->provide_list_abilities() ), array( 'order', 'order_item', 'coupon' ) );
+ $covered = array_merge( array_keys( $this->provide_list_abilities() ), array( 'order', 'order_item' ) );
$this->assertEqualsCanonicalizing( array_keys( $resources->getValue() ), $covered, 'Add the new resource to provide_list_abilities().' );
}