Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/StoreApi/Utilitie...
File: CartController.php
<?php
[0] Fix | Delete
namespace Automattic\WooCommerce\StoreApi\Utilities;
[1] Fix | Delete
[2] Fix | Delete
use Automattic\WooCommerce\Checkout\Helpers\ReserveStock;
[3] Fix | Delete
use Automattic\WooCommerce\Enums\ProductStatus;
[4] Fix | Delete
use Automattic\WooCommerce\Enums\ProductType;
[5] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\InvalidCartException;
[6] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\NotPurchasableException;
[7] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\OutOfStockException;
[8] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\PartialOutOfStockException;
[9] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
[10] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\TooManyInCartException;
[11] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\ArrayUtils;
[12] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\DraftOrderTrait;
[13] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\NoticeHandler;
[14] Fix | Delete
use Automattic\WooCommerce\StoreApi\Utilities\QuantityLimits;
[15] Fix | Delete
use WP_Error;
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Woo Cart Controller class.
[19] Fix | Delete
*
[20] Fix | Delete
* Helper class to bridge the gap between the cart API and Woo core.
[21] Fix | Delete
*/
[22] Fix | Delete
class CartController {
[23] Fix | Delete
use DraftOrderTrait;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Makes the cart and sessions available to a route by loading them from core.
[27] Fix | Delete
*/
[28] Fix | Delete
public function load_cart() {
[29] Fix | Delete
if ( ! did_action( 'woocommerce_load_cart_from_session' ) ) {
[30] Fix | Delete
// Initialize the cart.
[31] Fix | Delete
wc_load_cart();
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
// Load cart from session.
[35] Fix | Delete
$cart = $this->get_cart_instance();
[36] Fix | Delete
$cart->cart_context = 'store-api';
[37] Fix | Delete
$cart->get_cart();
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Normalizes the cart by fixing any quantity violations.
[42] Fix | Delete
*/
[43] Fix | Delete
public function normalize_cart() {
[44] Fix | Delete
$quantity_limits = new QuantityLimits();
[45] Fix | Delete
$cart_items = $this->get_cart_items();
[46] Fix | Delete
[47] Fix | Delete
foreach ( $cart_items as $cart_item ) {
[48] Fix | Delete
$normalized_qty = $quantity_limits->normalize_cart_item_quantity( $cart_item['quantity'], $cart_item );
[49] Fix | Delete
[50] Fix | Delete
if ( $normalized_qty !== $cart_item['quantity'] ) {
[51] Fix | Delete
try {
[52] Fix | Delete
$this->set_cart_item_quantity( $cart_item['key'], $normalized_qty );
[53] Fix | Delete
} catch ( RouteException $e ) {
[54] Fix | Delete
// Ignore errors and continue.
[55] Fix | Delete
continue;
[56] Fix | Delete
}
[57] Fix | Delete
}
[58] Fix | Delete
}
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Gets the latest cart instance, and ensures totals have been calculated before returning.
[63] Fix | Delete
*
[64] Fix | Delete
* @return \WC_Cart
[65] Fix | Delete
*/
[66] Fix | Delete
public function get_cart_for_response() {
[67] Fix | Delete
return did_action( 'woocommerce_after_calculate_totals' ) ? $this->get_cart_instance() : $this->calculate_totals();
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Recalculates the cart totals and returns the updated cart instance.
[72] Fix | Delete
*
[73] Fix | Delete
* @since 9.2.0 Calculate shipping was removed here because it's called already by calculate_totals.
[74] Fix | Delete
*
[75] Fix | Delete
* @return \WC_Cart
[76] Fix | Delete
*/
[77] Fix | Delete
public function calculate_totals() {
[78] Fix | Delete
$cart = $this->get_cart_instance();
[79] Fix | Delete
$cart->get_cart();
[80] Fix | Delete
$cart->calculate_fees();
[81] Fix | Delete
$cart->calculate_totals();
[82] Fix | Delete
return $cart;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Based on the core cart class but returns errors rather than rendering notices directly.
[87] Fix | Delete
*
[88] Fix | Delete
* @todo Overriding the core add_to_cart method was necessary because core outputs notices when an item is added to
[89] Fix | Delete
* the cart. For us this would cause notices to build up and output on the store, out of context. Core would need
[90] Fix | Delete
* refactoring to split notices out from other cart actions.
[91] Fix | Delete
*
[92] Fix | Delete
* @throws RouteException Exception if invalid data is detected.
[93] Fix | Delete
*
[94] Fix | Delete
* @param array $request Add to cart request params.
[95] Fix | Delete
* @return string
[96] Fix | Delete
*/
[97] Fix | Delete
public function add_to_cart( $request ) {
[98] Fix | Delete
$cart = $this->get_cart_instance();
[99] Fix | Delete
$request = wp_parse_args(
[100] Fix | Delete
$request,
[101] Fix | Delete
[
[102] Fix | Delete
'id' => 0,
[103] Fix | Delete
'quantity' => 1,
[104] Fix | Delete
'variation' => [],
[105] Fix | Delete
'cart_item_data' => [],
[106] Fix | Delete
]
[107] Fix | Delete
);
[108] Fix | Delete
[109] Fix | Delete
$request = $this->filter_request_data( $this->parse_variation_data( $request ) );
[110] Fix | Delete
$product = $this->get_product_for_cart( $request );
[111] Fix | Delete
$cart_id = $cart->generate_cart_id(
[112] Fix | Delete
$this->get_product_id( $product ),
[113] Fix | Delete
$this->get_variation_id( $product ),
[114] Fix | Delete
$request['variation'],
[115] Fix | Delete
$request['cart_item_data']
[116] Fix | Delete
);
[117] Fix | Delete
[118] Fix | Delete
$quantity_limits = new QuantityLimits();
[119] Fix | Delete
[120] Fix | Delete
// If quantity was not passed, it should default to the minimum allowed quantity.
[121] Fix | Delete
if ( null === $request['quantity'] ) {
[122] Fix | Delete
$request['quantity'] = $quantity_limits->get_add_to_cart_limits( $product )['minimum'];
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
$this->validate_add_to_cart( $product, $request );
[126] Fix | Delete
$existing_cart_id = $cart->find_product_in_cart( $cart_id );
[127] Fix | Delete
$request_quantity = wc_stock_amount( $request['quantity'] );
[128] Fix | Delete
[129] Fix | Delete
if ( $existing_cart_id ) {
[130] Fix | Delete
$cart_item = $cart->cart_contents[ $existing_cart_id ];
[131] Fix | Delete
$updated_quantity = $request_quantity + $cart_item['quantity'];
[132] Fix | Delete
$quantity_validation = $quantity_limits->validate_cart_item_quantity( $updated_quantity, $cart_item );
[133] Fix | Delete
[134] Fix | Delete
if ( is_wp_error( $quantity_validation ) ) {
[135] Fix | Delete
throw new RouteException(
[136] Fix | Delete
esc_html( $quantity_validation->get_error_code() ),
[137] Fix | Delete
esc_html( $quantity_validation->get_error_message() ),
[138] Fix | Delete
400
[139] Fix | Delete
);
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
$cart->set_quantity( $existing_cart_id, $updated_quantity, true );
[143] Fix | Delete
[144] Fix | Delete
return $existing_cart_id;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
// Normalize quantity.
[148] Fix | Delete
$add_to_cart_limits = $quantity_limits->get_add_to_cart_limits( $product );
[149] Fix | Delete
[150] Fix | Delete
if ( $add_to_cart_limits['maximum'] ) {
[151] Fix | Delete
$request_quantity = min( $request_quantity, $add_to_cart_limits['maximum'] );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
$request_quantity = max( $request_quantity, $add_to_cart_limits['minimum'] );
[155] Fix | Delete
$request_quantity = $quantity_limits->limit_to_multiple( $request_quantity, $add_to_cart_limits['multiple_of'] );
[156] Fix | Delete
[157] Fix | Delete
/**
[158] Fix | Delete
* Filters the item being added to the cart.
[159] Fix | Delete
*
[160] Fix | Delete
* @since 2.5.0
[161] Fix | Delete
*
[162] Fix | Delete
* @internal Matches filter name in WooCommerce core.
[163] Fix | Delete
*
[164] Fix | Delete
* @param array $cart_item_data Array of cart item data being added to the cart.
[165] Fix | Delete
* @param string $cart_id Id of the item in the cart.
[166] Fix | Delete
* @return array Updated cart item data.
[167] Fix | Delete
*/
[168] Fix | Delete
$cart->cart_contents[ $cart_id ] = apply_filters(
[169] Fix | Delete
'woocommerce_add_cart_item',
[170] Fix | Delete
array_merge(
[171] Fix | Delete
$request['cart_item_data'],
[172] Fix | Delete
array(
[173] Fix | Delete
'key' => $cart_id,
[174] Fix | Delete
'product_id' => $this->get_product_id( $product ),
[175] Fix | Delete
'variation_id' => $this->get_variation_id( $product ),
[176] Fix | Delete
'variation' => $request['variation'],
[177] Fix | Delete
'quantity' => $request_quantity,
[178] Fix | Delete
'data' => $product,
[179] Fix | Delete
'data_hash' => wc_get_cart_item_data_hash( $product ),
[180] Fix | Delete
)
[181] Fix | Delete
),
[182] Fix | Delete
$cart_id
[183] Fix | Delete
);
[184] Fix | Delete
[185] Fix | Delete
/**
[186] Fix | Delete
* Filters the entire cart contents when the cart changes.
[187] Fix | Delete
*
[188] Fix | Delete
* @since 2.5.0
[189] Fix | Delete
*
[190] Fix | Delete
* @internal Matches filter name in WooCommerce core.
[191] Fix | Delete
*
[192] Fix | Delete
* @param array $cart_contents Array of all cart items.
[193] Fix | Delete
* @return array Updated array of all cart items.
[194] Fix | Delete
*/
[195] Fix | Delete
$cart->cart_contents = apply_filters( 'woocommerce_cart_contents_changed', $cart->cart_contents );
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Fires when an item is added to the cart.
[199] Fix | Delete
*
[200] Fix | Delete
* This hook fires when an item is added to the cart. This is triggered from the Store API in this context, but
[201] Fix | Delete
* WooCommerce core add to cart events trigger the same hook.
[202] Fix | Delete
*
[203] Fix | Delete
* @since 2.5.0
[204] Fix | Delete
*
[205] Fix | Delete
* @internal Matches action name in WooCommerce core.
[206] Fix | Delete
*
[207] Fix | Delete
* @param string $cart_id ID of the item in the cart.
[208] Fix | Delete
* @param integer $product_id ID of the product added to the cart.
[209] Fix | Delete
* @param integer $request_quantity Quantity of the item added to the cart.
[210] Fix | Delete
* @param integer $variation_id Variation ID of the product added to the cart.
[211] Fix | Delete
* @param array $variation Array of variation data.
[212] Fix | Delete
* @param array $cart_item_data Array of other cart item data.
[213] Fix | Delete
*/
[214] Fix | Delete
do_action(
[215] Fix | Delete
'woocommerce_add_to_cart',
[216] Fix | Delete
$cart_id,
[217] Fix | Delete
$this->get_product_id( $product ),
[218] Fix | Delete
$request_quantity,
[219] Fix | Delete
$this->get_variation_id( $product ),
[220] Fix | Delete
$request['variation'],
[221] Fix | Delete
$request['cart_item_data']
[222] Fix | Delete
);
[223] Fix | Delete
[224] Fix | Delete
return $cart_id;
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* Based on core `set_quantity` method, but validates if an item is sold individually first and enforces any limits in
[229] Fix | Delete
* place.
[230] Fix | Delete
*
[231] Fix | Delete
* @throws RouteException Exception if invalid data is detected.
[232] Fix | Delete
*
[233] Fix | Delete
* @param string $item_id Cart item id.
[234] Fix | Delete
* @param int|float $quantity Cart quantity.
[235] Fix | Delete
*/
[236] Fix | Delete
public function set_cart_item_quantity( $item_id, $quantity = 1 ) {
[237] Fix | Delete
$cart_item = $this->get_cart_item( $item_id );
[238] Fix | Delete
[239] Fix | Delete
if ( empty( $cart_item ) ) {
[240] Fix | Delete
throw new RouteException( 'woocommerce_rest_cart_invalid_key', esc_html__( 'Cart item does not exist.', 'woocommerce' ), 409 );
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
$product = $cart_item['data'] ?? false;
[244] Fix | Delete
[245] Fix | Delete
if ( ! $product instanceof \WC_Product ) {
[246] Fix | Delete
throw new RouteException( 'woocommerce_rest_cart_invalid_product', esc_html__( 'Cart item is invalid.', 'woocommerce' ), 404 );
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
$quantity_validation = ( new QuantityLimits() )->validate_cart_item_quantity( $quantity, $cart_item );
[250] Fix | Delete
[251] Fix | Delete
if ( is_wp_error( $quantity_validation ) ) {
[252] Fix | Delete
throw new RouteException( $quantity_validation->get_error_code(), $quantity_validation->get_error_message(), 400 ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
[253] Fix | Delete
}
[254] Fix | Delete
[255] Fix | Delete
$cart = $this->get_cart_instance();
[256] Fix | Delete
$cart->set_quantity( $item_id, $quantity );
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
/**
[260] Fix | Delete
* Validate all items in the cart and check for errors.
[261] Fix | Delete
*
[262] Fix | Delete
* @throws RouteException Exception if invalid data is detected.
[263] Fix | Delete
*
[264] Fix | Delete
* @param \WC_Product $product Product object associated with the cart item.
[265] Fix | Delete
* @param array $request Add to cart request params.
[266] Fix | Delete
*/
[267] Fix | Delete
public function validate_add_to_cart( \WC_Product $product, $request ) {
[268] Fix | Delete
if ( ! $product->is_purchasable() ) {
[269] Fix | Delete
$this->throw_default_product_exception( $product );
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
if ( floatval( $request['quantity'] ) <= 0 ) {
[273] Fix | Delete
throw new RouteException(
[274] Fix | Delete
'woocommerce_rest_product_invalid_quantity',
[275] Fix | Delete
sprintf(
[276] Fix | Delete
/* translators: %s: product name */
[277] Fix | Delete
esc_html__( 'You cannot add &quot;%s&quot; with a quantity less than or equal to 0 to the cart.', 'woocommerce' ),
[278] Fix | Delete
esc_html( $product->get_name() )
[279] Fix | Delete
),
[280] Fix | Delete
400
[281] Fix | Delete
);
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
if ( ! $product->is_in_stock() ) {
[285] Fix | Delete
throw new RouteException(
[286] Fix | Delete
'woocommerce_rest_product_out_of_stock',
[287] Fix | Delete
sprintf(
[288] Fix | Delete
/* translators: %s: product name */
[289] Fix | Delete
esc_html__( 'You cannot add &quot;%s&quot; to the cart because the product is out of stock.', 'woocommerce' ),
[290] Fix | Delete
$product->get_name()
[291] Fix | Delete
),
[292] Fix | Delete
400
[293] Fix | Delete
);
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
if ( $product->managing_stock() && ! $product->backorders_allowed() ) {
[297] Fix | Delete
$request_quantity = wc_stock_amount( $request['quantity'] );
[298] Fix | Delete
$qty_remaining = $this->get_remaining_stock_for_product( $product );
[299] Fix | Delete
$qty_in_cart = $this->get_product_quantity_in_cart( $product );
[300] Fix | Delete
[301] Fix | Delete
if ( $qty_remaining < $qty_in_cart + $request_quantity ) {
[302] Fix | Delete
throw new RouteException(
[303] Fix | Delete
'woocommerce_rest_product_partially_out_of_stock',
[304] Fix | Delete
sprintf(
[305] Fix | Delete
/* translators: 1: product name 2: quantity in stock */
[306] Fix | Delete
esc_html__( 'You cannot add that amount of &quot;%1$s&quot; to the cart because there is not enough stock (%2$s remaining).', 'woocommerce' ),
[307] Fix | Delete
$product->get_name(),
[308] Fix | Delete
wc_format_stock_quantity_for_display( $qty_remaining, $product )
[309] Fix | Delete
),
[310] Fix | Delete
400
[311] Fix | Delete
);
[312] Fix | Delete
}
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/**
[316] Fix | Delete
* Filters if an item being added to the cart passed validation checks.
[317] Fix | Delete
*
[318] Fix | Delete
* Allow 3rd parties to validate if an item can be added to the cart. This is a legacy hook from Woo core.
[319] Fix | Delete
* This filter will be deprecated because it encourages usage of wc_add_notice. For the API we need to capture
[320] Fix | Delete
* notices and convert to exceptions instead.
[321] Fix | Delete
*
[322] Fix | Delete
* @since 7.2.0
[323] Fix | Delete
*
[324] Fix | Delete
* @deprecated
[325] Fix | Delete
* @param boolean $passed_validation True if the item passed validation.
[326] Fix | Delete
* @param integer $product_id Product ID being validated.
[327] Fix | Delete
* @param integer $quantity Quantity added to the cart.
[328] Fix | Delete
* @param integer $variation_id Variation ID being added to the cart.
[329] Fix | Delete
* @param array $variation Variation data.
[330] Fix | Delete
* @return boolean
[331] Fix | Delete
*/
[332] Fix | Delete
$passed_validation = apply_filters(
[333] Fix | Delete
'woocommerce_add_to_cart_validation',
[334] Fix | Delete
true,
[335] Fix | Delete
$this->get_product_id( $product ),
[336] Fix | Delete
$request['quantity'],
[337] Fix | Delete
$this->get_variation_id( $product ),
[338] Fix | Delete
$request['variation'],
[339] Fix | Delete
$request['cart_item_data']
[340] Fix | Delete
);
[341] Fix | Delete
[342] Fix | Delete
if ( ! $passed_validation ) {
[343] Fix | Delete
// Validation did not pass - see if an error notice was thrown.
[344] Fix | Delete
NoticeHandler::convert_notices_to_exceptions( 'woocommerce_rest_add_to_cart_error' );
[345] Fix | Delete
[346] Fix | Delete
// If no notice was thrown, throw the default notice instead.
[347] Fix | Delete
$this->throw_default_product_exception( $product );
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Fires during validation when adding an item to the cart via the Store API.
[352] Fix | Delete
*
[353] Fix | Delete
* @param \WC_Product $product Product object being added to the cart.
[354] Fix | Delete
* @param array $request Add to cart request params including id, quantity, and variation attributes.
[355] Fix | Delete
* @deprecated 7.1.0 Use woocommerce_store_api_validate_add_to_cart instead.
[356] Fix | Delete
*/
[357] Fix | Delete
wc_do_deprecated_action(
[358] Fix | Delete
'wooocommerce_store_api_validate_add_to_cart',
[359] Fix | Delete
array(
[360] Fix | Delete
$product,
[361] Fix | Delete
$request,
[362] Fix | Delete
),
[363] Fix | Delete
'7.1.0',
[364] Fix | Delete
'woocommerce_store_api_validate_add_to_cart',
[365] Fix | Delete
'This action was deprecated in WooCommerce Blocks version 7.1.0. Please use woocommerce_store_api_validate_add_to_cart instead.'
[366] Fix | Delete
);
[367] Fix | Delete
[368] Fix | Delete
/**
[369] Fix | Delete
* Fires during validation when adding an item to the cart via the Store API.
[370] Fix | Delete
*
[371] Fix | Delete
* Fire action to validate add to cart. Functions hooking into this should throw an \Exception to prevent
[372] Fix | Delete
* add to cart from happening.
[373] Fix | Delete
*
[374] Fix | Delete
* @since 7.1.0
[375] Fix | Delete
*
[376] Fix | Delete
* @param \WC_Product $product Product object being added to the cart.
[377] Fix | Delete
* @param array $request Add to cart request params including id, quantity, and variation attributes.
[378] Fix | Delete
*/
[379] Fix | Delete
do_action( 'woocommerce_store_api_validate_add_to_cart', $product, $request );
[380] Fix | Delete
}
[381] Fix | Delete
[382] Fix | Delete
/**
[383] Fix | Delete
* Generates the error message for out of stock products and adds product names to it.
[384] Fix | Delete
*
[385] Fix | Delete
* @param string $singular The message to use when only one product is in the list.
[386] Fix | Delete
* @param string $plural The message to use when more than one product is in the list.
[387] Fix | Delete
* @param array $items The list of cart items whose names should be inserted into the message.
[388] Fix | Delete
* @returns string The translated and correctly pluralised message.
[389] Fix | Delete
*/
[390] Fix | Delete
private function add_product_names_to_message( $singular, $plural, $items ) {
[391] Fix | Delete
$product_names = wc_list_pluck( $items, 'getProductName' );
[392] Fix | Delete
$message = ( count( $items ) > 1 ) ? $plural : $singular;
[393] Fix | Delete
return sprintf(
[394] Fix | Delete
$message,
[395] Fix | Delete
ArrayUtils::natural_language_join( $product_names, true )
[396] Fix | Delete
);
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
/**
[400] Fix | Delete
* Takes a string describing the type of stock extension, whether there is a single product or multiple products
[401] Fix | Delete
* causing this exception and returns an appropriate error message.
[402] Fix | Delete
*
[403] Fix | Delete
* @param string $exception_type The type of exception encountered.
[404] Fix | Delete
* @param string $singular_or_plural Whether to get the error message for a single product or multiple.
[405] Fix | Delete
*
[406] Fix | Delete
* @return string
[407] Fix | Delete
*/
[408] Fix | Delete
private function get_error_message_for_stock_exception_type( $exception_type, $singular_or_plural ) {
[409] Fix | Delete
$stock_error_messages = [
[410] Fix | Delete
'out_of_stock' => [
[411] Fix | Delete
/* translators: %s: product name. */
[412] Fix | Delete
'singular' => esc_html__(
[413] Fix | Delete
'%s is out of stock and cannot be purchased. Please remove it from your cart.',
[414] Fix | Delete
'woocommerce'
[415] Fix | Delete
),
[416] Fix | Delete
/* translators: %s: product names. */
[417] Fix | Delete
'plural' => esc_html__(
[418] Fix | Delete
'%s are out of stock and cannot be purchased. Please remove them from your cart.',
[419] Fix | Delete
'woocommerce'
[420] Fix | Delete
),
[421] Fix | Delete
],
[422] Fix | Delete
'not_purchasable' => [
[423] Fix | Delete
/* translators: %s: product name. */
[424] Fix | Delete
'singular' => esc_html__(
[425] Fix | Delete
'%s cannot be purchased. Please remove it from your cart.',
[426] Fix | Delete
'woocommerce'
[427] Fix | Delete
),
[428] Fix | Delete
/* translators: %s: product names. */
[429] Fix | Delete
'plural' => esc_html__(
[430] Fix | Delete
'%s cannot be purchased. Please remove them from your cart.',
[431] Fix | Delete
'woocommerce'
[432] Fix | Delete
),
[433] Fix | Delete
],
[434] Fix | Delete
'too_many_in_cart' => [
[435] Fix | Delete
/* translators: %s: product names. */
[436] Fix | Delete
'singular' => esc_html__(
[437] Fix | Delete
'There are too many %s in the cart. Only 1 can be purchased. Please reduce the quantity in your cart.',
[438] Fix | Delete
'woocommerce'
[439] Fix | Delete
),
[440] Fix | Delete
/* translators: %s: product names. */
[441] Fix | Delete
'plural' => esc_html__(
[442] Fix | Delete
'There are too many %s in the cart. Only 1 of each can be purchased. Please reduce the quantities in your cart.',
[443] Fix | Delete
'woocommerce'
[444] Fix | Delete
),
[445] Fix | Delete
],
[446] Fix | Delete
'partial_out_of_stock' => [
[447] Fix | Delete
/* translators: %s: product names. */
[448] Fix | Delete
'singular' => esc_html__(
[449] Fix | Delete
'There is not enough %s in stock. Please reduce the quantity in your cart.',
[450] Fix | Delete
'woocommerce'
[451] Fix | Delete
),
[452] Fix | Delete
/* translators: %s: product names. */
[453] Fix | Delete
'plural' => esc_html__(
[454] Fix | Delete
'There are not enough %s in stock. Please reduce the quantities in your cart.',
[455] Fix | Delete
'woocommerce'
[456] Fix | Delete
),
[457] Fix | Delete
],
[458] Fix | Delete
];
[459] Fix | Delete
[460] Fix | Delete
if (
[461] Fix | Delete
isset( $stock_error_messages[ $exception_type ] ) &&
[462] Fix | Delete
isset( $stock_error_messages[ $exception_type ][ $singular_or_plural ] )
[463] Fix | Delete
) {
[464] Fix | Delete
return $stock_error_messages[ $exception_type ][ $singular_or_plural ];
[465] Fix | Delete
}
[466] Fix | Delete
[467] Fix | Delete
return esc_html__( 'There was an error with an item in your cart.', 'woocommerce' );
[468] Fix | Delete
}
[469] Fix | Delete
[470] Fix | Delete
/**
[471] Fix | Delete
* Validate cart and check for errors.
[472] Fix | Delete
*
[473] Fix | Delete
* @throws InvalidCartException Exception if invalid data is detected in the cart.
[474] Fix | Delete
*/
[475] Fix | Delete
public function validate_cart() {
[476] Fix | Delete
$this->validate_cart_items();
[477] Fix | Delete
$this->validate_cart_coupons();
[478] Fix | Delete
[479] Fix | Delete
$cart = $this->get_cart_instance();
[480] Fix | Delete
$cart_errors = new WP_Error();
[481] Fix | Delete
[482] Fix | Delete
/**
[483] Fix | Delete
* Fires an action to validate the cart.
[484] Fix | Delete
*
[485] Fix | Delete
* Functions hooking into this should add custom errors using the provided WP_Error instance.
[486] Fix | Delete
*
[487] Fix | Delete
* @since 7.2.0
[488] Fix | Delete
*
[489] Fix | Delete
* @example See docs/examples/validate-cart.md
[490] Fix | Delete
*
[491] Fix | Delete
* @param \WP_Error $errors WP_Error object.
[492] Fix | Delete
* @param \WC_Cart $cart Cart object.
[493] Fix | Delete
*/
[494] Fix | Delete
do_action( 'woocommerce_store_api_cart_errors', $cart_errors, $cart );
[495] Fix | Delete
[496] Fix | Delete
if ( $cart_errors->has_errors() ) {
[497] Fix | Delete
throw new InvalidCartException(
[498] Fix | Delete
'woocommerce_cart_error',
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function