/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Utilities/QuantityLimits.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
declare( strict_types );

namespace 
Automattic\WooCommerce\StoreApi\Utilities;

use 
Automattic\WooCommerce\Checkout\Helpers\ReserveStock;
use 
Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait;

/**
 * QuantityLimits class.
 *
 * Returns limits for products and cart items when using the StoreAPI and supporting classes.
 */
final class QuantityLimits {
    use 
DraftOrderTrait;

    
/**
     * Get quantity limits (min, max, step/multiple) for a product or cart item.
     *
     * @param array $cart_item A cart item array.
     * @return array
     */
    
public function get_cart_item_quantity_limits$cart_item ) {
        
$product $cart_item['data'] ?? false;

        if ( ! 
$product instanceof \WC_Product ) {
            return [
                
'minimum'     => 1,
                
'maximum'     => 9999,
                
'multiple_of' => 1,
                
'editable'    => true,
            ];
        }

        
$multiple_of $this->filter_numeric_value1'multiple_of'$cart_item );
        
$minimum     $this->filter_numeric_value1'minimum'$cart_item );
        
$maximum     $this->filter_numeric_value$this->get_product_quantity_limit$product ), 'maximum'$cart_item );
        
$editable    $this->filter_boolean_value( ! $product->is_sold_individually(), 'editable'$cart_item );

        
// Minimum must be at least 1.
        
$minimum max$minimum);

        
// Maximum must be at least minimum.
        
$maximum max$maximum$minimum );

        return [
            
'minimum'     => $this->limit_to_multiple$minimum$multiple_of'ceil' ),
            
'maximum'     => $this->limit_to_multiple$maximum$multiple_of'floor' ),
            
'multiple_of' => $multiple_of,
            
'editable'    => $editable,
        ];
    }

    
/**
     * Get limits for product add to cart forms.
     *
     * @param \WC_Product $product Product instance.
     * @return array
     */
    
public function get_add_to_cart_limits\WC_Product $product ) {
        
$multiple_of $this->filter_numeric_value1'multiple_of'$product );
        
$minimum     $this->filter_numeric_value1'minimum'$product );
        
$maximum     $this->filter_numeric_value$this->get_product_quantity_limit$product ), 'maximum'$product );

        
// Minimum must be at least 1.
        
$minimum max$minimum);

        
// Maximum must be at least minimum.
        
$maximum max$maximum$minimum );

        return [
            
'minimum'     => $this->limit_to_multiple$minimum$multiple_of'ceil' ),
            
'maximum'     => $this->limit_to_multiple$maximum$multiple_of'floor' ),
            
'multiple_of' => $multiple_of,
        ];
    }

    
/**
     * Fix a quantity violation by adjusting it to the nearest valid quantity.
     *
     * @param int   $quantity The quantity to fix.
     * @param array $cart_item The cart item.
     * @return int
     */
    
public function normalize_cart_item_quantityint $quantity, array $cart_item ) {
        
$product $cart_item['data'] ?? false;

        if ( ! 
$product instanceof \WC_Product ) {
            return 
$quantity;
        }

        
$limits       $this->get_cart_item_quantity_limits$cart_item );
        
$new_quantity $quantity;

        if ( 
$new_quantity $limits['multiple_of'] ) {
            
$new_quantity $this->limit_to_multiple$new_quantity$limits['multiple_of'], 'round' );
        }

        if ( 
$new_quantity $limits['minimum'] ) {
            
$new_quantity $limits['minimum'];
        }

        if ( 
$new_quantity $limits['maximum'] ) {
            
$new_quantity $limits['maximum'];
        }

        return 
$new_quantity;
    }

    
/**
     * Return a number using the closest multiple of another number. Used to enforce step/multiple values.
     *
     * @param int    $number Number to round.
     * @param int    $multiple_of The multiple.
     * @param string $rounding_function ceil, floor, or round.
     * @return int
     */
    
public function limit_to_multipleint $numberint $multiple_ofstring $rounding_function 'round' ) {
        if ( 
$multiple_of <= ) {
            return 
$number;
        }
        
$rounding_function in_array$rounding_function, [ 'ceil''floor''round' ], true ) ? $rounding_function 'round';
        return 
$rounding_function$number $multiple_of ) * $multiple_of;
    }

    
/**
     * Check that a given quantity is valid according to any limits in place.
     *
     * @param integer $quantity Quantity to validate.
     * @param array   $cart_item Cart item.
     * @return \WP_Error|true
     */
    
public function validate_cart_item_quantity$quantity$cart_item ) {
        
$limits  $this->get_cart_item_quantity_limits$cart_item );
        
$product $cart_item['data'] ?? false;

        if ( ! 
$product instanceof \WC_Product ) {
            return 
true;
        }

        if ( ! 
$limits['editable'] ) {
            
/* translators: 1: product name */
            
return new \WP_Error'readonly_quantity'sprintf__'The quantity of &quot;%1$s&quot; cannot be changed''woocommerce' ), $product->get_name() ) );
        }

        if ( 
$quantity $limits['minimum'] ) {
            
/* translators: 1: product name 2: minimum quantity */
            
return new \WP_Error'invalid_quantity'sprintf__'The minimum quantity of &quot;%1$s&quot; allowed in the cart is %2$s''woocommerce' ), $product->get_name(), $limits['minimum'] ) );
        }

        if ( 
$quantity $limits['maximum'] ) {
            
/* translators: 1: product name 2: maximum quantity */
            
return new \WP_Error'invalid_quantity'sprintf__'The maximum quantity of &quot;%1$s&quot; allowed in the cart is %2$s''woocommerce' ), $product->get_name(), $limits['maximum'] ) );
        }

        if ( 
$quantity $limits['multiple_of'] ) {
            
/* translators: 1: product name 2: multiple of */
            
return new \WP_Error'invalid_quantity'sprintf__'The quantity of &quot;%1$s&quot; must be a multiple of %2$s''woocommerce' ), $product->get_name(), $limits['multiple_of'] ) );
        }

        return 
true;
    }

    
/**
     * Get the limit for the total number of a product allowed in the cart.
     *
     * This is based on product properties, including remaining stock, and defaults to a maximum of 9999 of any product
     * in the cart at once.
     *
     * @param \WC_Product $product Product instance.
     * @return int
     */
    
protected function get_product_quantity_limit\WC_Product $product ) {
        
$limits = [ 9999 ];

        if ( 
$product->is_sold_individually() ) {
            
$limits[] = 1;
        } elseif ( ! 
$product->backorders_allowed() ) {
            
$limits[] = $this->get_remaining_stock$product );
        }

        
$limit maxminarray_filter$limits ) ), );

        
/**
         * Filters the quantity limit for a product being added to the cart via the Store API.
         *
         * Filters the variation option name for custom option slugs.
         *
         * @since 6.8.0
         *
         * @param integer $quantity_limit Quantity limit which defaults to 9999 unless sold individually.
         * @param \WC_Product $product Product instance.
         * @return integer
         */
        
$filtered_limit apply_filters'woocommerce_store_api_product_quantity_limit'$limit$product );

        
// Only return the filtered limit if it's numeric, otherwise return the original limit.
        
return is_numeric$filtered_limit ) ? (int) $filtered_limit $limit;
    }

    
/**
     * Returns the remaining stock for a product if it has stock.
     *
     * This also factors in draft orders.
     *
     * @param \WC_Product $product Product instance.
     * @return integer|null
     */
    
protected function get_remaining_stock\WC_Product $product ) {
        if ( 
is_null$product->get_stock_quantity() ) ) {
            return 
null;
        }

        
$reserve_stock  = new ReserveStock();
        
$reserved_stock $reserve_stock->get_reserved_stock$product$this->get_draft_order_id() );

        return 
$product->get_stock_quantity() - $reserved_stock;
    }

    
/**
     * Get a quantity for a product or cart item by running it through a filter hook.
     *
     * @param int               $value Value to filter.
     * @param string            $value_type Type of value. Used for filter suffix.
     * @param \WC_Product|array $cart_item_or_product Either a cart item or a product instance.
     * @return int
     */
    
protected function filter_numeric_valueint $valuestring $value_type$cart_item_or_product ) {
        
$is_product $cart_item_or_product instanceof \WC_Product;
        
$product    $is_product $cart_item_or_product $cart_item_or_product['data'];
        
$cart_item  $is_product null $cart_item_or_product;

        
/**
         * Filters the quantity minimum for a cart item in Store API. This allows extensions to control the minimum qty
         * of items already within the cart.
         *
         * The suffix of the hook will vary depending on the value being filtered.
         * For example, minimum, maximum, multiple_of, editable.
         *
         * @since 6.8.0
         *
         * @param mixed $value The value being filtered.
         * @param \WC_Product $product The product object.
         * @param array|null $cart_item The cart item if the product exists in the cart, or null.
         * @return mixed
         */
        
$filtered_value apply_filters"woocommerce_store_api_product_quantity_{$value_type}"$value$product$cart_item );

        return 
is_numeric$filtered_value ) ? (int) $filtered_value $value;
    }

    
/**
     * Get a quantity for a product or cart item by running it through a filter hook.
     *
     * @param bool              $value Value to filter.
     * @param string            $value_type Type of value. Used for filter suffix.
     * @param \WC_Product|array $cart_item_or_product Either a cart item or a product instance.
     * @return bool
     */
    
protected function filter_boolean_value$valuestring $value_type$cart_item_or_product ) {
        
$is_product $cart_item_or_product instanceof \WC_Product;
        
$product    $is_product $cart_item_or_product $cart_item_or_product['data'];
        
$cart_item  $is_product null $cart_item_or_product;

        
/**
         * Filters the quantity minimum for a cart item in Store API. This allows extensions to control the minimum qty
         * of items already within the cart.
         *
         * The suffix of the hook will vary depending on the value being filtered.
         * For example, minimum, maximum, multiple_of, editable.
         *
         * @since 6.8.0
         *
         * @param mixed $value The value being filtered.
         * @param \WC_Product $product The product object.
         * @param array|null $cart_item The cart item if the product exists in the cart, or null.
         * @return mixed
         */
        
$filtered_value apply_filters"woocommerce_store_api_product_quantity_{$value_type}"$value$product$cart_item );

        return 
is_bool$filtered_value ) ? (bool) $filtered_value $value;
    }
}