by Guest » 13 Jan 2025, 19:21
Ich möchte eine Fehlermeldung auf der WooCommerce-Warenkorbseite anzeigen, wenn der Benutzer versucht hat, mehr als die verfügbare Produktmenge hinzuzufügen.
Ich habe versucht, den folgenden Code in die „functions.php“ des Themes einzufügen:< /p>
Code: Select all
add_filter('woocommerce_add_to_cart_validation', 'validate_product_stock_before_add_to_cart', 10, 5);
function validate_product_stock_before_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variation = array()) {
// Get the product object
$product = wc_get_product($product_id);
// Get the stock quantity
$stock_quantity = $product->get_stock_quantity();
// Check if stock management is enabled and stock is insufficient
if ($product->managing_stock() && $stock_quantity !== null && $quantity > $stock_quantity) {
// Set an error message
wc_add_notice(
sprintf(
__('You cannot add more than %d of this product to your cart. Only %d left in stock.', 'woocommerce'),
$quantity,
$stock_quantity
),
'error'
);
// Return false to prevent adding to cart
return false;
}
return $passed;
}
Hier wird jedoch keine Fehlermeldung angezeigt.
Ich möchte eine Fehlermeldung auf der WooCommerce-Warenkorbseite anzeigen, wenn der Benutzer versucht hat, mehr als die verfügbare Produktmenge hinzuzufügen.
Ich habe versucht, den folgenden Code in die „functions.php“ des Themes einzufügen:< /p>
[code]add_filter('woocommerce_add_to_cart_validation', 'validate_product_stock_before_add_to_cart', 10, 5);
function validate_product_stock_before_add_to_cart($passed, $product_id, $quantity, $variation_id = 0, $variation = array()) {
// Get the product object
$product = wc_get_product($product_id);
// Get the stock quantity
$stock_quantity = $product->get_stock_quantity();
// Check if stock management is enabled and stock is insufficient
if ($product->managing_stock() && $stock_quantity !== null && $quantity > $stock_quantity) {
// Set an error message
wc_add_notice(
sprintf(
__('You cannot add more than %d of this product to your cart. Only %d left in stock.', 'woocommerce'),
$quantity,
$stock_quantity
),
'error'
);
// Return false to prevent adding to cart
return false;
}
return $passed;
}
[/code]
Hier wird jedoch keine Fehlermeldung angezeigt.