0

I have this JS/jQuery code

window.onload = function(){

    var request = $.ajax({
    url: "../wp-content/plugins/woocommerce/admin/test.php",
    type: "GET",            
    dataType: "html"
    //data: {$lastid},
    });

    request.done(function(msg) {
        $(".recent-orders").append(msg);
    });

    request.fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    });
EDIT: This is the method where I get the $lastid.


<?php
function woocommerce_dashboard_recent_orders() {

    $args = array(
        'numberposts'     => 8,
        'orderby'         => 'post_date',
        'order'           => 'ASC',
        'post_type'       => 'shop_order',
        'post_status'     => 'publish'
    );
    $orders = get_posts( $args );
    if ($orders) :
    echo '<ul class="recent-orders">';
        foreach ($orders as $order) :

            $this_order = new WC_Order( $order->ID );

            echo '
            <li>
                <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time( __( 'l jS \of F Y h:i:s A', 'woocommerce' ), $order->ID ) . '</a><br />
                <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce' ) . ' ' . woocommerce_price($this_order->order_total).'</span></small>
            </li>';

        endforeach;
        $lastid = $order->ID;
        echo '</ul>';
    else:
        echo '<p>' . __( 'There are no product orders yet.', 'woocommerce' ) . '</p>';
    endif;
}
?>

That calls a php file called test.php.

test.php

    <?php
//woocommerce_dashboard_recent_orders_realtime();

/**
 * Init the dashboard widgets.
 *
 * @access public
 * @return void
 */

function filter_where( $where = '' ) {
    $oid = 2100;
    $where = " AND ID > $oid";

    return $where;

}


add_filter( 'posts_where', 'filter_where' );

    $args = array(
        'numberposts'     => 8,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'shop_order',
        'post_status'     => 'publish',
        'suppress_filters' =>  FALSE
    );

    $orders = get_posts( $args );

    if ($orders) :
        foreach ($orders as $order) :
            //echo " $order->ID";
            $this_order = new WC_Order( $order->ID );
            echo '
            <li>
                <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time( __( 'l jS \of F Y h:i:s A', 'woocommerce' ), $order->ID ) . '</a><br />
                <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce' ) . ' ' . woocommerce_price($this_order->order_total).'</span></small>
            </li>';

            //echo (gettype($time3));
        endforeach;

    endif;
//}
?>

What I want to do is to pass the $lastid from the javascript to the test.php file and receive it as something like $lastid also.

I know I should post, but I'm having trouble using it. Can anyone lead me to the right method?

My CODE now

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" >
window.onload = function(){
    //setInterval(function(){
        //var lastid = '<?php echo $lastid; ?>';
        //alert(lastid);
        var request = $.ajax({
        url: "../wp-content/plugins/woocommerce/admin/test.php",
        type: "POST",      
        dataType: "html",
        data: { lastid : '<?php echo $lastid; ?>'},
        });

        request.done(function(msg) {
            $(".recent-orders").append(msg);
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
        //addElement();

    //},1000);
setInterval(function(){


},1000);

}

</script>

<?php
function woocommerce_dashboard_recent_orders() {

    $args = array(
        'numberposts'     => 8,
        'orderby'         => 'post_date',
        'order'           => 'ASC',
        'post_type'       => 'shop_order',
        'post_status'     => 'publish'
    );
    $orders = get_posts( $args );
    if ($orders) :
    echo '<ul class="recent-orders">';
        foreach ($orders as $order) :

            $this_order = new WC_Order( $order->ID );

            echo '
            <li>
                <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time( __( 'l jS \of F Y h:i:s A', 'woocommerce' ), $order->ID ) . '</a><br />
                <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce' ) . ' ' . woocommerce_price($this_order->order_total).'</span></small>
            </li>';

        endforeach;
        $lastid = $order->ID;
        echo '</ul>';
    else:
        echo '<p>' . __( 'There are no product orders yet.', 'woocommerce' ) . '</p>';
    endif;
}
?>

<?php
function filter_where( $where = '' ) {
    $oid = 2110;
    $where = " AND ID > $oid";

    return $where;

}

$lastid = $_GET['lastid'];

add_filter( 'posts_where', 'filter_where' );

    $args = array(
        'numberposts'     => 8,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'shop_order',
        'post_status'     => 'publish',
        'suppress_filters' =>  FALSE
    );


    $orders = get_posts( $args );
    echo "LAST ID: $lastid";
    if ($orders) :
        foreach ($orders as $order) :
            $this_order = new WC_Order( $order->ID );


            echo '
            <li>
                <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time( __( 'l jS \of F Y h:i:s A', 'woocommerce' ), $order->ID ) . '</a><br />
                <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce' ) . ' ' . woocommerce_price($this_order->order_total).'</span></small>
            </li>';

        endforeach;

    endif;
    remove_filter( 'posts_where', 'filter_where' );
//}
?>

4 Answers 4

1

I'm not sure if I understand if I understand your question, but it seems like your first page has already evaluated $lastId, most likely from an insert query... and you want to also set it to a javascript variable, while also using post method. Assuming all that this is how I would for the first page

<script>
var $lastid = <?php echo $lastid ?>;

...

window.onload = function(){

var request = $.ajax({
url: "../wp-content/plugins/woocommerce/admin/test.php",
type: "POST",            
dataType: "html"
data: {"lastid":$lastid},
});

request.done(function(msg) {
    $(".recent-orders").append(msg);
});

request.fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
});

....

</script>

Then on the second page use this to access the post

<?php
$lastid = $_POST['lastid'];
?>

That is how you do post in php hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Try the following in the original code

...
url: "../wp-content/plugins/woocommerce/admin/test.php?lastid=2098"
...

Then in test.php, access the variable using

$lastid = $_GET['lastid'];

There are several other ways that this can be done, this is just a quick and dirty method.

9 Comments

It works, but how can I change the "2098" into a variable? since the $lastid in the javascript is actually dynamic it increases so I should really pass the variable. There's more to the code but they are kinda irrelevant.
@user3295330 in that case you have to assign the value to a variable (like var lastid = '<?php echo $lastid; ?>'; then you can change it how many times you want like this: lastid = some_new_value (or simply lastid++ if you want to increase the value with one)).
@user3295330 and when sending it just use this code: url: '../wp-content/plugins/woocommerce/admin/test.php?lastid='+lastid,
@DanielLisik I tried doing this var lastid = '<?php echo $lastid ?>'; and alert(lastid); it shows an empty string.
@user3295330 can you try with: var lastid = '<?php echo $lastid; ?>';? Also, make sure you didn't define lastid before that code.
|
0

1. Send the variable:

Change:

//data: {$lastid},

to:

data: { lastid : '<?php echo $lastid; ?>'},

2. Get variable in test.php:

$lastid = $_GET['lastid'];

Comments

0
window.onload = function(){
var lastId = <?php echo $lastId; ?>;
var request = $.ajax({
url: "../wp-content/plugins/woocommerce/admin/test.php" + "?lastid=" + lastId,
type: "GET",            
dataType: "html"
//data: {$lastid},
});

request.done(function(msg) {
    $(".recent-orders").append(msg);
});

request.fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
});

on test.php add this line

$lastId = $_GET['lastid'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.