#57803
chrisdavies71
Participant

Hi Zac,

Here is the class.php file which is where i need to do the post ID stuff. If you need anything else let me know.

<?php

include_once("../../../../../wp-load.php");

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class current_rms_api
{
    //TODO: Make the version selectable from a dropdown in InspectorControls - currently only v1.
    //NOTE: apikey ad subdomain are here for reference only remove later
    //private $apikey = 'JNxCt3e_mNa-M2v71qJF';
    //private $subdomain = 'cpackham';
    private $version = 'v1';

    //public function __construct( $postid) {
    public function __construct() {
        //$settings = get_option( 'cp_currentrms_settings' );
        //print_r ($settings);
        //$this->apikey = $settings['cp_currentrms_apikey'];
        //$this->subdomain = $settings['cp_currentrms_subdomain'];
        //$this->apikey = get_post_meta( $postid, 'current_rms_apikey', 'true' );
        //$this->subdomain = get_post_meta( $postid, 'current_rms_subdomain', 'true' );
        $this->apikey = get_post_meta( '869', 'current_rms_apikey', 'true' );
        $this->subdomain = get_post_meta( '869', 'current_rms_subdomain', 'true' );
    }

    private function query( $endpoint, $params=array())
    {
        $params['subdomain']=$this->subdomain;
        $params['apikey']=$this->apikey;
        // Call API url
        $url = 'https://api.current-rms.com/api/'.$this->version.'/'.$endpoint.'?' . http_build_query($params);

        //echo $url;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);

        $data = json_decode($response);

        return $data;
    }

    // Function gets a list of product groups (categories).
    public function getProductGroups()
    {
        $data = $this->query('product_groups');
        return $data->product_groups;
    }

    // Function gets a list of products in a product group (category).
    // NOTE: You can not do this using the id of the product group.
    // TODO: See if we can get this to only show categories that have a product count / hide empty categories
    // TODO: See if we can get this to show only product with a specific tag (so that tab panel nav works.
    public function getProducts( $name )
    {
        $data = $this->query('products', array('q[product_group_name_eq]'=>$name,'q[accessory_only_eq]'=>false,'per_page'=>100));
        return $data->products;
    }

    // Function gets an individual product based on the product ID.
    public function getProduct( $id )
    {
        $data = $this->query('products/' . $id);
        return $data->product;
    }

    // Function gets a list of accessories based on the product ID
    public function getAccessories( $id )
    {
        $data = $this->query('products/' . $id . '/accessories');
        return $data->accessories;
    }

    // Function gets the price based on the product ID
    public function getProductPrice( $id )
    {
        $data = $this->query('products/' . $id . '/item_price');
        return $data->data;
    }

    // Function gets the rate definitions for the products
    public function getRates( $id )
    {
        $data = $this->query('rate_definitions/' . $id );
        return $data->rate_definition;
    }

    // Function gets a list of tags
    public function getTags()
    {
        $data = $this->query('products/tag_cloud');
        return $data->tag_cloud;
    }

    // Function gets products by tag
    public function getProductsbyTag( $tag )
    {
        $data = $this->query('products', array('tags'=>'['.$tag.']'));
        return $data->products;
    }

}
?>

Regards

CHRIS