Class ‘WC_Order’ not found error

I am trying to write a plugin for wordpress. When i the code is like this

enter image description here

Read More

But i am getting class not found error. I want to get mobile number from order id. Any help is appreciated.

Entire code of plugin

<?php 
/*
Plugin Name: Dorf SMS Plugin
Plugin URI: http://akismet.com/?return=true
Description: amaing sms plugin
Version: 1.0.1
Author: akshay
Author URI: http://www.yourkishore.com
License: GPLv2 or later
*/

include_once('mySettings.php');
if( is_admin() ){
    $my_settings_page = new MySettingsPage();
}

add_action( 'admin_menu', 'send_sms_options_page', $priority = 10, $accepted_args = 1 );
function send_sms_options_page(){
    add_options_page( 'Send Sms', 'Send Sms', 'manage_options', 'send_sms_slag', 'send_sms_func' );
}
function send_sms_func(){
    ?>
        <?php screen_icon(); ?> <h2>Kishore Sms Settings</h2>
        <div id="wrap" style="margin-top:30px">
        <div style="width:400px" id="jw_options">
                    <div style="width:200px;float:left">

                        <b>User name</b>   <br>
                        <b>User Mobile number</b>  <br>
                        <b>User Shipping Address</b>   <br>
                        <b>User Billing Address</b>  <br>
                        <b>User Order date</b>  <br>
                        <b>User Email</b>  <br>

                    </div>
                    <div style="width:200px;float:left">

                        [user_name] <br>
                        [user_number] <br>
                        [user_shipping] <br>
                        [user_billing] <br>
                        [user_order_date] <br>
                        [user_email] <br>
                    </div>
                </div>
                <div style="clear:both"></div>
                <br>
            Enter Order Id :<br> <input id="sa_order_id" type="text" name="" value="" placeholder=""><br><br>
            Enter Message : <br><textarea id="sa_message" style="width:300px;height:200px"></textarea><br><br>
            <button class="button button-primary" onclick="send_sms();">Send Sms</button>
        </div>
        <script type="text/javascript">
        function send_sms(){
            var sa_order_id = document.getElementById('sa_order_id').value;
            var sa_message = document.getElementById('sa_message').value;
            var hr = new XMLHttpRequest();
            var url = "http://localhost/wordpress/wp-admin/options-general.php?page=send_sms_slag";
            var vars = "sa_order_id="+sa_order_id+"&sa_message="+sa_message;
            hr.open("POST",url,true);
            hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            hr.onreadystatechange = function()
            {
                if(hr.readyState == 4 && hr.status == 200)
                {
                    var return_data = hr.responseText;  
                    console.log(return_data);
                }   
            }
            hr.send(vars);

        }
        </script>   
    <?php
}



// Sending message after order placed 

function send_confirmation_message($order_id){

    // send order completed message to user
    $order = new WC_Order( $order_id );
    // get options
    $sms_options = get_option( 'my_option_name');
    $to_send = $sms_options['sa_on_payment_success'];
    $admin_number = $sms_options['admin_mobile_number'];

    $to_find = array('[user_order_id]','[user_order_amount]','[user_name]','[user_number]','[user_order_date]','[user_email]');
    $to_replace_with = array($order->id,$order->order_total.' Rs',$order->billing_last_name,$order->billing_phone.','.$admin_number,$order->completed_date,$order->billing_email);
    $id = urlencode(str_replace($to_find, $to_replace_with, $to_send));

    $to_user_url = "http://enterprise.smsgupshup.com/GatewayAPI/rest?method=SendMessage&send_to=".$order->billing_phone."&msg=".$id."&msg_type=TEXT&userid=2012345&auth_scheme=plain&password=testing&v=1.1&format=text";
    file_get_contents($to_user_url);
}
add_action( 'woocommerce_thankyou','send_confirmation_message', $priority = 10, $accepted_args = 1 );






// Sending message on order status completed

function sa_order_status_completed($order_id){

    // send order completed message to user
    $order = new WC_Order( $order_id );
    // get options
    $sms_options = get_option( 'my_option_name');
    $to_send = $sms_options['sa_on_order_completed'];

    $to_find = array('[user_order_id]','[user_order_amount]','[user_name]','[user_number]','[user_order_date]','[user_email]');
    $to_replace_with = array($order->id,$order->order_total.' Rs',$order->billing_last_name,$order->billing_phone,$order->completed_date,$order->billing_email);
    $id = urlencode(str_replace($to_find, $to_replace_with, $to_send));

    $to_user_url = "http://enterprise.smsgupshup.com/GatewayAPI/rest?method=SendMessage&send_to=".$order->billing_phone."&msg=".$id."&msg_type=TEXT&userid=200123456&auth_scheme=plain&password=testing&v=1.1&format=text";
    file_get_contents($to_user_url);
}
add_action( 'woocommerce_order_status_completed', 'sa_order_status_completed' );
?>

Related posts

Leave a Reply

1 comment

  1. Initialize the order object inside init function. It will fix you problem.

    add_action('init', 'my_init', 1);
    function my_init(){
        $order_id = <my oder id>
        $order = new WC_Order($order_id);
    }