While doing ajax, WordPress returning internal server error 500 and stating that my theme functions are undefined

I have an ajax call in my js file loaded in footer (it actually doesn't matter if I make it simpler or without the json parsing, I will get the same errors) - in this case I pass the callback and any arguments in a function; if I just make this plain and simple it doesn't change:

 function pigafettaGetJSON(func, a) {

    var output = $.parseJSON( $.ajax({
            type: 'POST',
            url : pigafetta.ajax_url,
            data : {
                action : func,
                args : a
            },
            dataType : "json",
            async : false
        }).responseText);

    return output;
}

the ajax url is stored as a variable using wp_localize_script, among others:

    wp_enqueue_script( 'pigafetta', PIGAFETTA_URI . '/assets/scripts/js/app.min.js', array( 'jquery', 'vendor' ), PIGAFETTA_VERSION, true );
    wp_localize_script( 'pigafetta', 'pigafetta', array(
        'ajax_url'      = admin_url( 'admin-ajax.php' ),
        'templates_url' = PIGAFETTA_URI . '/templates/views/',
        'partials_url'  = PIGAFETTA_URI . '/templates/views/partials',
        'i18n'          = pigafetta_phrases()
    ) );

this is the callback function :

function pigafetta_ajax_get_cart() {
    $cart = pigafetta_get_cart();
    wp_send_json_success($cart);
}
add_action( 'wp_ajax_pigafetta_ajax_get_cart', 'pigafetta_ajax_get_cart' );
add_action( 'wp_ajax_nopriv_pigafetta_ajax_get_cart', 'pigafetta_ajax_get_cart' );

this is the function invoked by the ajax callback - which will function fine if called by PHP:

function pigafetta_get_cart() {

    $cart = array();
    $cart['items'] = pigafetta_get_cart_items();
    $count = sizeof( $cart['items'] );
    $cart['count']['items'] = $count;
    $cart['count']['label'] = $count  0 ? sprintf( _n( '%s item', '%s items', $count, 'pigafetta' ), $count ) : __( 'Cart is empty', 'pigafetta' );
    $cart['empty']['notice'] = $count = 0 ? __( "You don't have any items yet in your booking.", 'pigafetta' ): '';
    $total = WC()-cart-get_cart_subtotal();
    $cart['totals']['amount'] = $total;
    $cart['totals']['label'] = $count  0 ? sprintf( _x( 'Total: %s', 'Total amount in cart', 'pigafetta' ), $total ) : '';
    $cart['actions'] = array(
        array(
            'icon' = 'i class="fa fa-shopping-cart"/i',
            'link'  = WC()-cart-get_cart_url(),
            'label' = __( 'View cart', 'pigafetta' ),
            'description' = __( 'Review your booking', 'pigafetta' ),
            'class' = 'view-cart',
        ),
        array(
            'icon' = 'i class="fa fa-credit-card"/i',
            'link'  = WC()-cart-get_checkout_url(),
            'label' = __( 'Checkout', 'pigafetta' ),
            'description' = __( 'Finalize your booking', 'pigafetta' ),
            'class' = 'checkout',
        ),
        array(
            'icon' = 'i class="fa fa-trash"/i',
            'link'  = '',
            'label' = __( 'Empty cart', 'pigafetta' ),
            'description' = __( 'Remove all items from cart', 'pigafetta' ),
            'class' = 'empty-cart',
        )
    );

    return $cart;
}

however, when called with admin-ajax.php in front-size it will return many errors; mostly it will not recognize functions from my own theme; for example, pigafetta_get_cart_items() invokes a another function in my theme... that function is not recognized. But it doesn't make sense, because I don't get the same error if I run these functions from PHP. They have been included in my functions.php file in my theme. If I include again the same function with a require_once inside the function calling the other function it will work but then stopping somewhere else and saying that wp_db_version is an undefined variable, for example.

I don't understand where the error could be, since this is working fine in PHP - my host is WPEngine and I don't have any other issue with them. I double checked this also on my local environment with Vagrant and I get the same errors, including the internal server error.

WP_DEBUG is on but I can't see any relevant information from logs and notices.

I've been spending hours on this and don't know where this comes from...

Topic 500-internal-error internal-server-error ajax errors Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.