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