0

I have wordpress woocommerce site. I am trying to get data from this site for mobile backend. When the user login via backend, I want to get expiry date from site using my backend. But I am getting errors.

What I want to know is that what woocommerce files should I have to include for run the wc_memberships_get_user_memberships() function.

Here is my backend source.

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// include database and object files
include_once 'db_connect.php';
include_once '../wp-content/plugins/woocommerce/includes/class_woocommerce.php';
include_once '../wp-content/plugins/woocommerce-memberships/woocommerce-memberships.php';
include_once '../wp-content/plugins/woocommerce-memberships/includes/functions/wc-memberships-functions-user-memberships.php';

class FetchData {

    public function logIn() {
        if( isset($_POST['user_name']) && isset($_POST['password']) ){

            $password = $_POST['password'];

            $sql = "SELECT * FROM `wprl_users` WHERE `user_email` = '".$_POST['user_name']."' limit 1";
            $query = mysqli_query(DBOpen(), $sql);
            $user_info = mysqli_fetch_assoc($query);            

            if (count($user_info) > 0) {

                require_once( '../wp-includes/class-phpass.php' );
                $hasher = new PasswordHash(8, TRUE);

                $pass_match = $hasher->CheckPassword( $password, $user_info['user_pass'] );

                if($pass_match == TRUE) {
                    // Get memberships for the current user.
                    $memberships = wc_memberships_get_user_memberships($user_info['ID']);

                    // Verify that they have some memberships.
                    if ( $memberships ) {
                            foreach( $memberships as $memberships ) {
                                // Print the expiration date in mysql format.
                                $end_date = $membership->get_end_date();
                            }
                    }
                    echo json_encode(array('result'=>'success', 'member_name'=>$user_info['display_name'], 'member_num'=>
                                            $user_info['ID'], 'expiry_date'=>$end_date));
                }
                else {
                    echo json_encode(array('result'=>'failed', 'message'=>'User name or password incorrect!'));
                }
            }
            else {                                              
                echo json_encode(array('result'=>'failed', 'message'=>'User name or password incorrect!'));
            }   
        }
        else {
            echo json_encode(array('result'=>'failed', 'message'=>'Wrong parameter'));
        }
    }
}

$option = new Fetchdata();

$options = (isset($_POST['options'])) ? $_POST['options'] : 1;

    switch ($options){ 
        case "login":
            $option->logIn();
            break;
    }

?>
mobilewin
  • 1
  • 1

1 Answers1

0

Wordpress has a certain way to bootstrap their code, connect to DB, initializing plugin etc. the way you do it, you try to include these files manually, even you were using check pass manually. there is https://codex.wordpress.org/Function_Reference/wp_check_password per se. so what you are doing is kind of not following their pattern. if you really want to do it like this, you can backtrace the bootstrap code from /index.php

however, the best solution for you is that You should create a wordpress plugin. you can follow this guide https://codex.wordpress.org/Writing_a_Plugin

or https://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/

or you can try to use framework like this: https://framework.themosis.com

r4ccoon
  • 3,056
  • 4
  • 26
  • 32