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
CartExtensionsSchema.php
92 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Schemas\V1; |
| 3 | |
| 4 | use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; |
| 5 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 6 | use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema; |
| 7 | use Automattic\WooCommerce\StoreApi\Utilities\CartController; |
| 8 | |
| 9 | /** |
| 10 | * Class CartExtensionsSchema |
| 11 | */ |
| 12 | class CartExtensionsSchema extends AbstractSchema { |
| 13 | /** |
| 14 | * The schema item name. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $title = 'cart-extensions'; |
| 19 | |
| 20 | /** |
| 21 | * The schema item identifier. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | const IDENTIFIER = 'cart-extensions'; |
| 26 | |
| 27 | /** |
| 28 | * Cart schema instance. |
| 29 | * |
| 30 | * @var CartSchema |
| 31 | */ |
| 32 | public $cart_schema; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | * |
| 37 | * @param ExtendSchema $extend Rest Extending instance. |
| 38 | * @param SchemaController $controller Schema Controller instance. |
| 39 | */ |
| 40 | public function __construct( ExtendSchema $extend, SchemaController $controller ) { |
| 41 | parent::__construct( $extend, $controller ); |
| 42 | $this->cart_schema = $this->controller->get( CartSchema::IDENTIFIER ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Cart extensions schema properties. |
| 47 | * |
| 48 | * @return array |
| 49 | */ |
| 50 | public function get_properties() { |
| 51 | return []; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Handle the request and return a valid response for this endpoint. |
| 56 | * |
| 57 | * @param \WP_REST_Request $request Request containing data for the extension callback. |
| 58 | * @throws RouteException When callback is not callable or parameters are incorrect. |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | public function get_item_response( $request = null ) { |
| 63 | try { |
| 64 | $callback = $this->extend->get_update_callback( $request['namespace'] ); |
| 65 | } catch ( \Exception $e ) { |
| 66 | throw new RouteException( |
| 67 | 'woocommerce_rest_cart_extensions_error', |
| 68 | esc_html( $e->getMessage() ), |
| 69 | 400 |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | // Run the callback. Exceptions are not caught here. |
| 74 | $callback( $request['data'] ); |
| 75 | |
| 76 | try { |
| 77 | // We recalculate the cart if we had something to run. |
| 78 | $controller = new CartController(); |
| 79 | $cart = $controller->calculate_totals(); |
| 80 | $response = $this->cart_schema->get_item_response( $cart ); |
| 81 | |
| 82 | return rest_ensure_response( $response ); |
| 83 | } catch ( \Exception $e ) { |
| 84 | throw new RouteException( |
| 85 | 'woocommerce_rest_cart_extensions_error', |
| 86 | esc_html( $e->getMessage() ), |
| 87 | 400 |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 |