set( 'storeapi.cart_extension', static fn( Container $c ): CartExtension => new CartExtension( $c->get( 'extra.service' ), $c->get( 'context.factory' ), $c->get( 'registry.types' ), $c->get( 'goals.service' ), $c->get( 'pricing.tax' ) ) ); $c->set( 'storeapi.update_callback', static fn( Container $c ): UpdateCallback => new UpdateCallback( $c->get( 'extra.service' ), $c->get( 'context.factory' ) ) ); $c->set( 'storeapi.cart_validation', static fn( Container $c ): CartValidation => new CartValidation( $c->get( 'extra.service' ), $c->get( 'context.factory' ) ) ); } /** * {@inheritDoc} * * @param Container $c Container. */ public function boot( Container $c ): void { $register = static function () use ( $c ): void { $c->get( 'storeapi.cart_extension' )->register(); $c->get( 'storeapi.update_callback' )->register(); }; if ( did_action( 'woocommerce_blocks_loaded' ) ) { $register(); } else { add_action( 'woocommerce_blocks_loaded', $register ); } add_filter( 'rest_request_before_callbacks', static fn( $response, $handler, $request ) => $c->get( 'storeapi.cart_validation' )->track_request( $response, $handler, $request ), 10, 3 ); add_action( 'woocommerce_store_api_cart_errors', static function ( $errors, $cart ) use ( $c ): void { if ( $errors instanceof \WP_Error && $cart instanceof \WC_Cart ) { $c->get( 'storeapi.cart_validation' )->validate( $errors, $cart ); } }, 10, 2 ); } }