| Server IP : 146.59.209.152 / Your IP : 216.73.216.152 Web Server : Apache System : Linux webm009.cluster131.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64 User : monpetu ( 144298) PHP Version : 7.4.33 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/monpetu/www/ipprint/wp-content/plugins/woocommerce/src/Api/Queries/Coupons/ |
Upload File : |
<?php
declare(strict_types=1);
namespace Automattic\WooCommerce\Api\Queries\Coupons;
use Automattic\WooCommerce\Api\Attributes\Description;
use Automattic\WooCommerce\Api\Attributes\Name;
use Automattic\WooCommerce\Api\Attributes\RequiredCapability;
use Automattic\WooCommerce\Api\Types\Coupons\Coupon;
use Automattic\WooCommerce\Api\Utils\Coupons\CouponMapper;
#[Name( 'coupon' )]
#[Description( 'Retrieve a single coupon by ID or code. Exactly one of the two arguments must be provided.' )]
/**
* Query to retrieve a single coupon.
*/
#[RequiredCapability( 'read_private_shop_coupons' )]
class GetCoupon {
/**
* Retrieve a coupon by ID or code.
*
* @param ?int $id The coupon ID.
* @param ?string $code The coupon code.
* @return ?Coupon
* @throws \InvalidArgumentException When neither or both arguments are provided.
*/
public function execute(
#[Description( 'The ID of the coupon to retrieve.' )]
?int $id = null,
#[Description( 'The coupon code to look up.' )]
?string $code = null,
): ?Coupon {
if ( ( null === $id ) === ( null === $code ) ) {
throw new \InvalidArgumentException( 'Exactly one of "id" or "code" must be provided.' );
}
$wc_coupon = new \WC_Coupon( $id ?? $code );
if ( ! $wc_coupon->get_id() ) {
return null;
}
return CouponMapper::from_wc_coupon( $wc_coupon );
}
}