Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/StoreApi/Utilitie...
File: QuantityLimits.php
<?php
[0] Fix | Delete
declare( strict_types = 1 );
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\StoreApi\Utilities;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\Checkout\Helpers\ReserveStock;
[5] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait;
[6] Fix | Delete
use Automattic\WooCommerce\Utilities\NumberUtil;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* QuantityLimits class.
[10] Fix | Delete
*
[11] Fix | Delete
* Returns limits for products and cart items when using the StoreAPI and supporting classes.
[12] Fix | Delete
*/
[13] Fix | Delete
final class QuantityLimits {
[14] Fix | Delete
use DraftOrderTrait;
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Get quantity limits (min, max, step/multiple) for a product or cart item.
[18] Fix | Delete
*
[19] Fix | Delete
* @param array $cart_item A cart item array.
[20] Fix | Delete
* @return array
[21] Fix | Delete
*/
[22] Fix | Delete
public function get_cart_item_quantity_limits( $cart_item ) {
[23] Fix | Delete
$product = $cart_item['data'] ?? false;
[24] Fix | Delete
[25] Fix | Delete
if ( ! $product instanceof \WC_Product ) {
[26] Fix | Delete
return [
[27] Fix | Delete
'minimum' => 1,
[28] Fix | Delete
'maximum' => 9999,
[29] Fix | Delete
'multiple_of' => 1,
[30] Fix | Delete
'editable' => true,
[31] Fix | Delete
];
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
return array_merge(
[35] Fix | Delete
$this->get_add_to_cart_limits( $product, $cart_item ),
[36] Fix | Delete
[
[37] Fix | Delete
'editable' => $this->filter_boolean_value( ! $product->is_sold_individually(), 'editable', $product, $cart_item ),
[38] Fix | Delete
]
[39] Fix | Delete
);
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Get limits for product add to cart forms.
[44] Fix | Delete
*
[45] Fix | Delete
* @param \WC_Product $product Product instance.
[46] Fix | Delete
* @param array|null $cart_item Optional cart item associated with the product.
[47] Fix | Delete
* @return array
[48] Fix | Delete
*/
[49] Fix | Delete
public function get_add_to_cart_limits( \WC_Product $product, $cart_item = null ) {
[50] Fix | Delete
// Compatibility with the woocommerce_quantity_input_args filter. Gets initial values to match classic quantity input.
[51] Fix | Delete
$args = wc_get_quantity_input_args( [], $product );
[52] Fix | Delete
$minimum = $this->filter_numeric_value( $args['min_value'], 'minimum', $product, $cart_item );
[53] Fix | Delete
$maximum = $this->filter_numeric_value(
[54] Fix | Delete
$this->adjust_product_quantity_limit( $args['max_value'], $product, $cart_item ),
[55] Fix | Delete
'maximum',
[56] Fix | Delete
$product,
[57] Fix | Delete
$cart_item
[58] Fix | Delete
);
[59] Fix | Delete
$multiple_of = $this->filter_numeric_value( $args['step'], 'multiple_of', $product, $cart_item );
[60] Fix | Delete
[61] Fix | Delete
// Ensure values are compatible with each other.
[62] Fix | Delete
$minimum = max( $multiple_of, $this->limit_to_multiple( $minimum, $multiple_of, 'ceil' ) );
[63] Fix | Delete
$maximum = max( $minimum, $this->limit_to_multiple( $maximum, $multiple_of, 'floor' ) );
[64] Fix | Delete
[65] Fix | Delete
return [
[66] Fix | Delete
'minimum' => $minimum,
[67] Fix | Delete
'maximum' => $maximum,
[68] Fix | Delete
'multiple_of' => $multiple_of,
[69] Fix | Delete
];
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Fix a quantity violation by adjusting it to the nearest valid quantity.
[74] Fix | Delete
*
[75] Fix | Delete
* @param int|float $quantity Quantity.
[76] Fix | Delete
* @param array $cart_item Cart item.
[77] Fix | Delete
* @return int|float
[78] Fix | Delete
*/
[79] Fix | Delete
public function normalize_cart_item_quantity( $quantity, array $cart_item ) {
[80] Fix | Delete
$product = $cart_item['data'] ?? false;
[81] Fix | Delete
[82] Fix | Delete
if ( ! $product instanceof \WC_Product ) {
[83] Fix | Delete
return wc_stock_amount( $quantity );
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
$quantity = NumberUtil::normalize( $quantity );
[87] Fix | Delete
[88] Fix | Delete
if ( 0 >= $quantity ) {
[89] Fix | Delete
return wc_stock_amount( 0 );
[90] Fix | Delete
}
[91] Fix | Delete
[92] Fix | Delete
$limits = $this->get_cart_item_quantity_limits( $cart_item );
[93] Fix | Delete
$new_quantity = $this->limit_to_multiple( $quantity, $limits['multiple_of'], 'round' );
[94] Fix | Delete
[95] Fix | Delete
if ( $new_quantity < $limits['minimum'] ) {
[96] Fix | Delete
$new_quantity = $limits['minimum'];
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
if ( $new_quantity > $limits['maximum'] ) {
[100] Fix | Delete
$new_quantity = $limits['maximum'];
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
return wc_stock_amount( $new_quantity );
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* Return a number using the closest multiple of another number. Used to enforce step/multiple values.
[108] Fix | Delete
*
[109] Fix | Delete
* @param int|float $number Number to round.
[110] Fix | Delete
* @param int|float $multiple_of The multiple.
[111] Fix | Delete
* @param string $rounding_function ceil, floor, or round.
[112] Fix | Delete
* @return int|float
[113] Fix | Delete
*/
[114] Fix | Delete
public function limit_to_multiple( $number, $multiple_of, string $rounding_function = 'round' ) {
[115] Fix | Delete
// Handle edge cases.
[116] Fix | Delete
$number = NumberUtil::normalize( $number, null );
[117] Fix | Delete
$multiple_of = NumberUtil::normalize( $multiple_of, null );
[118] Fix | Delete
[119] Fix | Delete
if ( is_null( $multiple_of ) || is_null( $number ) ) {
[120] Fix | Delete
return 0;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
if ( 0 >= $multiple_of || $this->is_multiple_of( $number, $multiple_of ) ) {
[124] Fix | Delete
return $number;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
// Ensure valid rounding function.
[128] Fix | Delete
$rounding_function = in_array( $rounding_function, [ 'ceil', 'floor', 'round' ], true ) ? $rounding_function : 'round';
[129] Fix | Delete
[130] Fix | Delete
return NumberUtil::normalize( $rounding_function( $number / $multiple_of ) * $multiple_of );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Checks if a number is a multiple of another number.
[135] Fix | Delete
*
[136] Fix | Delete
* @param int|float $number The number to check.
[137] Fix | Delete
* @param int|float $multiple_of The multiple.
[138] Fix | Delete
* @return bool
[139] Fix | Delete
*/
[140] Fix | Delete
protected function is_multiple_of( $number, $multiple_of ) {
[141] Fix | Delete
if ( 0 >= $multiple_of ) {
[142] Fix | Delete
return false;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
$division_result = $number / $multiple_of;
[146] Fix | Delete
// Use tolerance for floating-point comparison to handle precision errors.
[147] Fix | Delete
// Example: 0.3 / 0.1 = 2.9999999999999996 instead of exactly 3.0 due to floating-point precision.
[148] Fix | Delete
return abs( $division_result - round( $division_result ) ) < 0.0001;
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* Check that a given quantity is valid according to any limits in place.
[153] Fix | Delete
*
[154] Fix | Delete
* @param int|float $quantity Quantity to validate.
[155] Fix | Delete
* @param array $cart_item Cart item.
[156] Fix | Delete
* @return \WP_Error|true
[157] Fix | Delete
*/
[158] Fix | Delete
public function validate_cart_item_quantity( $quantity, $cart_item ) {
[159] Fix | Delete
$limits = $this->get_cart_item_quantity_limits( $cart_item );
[160] Fix | Delete
$product = $cart_item['data'] ?? false;
[161] Fix | Delete
$quantity = wc_stock_amount( $quantity );
[162] Fix | Delete
[163] Fix | Delete
if ( ! $product instanceof \WC_Product ) {
[164] Fix | Delete
return true;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
if ( ! $limits['editable'] && $quantity > $limits['maximum'] ) {
[168] Fix | Delete
/* translators: 1: product name */
[169] Fix | Delete
return new \WP_Error( 'readonly_quantity', sprintf( __( 'The quantity of &quot;%1$s&quot; cannot be changed', 'woocommerce' ), $product->get_name() ) );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
if ( $quantity < $limits['minimum'] ) {
[173] Fix | Delete
/* translators: 1: product name 2: minimum quantity */
[174] Fix | Delete
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'] ) );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
if ( $quantity > $limits['maximum'] ) {
[178] Fix | Delete
/* translators: 1: product name 2: maximum quantity */
[179] Fix | Delete
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'] ) );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
if ( ! $this->is_multiple_of( $quantity, NumberUtil::normalize( $limits['multiple_of'] ) ) ) {
[183] Fix | Delete
/* translators: 1: product name 2: multiple of */
[184] Fix | Delete
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'] ) );
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
return true;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
/**
[191] Fix | Delete
* Get the limit for the total number of a product allowed in the cart.
[192] Fix | Delete
*
[193] Fix | Delete
* This is based on product properties, including remaining stock, and defaults to a maximum of 9999 of any product
[194] Fix | Delete
* in the cart at once.
[195] Fix | Delete
*
[196] Fix | Delete
* @param int|float $purchase_limit The purchase limit from the product. Usually maps to `get_max_purchase_quantity`.
[197] Fix | Delete
* @param \WC_Product $product Product instance.
[198] Fix | Delete
* @param array|null $cart_item Optional cart item associated with the product.
[199] Fix | Delete
* @return int|float
[200] Fix | Delete
*/
[201] Fix | Delete
protected function adjust_product_quantity_limit( $purchase_limit, \WC_Product $product, $cart_item = null ) {
[202] Fix | Delete
$limits = [ $purchase_limit > 0 ? $purchase_limit : 9999 ];
[203] Fix | Delete
[204] Fix | Delete
// If managing stock and backorders are not allowed, get the remaining stock considering active carts.
[205] Fix | Delete
if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
[206] Fix | Delete
$limits[] = $this->get_remaining_stock( $product );
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
return $this->filter_numeric_value( min( array_filter( $limits ) ), 'limit', $product, $cart_item );
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
/**
[213] Fix | Delete
* Returns the remaining stock for a product if it has stock.
[214] Fix | Delete
*
[215] Fix | Delete
* This also factors in draft orders.
[216] Fix | Delete
*
[217] Fix | Delete
* @param \WC_Product $product Product instance.
[218] Fix | Delete
* @return int|float|null
[219] Fix | Delete
*/
[220] Fix | Delete
protected function get_remaining_stock( \WC_Product $product ) {
[221] Fix | Delete
if ( is_null( $product->get_stock_quantity() ) ) {
[222] Fix | Delete
return null;
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
$reserve_stock = new ReserveStock();
[226] Fix | Delete
$reserved_stock = $reserve_stock->get_reserved_stock( $product, $this->get_draft_order_id() );
[227] Fix | Delete
[228] Fix | Delete
return wc_stock_amount( $product->get_stock_quantity() - $reserved_stock );
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* Get a numeric value while running it through a filter hook.
[233] Fix | Delete
*
[234] Fix | Delete
* @param int|float $value Value to filter.
[235] Fix | Delete
* @param string $value_type Type of value. Used for filter suffix.
[236] Fix | Delete
* @param \WC_Product $product Product instance.
[237] Fix | Delete
* @param array|null $cart_item Optional cart item associated with the product.
[238] Fix | Delete
* @return int|float
[239] Fix | Delete
*/
[240] Fix | Delete
protected function filter_numeric_value( $value, string $value_type, \WC_Product $product, $cart_item = null ) {
[241] Fix | Delete
/**
[242] Fix | Delete
* Filters a quantity for a cart item in Store API. This allows extensions to control the qty of items.
[243] Fix | Delete
*
[244] Fix | Delete
* The suffix of the hook will vary depending on the value being filtered.
[245] Fix | Delete
* For example, minimum, maximum, multiple_of, editable.
[246] Fix | Delete
*
[247] Fix | Delete
* @since 6.8.0
[248] Fix | Delete
*
[249] Fix | Delete
* @param mixed $value The value being filtered.
[250] Fix | Delete
* @param \WC_Product $product The product object.
[251] Fix | Delete
* @param array|null $cart_item The cart item if the product exists in the cart, or null.
[252] Fix | Delete
* @return mixed
[253] Fix | Delete
*/
[254] Fix | Delete
$filtered_value = apply_filters( 'woocommerce_store_api_product_quantity_' . $value_type, $value, $product, $cart_item );
[255] Fix | Delete
[256] Fix | Delete
return wc_stock_amount( NumberUtil::normalize( $filtered_value, $value ) );
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
/**
[260] Fix | Delete
* Get a boolean value while running it through a filter hook.
[261] Fix | Delete
*
[262] Fix | Delete
* @param bool $value Value to filter.
[263] Fix | Delete
* @param string $value_type Type of value. Used for filter suffix.
[264] Fix | Delete
* @param \WC_Product $product Product instance.
[265] Fix | Delete
* @param array|null $cart_item Optional cart item associated with the product.
[266] Fix | Delete
* @return bool
[267] Fix | Delete
*/
[268] Fix | Delete
protected function filter_boolean_value( $value, string $value_type, \WC_Product $product, $cart_item = null ) {
[269] Fix | Delete
[270] Fix | Delete
/**
[271] Fix | Delete
* Filters boolean data for a cart item in Store API.
[272] Fix | Delete
*
[273] Fix | Delete
* The suffix of the hook will vary depending on the value being filtered. For example, editable.
[274] Fix | Delete
*
[275] Fix | Delete
* @since 6.8.0
[276] Fix | Delete
*
[277] Fix | Delete
* @param mixed $value The value being filtered.
[278] Fix | Delete
* @param \WC_Product $product The product object.
[279] Fix | Delete
* @param array|null $cart_item The cart item if the product exists in the cart, or null.
[280] Fix | Delete
* @return mixed
[281] Fix | Delete
*/
[282] Fix | Delete
$filtered_value = apply_filters( 'woocommerce_store_api_product_quantity_' . $value_type, $value, $product, $cart_item );
[283] Fix | Delete
[284] Fix | Delete
return is_bool( $filtered_value ) ? $filtered_value : (bool) $value;
[285] Fix | Delete
}
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function