英文:
WooCommerce minicart custom quantity buttons throwing an Internal error 500 on wp_admin-ajax.php
问题
我在小购物车中添加了数量选择器按钮,但在更改数量时出现问题。我已经将以下代码添加到了mini-cart.php文件中:
<div class="quantity-buttons">
<button class="decrement-quantity" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">-</button>
<input type="number" class="quantity-input" value="<?php echo esc_attr( $cart_item['quantity'] ); ?>" min="1" max="100" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">
<button class="increment-quantity" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">+</button>
</div>
我还使用了以下jQuery代码来在更改数量时更新:
<script>
jQuery(document).ready(function($) {
$('.quantity-buttons').on('click', '.decrement-quantity, .increment-quantity', function(e) {
e.preventDefault();
var button = $(this);
var productId = button.data('product-id');
var cartItemKey = button.data('cart-item-key');
var quantityInput = button.siblings('.quantity-input');
var quantity = parseInt(quantityInput.val());
var newQuantity = quantity;
if (button.hasClass('decrement-quantity')) {
newQuantity = quantity - 1;
} else if (button.hasClass('increment-quantity')) {
newQuantity = quantity + 1;
}
if (newQuantity < 1 || isNaN(newQuantity)) {
newQuantity = 1;
}
quantityInput.val(newQuantity);
// AJAX request to update the quantity
$.ajax({
url: '<?php echo esc_url( wc_ajax_url() ); ?>',
type: 'POST',
data: {
action: 'update_mini_cart_quantity',
product_id: productId,
cart_item_key: cartItemKey,
quantity: newQuantity
},
success: function(response) {
// Update mini cart contents
if (response && response.fragments) {
$.each(response.fragments, function(key, value) {
$(key).replaceWith(value);
});
}
}
});
});
});
</script>
最后,我将以下代码添加到了function.php文件中:
add_action('wp_ajax_update_mini_cart_quantity', 'update_mini_cart_quantity');
add_action('wp_ajax_nopriv_update_mini_cart_quantity', 'update_mini_cart_quantity');
function update_mini_cart_quantity() {
if (isset($_POST['product_id']) && isset($_POST['cart_item_key']) && isset($_POST['quantity'])) {
$product_id = sanitize_text_field($_POST['product_id']);
$cart_item_key = sanitize_text_field($_POST['cart_item_key']);
$quantity = intval($_POST['quantity']);
if ($quantity < 1) {
$quantity = 1;
}
// Update the quantity in the cart
WC()->cart->set_quantity($cart_item_key, $quantity, true);
// Calculate cart totals
WC()->cart->calculate_totals();
// Prepare cart fragments for AJAX response
$cart_fragments = WC()->cart->get_refreshed_fragments();
// Return updated fragments as JSON response
wp_send_json_success($cart_fragments);
}
wp_send_json_error();
}
我扩展了内存限制,但没有帮助。我尝试逐个重置插件,看是否有任何冲突,但也没有帮助。
英文:
I am looking for some help with this as I cant seem to figure it out.
I want to add quantity selector buttons to the mini cart without using a paid plugin, I thought it would be "fairly simple"
This is the code I added to mini-cart.php ( Elementor Menu cart widget (pro) )
<div class="quantity-buttons">
<button class="decrement-quantity" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">-</button>
<input type="number" class="quantity-input" value="<?php echo esc_attr( $cart_item['quantity'] ); ?>" min="1" max="100" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">
<button class="increment-quantity" data-product-id="<?php echo esc_attr( $product_id ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">+</button>
</div>
This is the jquery I used to update the quantity onChange. Added in codesnippets with 1 priority
<script>
jQuery(document).ready(function($) {
$('.quantity-buttons').on('click', '.decrement-quantity, .increment-quantity', function(e) {
e.preventDefault();
var button = $(this);
var productId = button.data('product-id');
var cartItemKey = button.data('cart-item-key');
var quantityInput = button.siblings('.quantity-input');
var quantity = parseInt(quantityInput.val());
var newQuantity = quantity;
if (button.hasClass('decrement-quantity')) {
newQuantity = quantity - 1;
} else if (button.hasClass('increment-quantity')) {
newQuantity = quantity + 1;
}
if (newQuantity < 1 || isNaN(newQuantity)) {
newQuantity = 1;
}
quantityInput.val(newQuantity);
// AJAX request to update the quantity
$.ajax({
url: '<?php echo esc_url( wc_ajax_url() ); ?>',
type: 'POST',
data: {
action: 'update_mini_cart_quantity',
product_id: productId,
cart_item_key: cartItemKey,
quantity: newQuantity
},
success: function(response) {
// Update mini cart contents
if (response && response.fragments) {
$.each(response.fragments, function(key, value) {
$(key).replaceWith(value);
});
}
}
});
});
});
</script>
And lastly, this was added to function.php
add_action('wp_ajax_update_mini_cart_quantity', 'update_mini_cart_quantity');
add_action('wp_ajax_nopriv_update_mini_cart_quantity', 'update_mini_cart_quantity');
function update_mini_cart_quantity() {
if (isset($_POST['product_id']) && isset($_POST['cart_item_key']) && isset($_POST['quantity'])) {
$product_id = sanitize_text_field($_POST['product_id']);
$cart_item_key = sanitize_text_field($_POST['cart_item_key']);
$quantity = intval($_POST['quantity']);
if ($quantity < 1) {
$quantity = 1;
}
// Update the quantity in the cart
WC()->cart->set_quantity($cart_item_key, $quantity, true);
// Calculate cart totals
WC()->cart->calculate_totals();
// Prepare cart fragments for AJAX response
$cart_fragments = WC()->cart->get_refreshed_fragments();
// Return updated fragments as JSON response
wp_send_json_success($cart_fragments);
}
wp_send_json_error();
}
What am I doing wrong? Should I provide the URL for the live site?
I expanded the memory limit as suggested by most people, but that didnt help.
I tried resetting the plugins one by one to see if there was any collisions, that didnt help.
答案1
得分: 1
在WordPress中,Ajax PHP函数钩入wp_ajax_{$action}
和/或wp_ajax_nopriv_{$action}
钩子时,始终需要在代码的最后一个闭合}
之前使用die();
或wp_die();
来停止执行,以避免在wp_admin-ajax.php上出现内部错误500...
add_action('wp_ajax_my_action', 'get_my_action');
add_action('wp_ajax_nopriv_my_action', 'get_my_action');
function get_my_action() {
// 这里是你的代码
wp_die(); // 始终在最后以停止执行
}
英文:
On WordPress Ajax PHP function hooked inwp_ajax_{$action}
and/or wp_ajax_nopriv_{$action}
hooks, die();
or wp_die();
is always required, at the end of the code before the last closing }
, to stop execution afterward, avoiding an Internal Error 500 on wp_admin-ajax.php…
add_action('wp_ajax_my_action', 'get_my_action');
add_action('wp_ajax_nopriv_my_action', 'get_my_action');
function get_my_action() {
// Here come your code
wp_die(); // Always at the end to stop execution afterward
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论