wordpress WooCommerce minicart自定义数量按钮抛出一个内部错误500上wp_admin-ajax.php

polkgigr  于 2023-06-21  发布在  WordPress
关注(0)|答案(1)|浏览(78)

我正在寻找一些帮助,因为我似乎不能弄清楚。我想添加数量选择器按钮的迷你车不使用付费插件,我认为这将是“相当简单”
这是我添加到mini-cart.php的代码(Elementor菜单购物车小部件(专业版))

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

这是我用来更新onChange数量的jquery。已添加在代码片段中,优先级为1

<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();
}

我做错了什么?我应该提供实时站点的URL吗?
我按照大多数人的建议扩大了记忆极限,但这没有帮助。我试着一个接一个地重置插件,看看是否有冲突,那没有帮助。

46scxncf

46scxncf1#

在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() {

     // Here come your code

     wp_die(); // Always at the end to stop execution afterward
}

参见official WordPress code example

相关问题