WooCommerce minicart 自定义数量按钮在 wp_admin-ajax.php 上引发了内部错误 500。

huangapple go评论72阅读模式
英文:

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) )

&lt;div class=&quot;quantity-buttons&quot;&gt;
&lt;button class=&quot;decrement-quantity&quot; data-product-id=&quot;&lt;?php echo esc_attr( $product_id ); ?&gt;&quot; data-cart-item-key=&quot;&lt;?php echo esc_attr( $cart_item_key ); ?&gt;&quot;&gt;-&lt;/button&gt;
&lt;input type=&quot;number&quot; class=&quot;quantity-input&quot; value=&quot;&lt;?php echo esc_attr( $cart_item[&#39;quantity&#39;] ); ?&gt;&quot; min=&quot;1&quot; max=&quot;100&quot; data-product-id=&quot;&lt;?php echo esc_attr( $product_id ); ?&gt;&quot; data-cart-item-key=&quot;&lt;?php echo esc_attr( $cart_item_key ); ?&gt;&quot;&gt;
&lt;button class=&quot;increment-quantity&quot; data-product-id=&quot;&lt;?php echo esc_attr( $product_id ); ?&gt;&quot; data-cart-item-key=&quot;&lt;?php echo esc_attr( $cart_item_key ); ?&gt;&quot;&gt;+&lt;/button&gt;
&lt;/div&gt;

This is the jquery I used to update the quantity onChange. Added in codesnippets with 1 priority

&lt;script&gt;
jQuery(document).ready(function($) {
$(&#39;.quantity-buttons&#39;).on(&#39;click&#39;, &#39;.decrement-quantity, .increment-quantity&#39;, function(e) {
e.preventDefault();
var button = $(this);
var productId = button.data(&#39;product-id&#39;);
var cartItemKey = button.data(&#39;cart-item-key&#39;);
var quantityInput = button.siblings(&#39;.quantity-input&#39;);
var quantity = parseInt(quantityInput.val());
var newQuantity = quantity;
if (button.hasClass(&#39;decrement-quantity&#39;)) {
newQuantity = quantity - 1;
} else if (button.hasClass(&#39;increment-quantity&#39;)) {
newQuantity = quantity + 1;
}
if (newQuantity &lt; 1 || isNaN(newQuantity)) {
newQuantity = 1;
}
quantityInput.val(newQuantity);
// AJAX request to update the quantity
$.ajax({
url: &#39;&lt;?php echo esc_url( wc_ajax_url() ); ?&gt;&#39;,
type: &#39;POST&#39;,
data: {
action: &#39;update_mini_cart_quantity&#39;,
product_id: productId,
cart_item_key: cartItemKey,
quantity: newQuantity
},
success: function(response) {
// Update mini cart contents
if (response &amp;&amp; response.fragments) {
$.each(response.fragments, function(key, value) {
$(key).replaceWith(value);
});
}
}
});
});
});
&lt;/script&gt;

And lastly, this was added to function.php

add_action(&#39;wp_ajax_update_mini_cart_quantity&#39;, &#39;update_mini_cart_quantity&#39;);
add_action(&#39;wp_ajax_nopriv_update_mini_cart_quantity&#39;, &#39;update_mini_cart_quantity&#39;);
function update_mini_cart_quantity() {
if (isset($_POST[&#39;product_id&#39;]) &amp;&amp; isset($_POST[&#39;cart_item_key&#39;]) &amp;&amp; isset($_POST[&#39;quantity&#39;])) {
$product_id = sanitize_text_field($_POST[&#39;product_id&#39;]);
$cart_item_key = sanitize_text_field($_POST[&#39;cart_item_key&#39;]);
$quantity = intval($_POST[&#39;quantity&#39;]);
if ($quantity &lt; 1) {
$quantity = 1;
}
// Update the quantity in the cart
WC()-&gt;cart-&gt;set_quantity($cart_item_key, $quantity, true);
// Calculate cart totals
WC()-&gt;cart-&gt;calculate_totals();
// Prepare cart fragments for AJAX response
$cart_fragments = WC()-&gt;cart-&gt;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(); // 始终在最后以停止执行
}

参见官方WordPress代码示例

英文:

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(&#39;wp_ajax_my_action&#39;, &#39;get_my_action&#39;);
add_action(&#39;wp_ajax_nopriv_my_action&#39;, &#39;get_my_action&#39;);
function get_my_action() {
// Here come your code
wp_die(); // Always at the end to stop execution afterward
}

See official WordPress code example

huangapple
  • 本文由 发表于 2023年6月19日 19:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定