0

Plugin Class File:

function __construct()
    {
        add_shortcode('user_registration_form', array($this, 'shortcode'));
    }

public function hook(){
        add_action( 'wp_ajax_get_product_serial_callback', 'get_product_serial_callback' );
        add_action( 'wp_ajax_nopriv_get_product_serial_callback', 'get_product_serial_callback' );
    }

public function product_serial_ajax() { ?>
        <script type="text/javascript">
                jQuery(document).ready(function(){
                    alert('Hello World!');

                    jQuery.ajax({
                                type: 'GET', 
                                url: "<?php echo admin_url('admin-ajax.php'); ?>",
                                //url: ajaxurl,
                                dataType : "JSON",
                                data : {action: "get_product_serial_callback"},
                                //cache: false, 
                                success: function(data){
                                alert('Eureka')';
                                }
                            });
                });
        </script><?php
    }

function csv_to_array($filename='', $delimiter=',')
    {
            //if(!file_exists($filename) || !is_readable($filename))
            //return FALSE;

            $header = NULL;
            $data = array();
            if (($handle = fopen($filename, 'r')) !== FALSE)
            {
                    while (($row = fgetcsv($handle, 1024, $delimiter)) !== FALSE)
                    {
                            if(!$header)
                                    $header = $row;
                            else
                                    $data[] = array_combine($header, $row);
                    }
                    fclose($handle);
            }
            return $data;
    }

function get_product_serial_callback(){

        $upload_dir = wp_upload_dir();
        $csvFile = $upload_dir['baseurl'].'/Eragon-Serial.csv';
        $csv = $this->csv_to_array($csvFile); //read csv

        foreach ($csv as $serialnum){
                $serial_num_array[]  = $serialnum['product_serial'];
        }

        $json_array = json_encode($serial_num_array);
        echo $json_array;
        die();
    }

function shortcode()
{
    $this->product_serial_ajax();//fetch product serial number
}

However, when encountered ajaxurl is not defined, I changed it ajaxurl which forms in below URL

http://example.com/wp-admin/admin-ajax.php?action=get_product_serial_callback

This too did not helped.

How can I call get_product_serial_callback function in order to fetch the JSON values and set those values in function(data) ?

3
  • Is the hook method called? Also, the callback should be array($this, 'get_product_serial_callback') as mentioned in the anser. – giraff Jun 18 '15 at 12:34
  • How do I check if its being called ? and changed the callback as you mentioned. Still it says ajaxurl is not defined – Slimshadddyyy Jun 18 '15 at 12:42
  • Have you call the hook function in init ? If not then please call that function in init action – Mitul Jun 18 '15 at 13:43
1

Put below code in _construct() function and change action name to get_product_serial_callback :-

add_action( 'wp_ajax_get_product_serial_callback', array($this,'get_product_serial_callback') );
add_action( 'wp_ajax_nopriv_get_product_serial_callback', array($this,'get_product_serial_callback' ));
6
  • Question updated. It still does not even call the function and returns 0 in response – Slimshadddyyy Jun 18 '15 at 12:20
  • add public to function get_product_serial_callback(). – Domain Jun 18 '15 at 12:59
  • Eg: public function get_product_serial_callback() – Domain Jun 18 '15 at 12:59
  • WisdmLabs: How do I get json data response to function product_serial_ajax ? The ajaxurl I see in response is http://example.com/wp-admin/admin-ajax.php?action=get_product_serial – Slimshadddyyy Jun 19 '15 at 5:32
  • You just need to perform add_action with action name as 'wp_ajax_get_product_serial' and call your function defined as above. – Domain Jun 19 '15 at 10:03
0

Plugin Class File:

function __construct()
    {


        add_shortcode('user_registration_form', array($this, 'shortcode'));
        wp_register_script('product-serial', plugins_url('bootstrap/js/product-serial.js', __FILE__),array('jquery')); //custom jquery for product serial
        wp_enqueue_script( 'product-serial' ); //custom jquery for product serial

        $this->hook();
    }

    public function hook()
    {
      add_action('wp_ajax_get_product_serial', array( $this,'get_product_serial'));
      add_action('wp_ajax_nopriv_get_product_serial',array( $this,'get_product_serial') );
    }

    public function product_serial_ajax(){ ?>

        <script type="text/javascript">load_product();</script>

        <?php
    }



   //convert csv data into array
    function csv_to_array($filename='', $delimiter=',')
    {
            //if(!file_exists($filename) || !is_readable($filename))
            //return FALSE;

            $header = NULL;
            $data = array();
            if (($handle = fopen($filename, 'r')) !== FALSE)
            {
                    while (($row = fgetcsv($handle, 1024, $delimiter)) !== FALSE)
                    {
                            if(!$header)
                                    $header = $row;
                            else
                                    $data[] = array_combine($header, $row);
                    }
                    fclose($handle);
            }
            return $data;
    }


    //get product serial number
    function get_product_serial(){


        $upload_dir = wp_upload_dir();
        $csvFile = $upload_dir['baseurl'].'/Eragon-Serial.csv';
        $csv = $this->csv_to_array($csvFile); //read csv

        foreach ($csv as $serialnum){
                $serial_num_array[]  = $serialnum['product_serial'];
        }

        $json_array = json_encode($serial_num_array);
        echo $json_array;
        die();
    }

    function shortcode()
    {
      $this->product_serial_ajax(); //fetch product serial number
    }

Seperate JS file

    function load_product(){
         jQuery.ajax({
          type: "GET", 
          url: ajaxurl,
          dataType : "JSON",
          data : {action: "get_product_serial"},
          //cache: false, 
          success: function(data){
            alert('Eureka');
          }
        });
    }

P.S: putting the following in the header.php of my theme worked for me

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
    </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.