/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Schemas/V1/CartExtensionsSchema.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;

use 
Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
use 
Automattic\WooCommerce\StoreApi\SchemaController;
use 
Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
use 
Automattic\WooCommerce\StoreApi\Utilities\CartController;

/**
 * Class CartExtensionsSchema
 */
class CartExtensionsSchema extends AbstractSchema {
    
/**
     * The schema item name.
     *
     * @var string
     */
    
protected $title 'cart-extensions';

    
/**
     * The schema item identifier.
     *
     * @var string
     */
    
const IDENTIFIER 'cart-extensions';

    
/**
     * Cart schema instance.
     *
     * @var CartSchema
     */
    
public $cart_schema;

    
/**
     * Constructor.
     *
     * @param ExtendSchema     $extend Rest Extending instance.
     * @param SchemaController $controller Schema Controller instance.
     */
    
public function __constructExtendSchema $extendSchemaController $controller ) {
        
parent::__construct$extend$controller );
        
$this->cart_schema $this->controller->getCartSchema::IDENTIFIER );
    }

    
/**
     * Cart extensions schema properties.
     *
     * @return array
     */
    
public function get_properties() {
        return [];
    }

    
/**
     * Handle the request and return a valid response for this endpoint.
     *
     * @param \WP_REST_Request $request Request containing data for the extension callback.
     * @throws RouteException When callback is not callable or parameters are incorrect.
     *
     * @return array
     */
    
public function get_item_response$request null ) {
        try {
            
$callback $this->extend->get_update_callback$request['namespace'] );
        } catch ( 
\Exception $e ) {
            throw new 
RouteException(
                
'woocommerce_rest_cart_extensions_error',
                
esc_html$e->getMessage() ),
                
400
            
);
        }

        
// Run the callback. Exceptions are not caught here.
        
$callback$request['data'] );

        try {
            
// We recalculate the cart if we had something to run.
            
$controller = new CartController();
            
$cart       $controller->calculate_totals();
            
$response   $this->cart_schema->get_item_response$cart );

            return 
rest_ensure_response$response );
        } catch ( 
\Exception $e ) {
            throw new 
RouteException(
                
'woocommerce_rest_cart_extensions_error',
                
esc_html$e->getMessage() ),
                
400
            
);
        }
    }
}