AI
1 year ago
Agentic
7 months ago
AbstractAddressSchema.php
1 year ago
AbstractSchema.php
1 month ago
BatchSchema.php
2 years ago
BillingAddressSchema.php
2 years ago
CartCouponSchema.php
1 year ago
CartExtensionsSchema.php
1 year ago
CartFeeSchema.php
2 years ago
CartItemSchema.php
1 month ago
CartSchema.php
2 months ago
CartShippingRateSchema.php
1 month ago
CheckoutOrderSchema.php
2 years ago
CheckoutSchema.php
1 month ago
ErrorSchema.php
2 years ago
ImageAttachmentSchema.php
3 months ago
ItemSchema.php
11 months ago
OrderCouponSchema.php
2 years ago
OrderFeeSchema.php
2 years ago
OrderItemSchema.php
1 month ago
OrderSchema.php
2 months ago
PatternsSchema.php
1 year ago
ProductAttributeSchema.php
2 years ago
ProductAttributeTermSchema.php
1 month ago
ProductBrandSchema.php
1 year ago
ProductCategorySchema.php
2 years ago
ProductCollectionDataSchema.php
11 months ago
ProductReviewSchema.php
2 years ago
ProductSchema.php
1 month ago
ShippingAddressSchema.php
2 years ago
ShopperListItemSchema.php
1 month ago
ShopperListSchema.php
1 month ago
TermSchema.php
2 years ago
CartCouponSchema.php
107 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Utilities\CartController; |
| 5 | |
| 6 | /** |
| 7 | * CartCouponSchema class. |
| 8 | */ |
| 9 | class CartCouponSchema extends AbstractSchema { |
| 10 | /** |
| 11 | * The schema item name. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $title = 'cart_coupon'; |
| 16 | |
| 17 | /** |
| 18 | * The schema item identifier. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | const IDENTIFIER = 'cart-coupon'; |
| 23 | |
| 24 | /** |
| 25 | * Cart schema properties. |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | public function get_properties() { |
| 30 | return [ |
| 31 | 'code' => [ |
| 32 | 'description' => __( 'The coupon\'s unique code.', 'woocommerce' ), |
| 33 | 'type' => 'string', |
| 34 | 'context' => [ 'view', 'edit' ], |
| 35 | 'arg_options' => [ |
| 36 | 'sanitize_callback' => 'wc_format_coupon_code', |
| 37 | 'validate_callback' => [ $this, 'coupon_exists' ], |
| 38 | ], |
| 39 | ], |
| 40 | 'discount_type' => [ |
| 41 | 'description' => __( 'The discount type for the coupon (e.g. percentage or fixed amount)', 'woocommerce' ), |
| 42 | 'type' => 'string', |
| 43 | 'context' => [ 'view', 'edit' ], |
| 44 | 'arg_options' => [ |
| 45 | 'validate_callback' => [ $this, 'coupon_exists' ], |
| 46 | ], |
| 47 | ], |
| 48 | 'totals' => [ |
| 49 | 'description' => __( 'Total amounts provided using the smallest unit of the currency.', 'woocommerce' ), |
| 50 | 'type' => 'object', |
| 51 | 'context' => [ 'view', 'edit' ], |
| 52 | 'readonly' => true, |
| 53 | 'properties' => array_merge( |
| 54 | $this->get_store_currency_properties(), |
| 55 | [ |
| 56 | 'total_discount' => [ |
| 57 | 'description' => __( 'Total discount applied by this coupon.', 'woocommerce' ), |
| 58 | 'type' => 'string', |
| 59 | 'context' => [ 'view', 'edit' ], |
| 60 | 'readonly' => true, |
| 61 | ], |
| 62 | 'total_discount_tax' => [ |
| 63 | 'description' => __( 'Total tax removed due to discount applied by this coupon.', 'woocommerce' ), |
| 64 | 'type' => 'string', |
| 65 | 'context' => [ 'view', 'edit' ], |
| 66 | 'readonly' => true, |
| 67 | ], |
| 68 | ] |
| 69 | ), |
| 70 | ], |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Check given coupon exists. |
| 76 | * |
| 77 | * @param string $coupon_code Coupon code. |
| 78 | * @return bool |
| 79 | */ |
| 80 | public function coupon_exists( $coupon_code ) { |
| 81 | $coupon = new \WC_Coupon( $coupon_code ); |
| 82 | return (bool) $coupon->get_id() || $coupon->get_virtual(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Generate a response from passed coupon code. |
| 87 | * |
| 88 | * @param string $coupon_code Coupon code from the cart. |
| 89 | * @return array |
| 90 | */ |
| 91 | public function get_item_response( $coupon_code ) { |
| 92 | $controller = new CartController(); |
| 93 | $cart = $controller->get_cart_instance(); |
| 94 | $coupon = new \WC_Coupon( $coupon_code ); |
| 95 | return [ |
| 96 | 'code' => $coupon->get_code(), |
| 97 | 'discount_type' => $coupon->get_discount_type(), |
| 98 | 'totals' => (object) $this->prepare_currency_response( |
| 99 | [ |
| 100 | 'total_discount' => $this->prepare_money_response( $cart->get_coupon_discount_amount( $coupon_code ), wc_get_price_decimals() ), |
| 101 | 'total_discount_tax' => $this->prepare_money_response( $cart->get_coupon_discount_tax_amount( $coupon_code ), wc_get_price_decimals(), PHP_ROUND_HALF_DOWN ), |
| 102 | ] |
| 103 | ), |
| 104 | ]; |
| 105 | } |
| 106 | } |
| 107 |