/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Schemas/V1/CartCouponSchema.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;

use 
Automattic\WooCommerce\StoreApi\Utilities\CartController;

/**
 * CartCouponSchema class.
 */
class CartCouponSchema extends AbstractSchema {
    
/**
     * The schema item name.
     *
     * @var string
     */
    
protected $title 'cart_coupon';

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

    
/**
     * Cart schema properties.
     *
     * @return array
     */
    
public function get_properties() {
        return [
            
'code'          => [
                
'description' => __'The coupon\'s unique code.''woocommerce' ),
                
'type'        => 'string',
                
'context'     => [ 'view''edit' ],
                
'arg_options' => [
                    
'sanitize_callback' => 'wc_format_coupon_code',
                    
'validate_callback' => [ $this'coupon_exists' ],
                ],
            ],
            
'discount_type' => [
                
'description' => __'The discount type for the coupon (e.g. percentage or fixed amount)''woocommerce' ),
                
'type'        => 'string',
                
'context'     => [ 'view''edit' ],
                
'arg_options' => [
                    
'validate_callback' => [ $this'coupon_exists' ],
                ],
            ],
            
'totals'        => [
                
'description' => __'Total amounts provided using the smallest unit of the currency.''woocommerce' ),
                
'type'        => 'object',
                
'context'     => [ 'view''edit' ],
                
'readonly'    => true,
                
'properties'  => array_merge(
                    
$this->get_store_currency_properties(),
                    [
                        
'total_discount'     => [
                            
'description' => __'Total discount applied by this coupon.''woocommerce' ),
                            
'type'        => 'string',
                            
'context'     => [ 'view''edit' ],
                            
'readonly'    => true,
                        ],
                        
'total_discount_tax' => [
                            
'description' => __'Total tax removed due to discount applied by this coupon.''woocommerce' ),
                            
'type'        => 'string',
                            
'context'     => [ 'view''edit' ],
                            
'readonly'    => true,
                        ],
                    ]
                ),
            ],
        ];
    }

    
/**
     * Check given coupon exists.
     *
     * @param string $coupon_code Coupon code.
     * @return bool
     */
    
public function coupon_exists$coupon_code ) {
        
$coupon = new \WC_Coupon$coupon_code );
        return (bool) 
$coupon->get_id() || $coupon->get_virtual();
    }

    
/**
     * Generate a response from passed coupon code.
     *
     * @param string $coupon_code Coupon code from the cart.
     * @return array
     */
    
public function get_item_response$coupon_code ) {
        
$controller           = new CartController();
        
$cart                 $controller->get_cart_instance();
        
$total_discounts      $cart->get_coupon_discount_totals();
        
$total_discount_taxes $cart->get_coupon_discount_tax_totals();
        
$coupon               = new \WC_Coupon$coupon_code );
        return [
            
'code'          => $coupon_code,
            
'discount_type' => $coupon->get_discount_type(),
            
'totals'        => (object) $this->prepare_currency_response(
                [
                    
'total_discount'     => $this->prepare_money_response( isset( $total_discounts$coupon_code ] ) ? $total_discounts$coupon_code ] : 0wc_get_price_decimals() ),
                    
'total_discount_tax' => $this->prepare_money_response( isset( $total_discount_taxes$coupon_code ] ) ? $total_discount_taxes$coupon_code ] : 0wc_get_price_decimals(), PHP_ROUND_HALF_DOWN ),
                ]
            ),
        ];
    }
}