Skip to content

Instantly share code, notes, and snippets.

@BlueInkAlchemist
Created January 8, 2019 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlueInkAlchemist/836d6ba3a2e05395723ced053d716549 to your computer and use it in GitHub Desktop.
Save BlueInkAlchemist/836d6ba3a2e05395723ced053d716549 to your computer and use it in GitHub Desktop.
WordPress plugin for API consumption
<?php
/**
* @package WooCommerce API Data Builder
* @version 0.4
*/
/*
Plugin Name: API Data Builder
Description: Creates REST API endpoint & builds the JSON response from the WooCommerce API
Author: Josh Loomis
Version: 0.4
*/
function aa_fetch_products() {
// Prep the array we're going to populate.
$results = [];
$this_product = [];
$this_meta = [];
$available_variations = [];
// Declare our other variables.
$consumer_key = '/* enter key here */';
$consumer_secret = '/* enter secret here*/';
// $category = '1803';
// Set up the API call.
$api_url = get_site_url(null,'','https');
$api_url .= '/wp-json/wc/v3/products';
$api_url .= '?consumer_key=';
$api_url .= $consumer_key;
$api_url .= '&consumer_secret=';
$api_url .= $consumer_secret;
$api_url .= '&page=';
$query = new WC_Product_Query( array(
'limit' => -1,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$products = $query->get_products();
$export = [];
foreach($products as $product_id) {
$product_array = [];
$product = new WC_Product_Variable($product_id);
// if(! product_is_rentable($product)) continue;
$product_array["id"] = $product_id;
$product_array["type"] = $product->get_type();
$product_array["name"] = $product->get_name();
$product_array["sku"] = $product->get_sku();
$product_array["permalink"] = get_permalink($product->get_id() );
$product_array["price"] = $product->get_price();
$product_array["thumbnail"] = get_the_post_thumbnail_url($product->get_id(), 'full' );
/**
* Extract attributes as variations
*/
foreach($product->get_attributes() as $product_attributes) {
$product_array['variations'] = $product_attributes->get_data()['options'];
}
/**
* Meta keys we care about
*/
$meta_keys = ['is_recommended_gear', 'is_for_sale', 'is_for_rent', 'gear_list_key'];
foreach($product->get_meta_data() as $meta) {
if(in_array($meta->key, $meta_keys)) {
$product_array[$meta->key] = $meta->value;
}
}
if($product_array["gear_list_key"] != "") {
array_push($export, $product_array);
}
}
//pp($export);
echo json_encode($export);
exit;
}
/*****************************************/
// Create our custom REST API endpoint.
add_action('rest_api_init', function() {
// register_rest_route($namespace, $route, $args = array())
register_rest_route( 'fetch/v1', '/products', array(
'methods' => 'GET', // We want this to be one-way.
'callback' => 'aa_fetch_products', // Calls the function above.
) );
} );
function pp($var) {
echo "<pre>";
print_r($var);
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment