0
votes

I have one clas and one .php file,

FIRST ONE, functions.php It's a .php file where it's my method that I am calling in the other class.

//PHP
add_action('wp_ajax_getJSONCurrencies', 'getJSONCurrencies');
add_action('wp_ajax_nopriv_getJSONCurrencies', 'getJSONCurrencies');

function getJSONCurrencies () {
        $endpoint = $_POST['endp'];
        $access_key = $_POST['access'];
        $currencies = $_POST['curren'];
        $source = $_POST['sour'];
        $format = 1;

        // Initialize CURL:
        $ch = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&currencies='.$currencies.'&source='.$source.'&format='.$format.'');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Store the data:
        $json = curl_exec($ch);
        curl_close($ch);

        // Decode JSON response:
        echo json_decode($json, true);

    }

And my second Class, custom-wc-widget...php where I call my first class function.

jQuery( document.body ).bind( \'bejob_price_slider_create bejob_price_slider_slide\', function( event, min, max ) {

        var simMoneda = jQuery(\'.woocommerce-currency-switcher-form > select option:selected\').text();
        var simbolo = simMoneda.split(",");

        simMoneda = simbolo[1].trim();

        var endpoint = \'live\';
        var access_key = \'0a85b3479a1a24ceaba1d04f030e7028\';
        var currencies = \'USD,ARS,CLP,PEN,COP,MXN,PAB\';
        var source = \'EUR\';

        jQuery.ajax({
           type:\'POST\',
           url: \'ajaxurl\',
           //url: \'https://apilayer.net/api/\' + endpoint +\'?access_key=\' + access_key + \'&currencies=\' + currencies + \'&source=\' + source + \'&format=\' + 1,
           data: {
                endp: endpoint,
                access: access_key,
                curren: currencies,
                sour: source,
                action: \'getJSONCurrencies\'
            },
            dataType: "json",
            success: function(data) {
                 debugger;
               console.log(data);
            }
        });

        if ( min == 0) {
                jQuery( \'.bejob_price_slider_amount span.from\' ).html( "Gratis");

        } else {
                jQuery( \'.bejob_price_slider_amount span.from\' ).html( min + simMoneda );

        }
        jQuery( \'.bejob_price_slider_amount span.to\' ).html( max + simMoneda );

        jQuery( document.body ).trigger( \'price_slider_updated\', [ min, max ] );
    });

This second class has \' symbols because it's inside a echo php, my idea is to send the data in the ajax statement to my function getJSONCurrencies and then at the success function from the ajax method receive the JSON object I am going to use on my web page.

The problem is when I run the debugger it never enters in my Ajax Method and I dont know why it isn't entering there.

1
Move your javascript into an external javascript file. Use wp_localize_script to set up your data (such as endpoint, access key, currencies, etc). That way you don't have to embed it in a PHP echo. This will dramatically simplify debugging / maintaining your code. (Here's a good writeup: pippinsplugins.com/use-wp_localize_script-it-is-awesome) - random_user_name
And as a side-note, you are calling these "classes", but they aren't classes. - random_user_name
Yeah I know they are not classes, but I use this term to distinguish between both .php files. - fiticida
Terms matter very much in programming. I'd suggest using class properly, and say "Files" if you mean different files. - random_user_name
Well as u wish, my FIRST class .php and my SECOND .php file. About the first question, yeah I know but it isn't my code, I can debugg it perfectly but the problem that it never enters at the ajax mehtod. - fiticida

1 Answers

0
votes

How do you call the jquery? If you have another function please edit your question.

try like below code using wp_localize_script:

add_action('wp_ajax_getJSONCurrencies', 'getJSONCurrencies');
add_action('wp_ajax_nopriv_getJSONCurrencies', 'getJSONCurrencies');

wp_enqueue_script('getJSONCurrencies', get_template_directory_uri().'/js/getJSONCurrencies.js', array('jquery'), '1.0', true );
wp_localize_script('getJSONCurrencies', 'ajax_var', array(
    'url' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('ajax-nonce')
));