Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created June 22, 2017 09:37
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 panoslyrakis/ccebee31292081bb260678b7b343c562 to your computer and use it in GitHub Desktop.
Save panoslyrakis/ccebee31292081bb260678b7b343c562 to your computer and use it in GitHub Desktop.
Manage products inventory via REST API
<?php
/*
Plugin Name: MarketPress Manage inventory via REST
Plugin URI: https://premium.wpmudev.org/
Description: Manage products inventory via REST API
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_MP_Inventory_REST' ) ) {
class WPMUDEV_MP_Inventory_REST {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_MP_Inventory_REST();
}
return self::$_instance;
}
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_mp_routes' ) );
}
public function register_mp_routes(){
register_rest_route( 'mp-store/v1', '/products-inventory', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'update_products_inventory' ),
) );
}
public function update_products_inventory( $request ){
$products_quantities = isset( $_GET[ 'products' ] ) ? $_GET[ 'products' ] : false;
$products_quantities = json_decode( stripcslashes( $products_quantities ) );
if( empty( $products_quantities ) ){
return;
}
foreach( $products_quantities as $product_id => $quantity ){
$product = new MP_Product( $product_id );
if ( $product->get_meta( 'inventory_tracking' ) ) {
$stock = $product->get_stock();
// Update inventory
$new_stock = ( $stock - $quantity );
$product->update_meta( 'inventory', $new_stock );
$product->update_meta( 'inv_inventory', $new_stock );
// Send low-stock notification if needed
if ( $new_stock <= mp_get_setting( 'inventory_threshhold' ) ) {
$product->low_stock_notification();
}
if ( mp_get_setting( 'inventory_remove' ) && $new_stock <= 0 ) {
// Flag product as out of stock - @version 2.9.5.8
wp_update_post( array(
'ID' => $product->ID,
'post_status' => 'draft'
) );
}
}
// Update sales count
$sales = $product->get_meta( 'mp_sales_count', 0 );
$sales += $quantity;
update_post_meta( $product->ID, 'mp_sales_count', $sales );
}
return 'SUCCESS';
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_MP_Inventory_REST'] = WPMUDEV_MP_Inventory_REST::get_instance();
}, 10 );
}
/*
HOW TO USE WITH wp_remote_request:
$wp_request_headers = array(
'Authorization' => 'Basic ' . base64_encode( 'panos:1234567890' )
);
$products_quantities = json_encode( array(
'97' => 2, // PRODUCT ID => AMOUNT TO SUBTRACT
'33' => 1
) );
$wp_request_url = 'http://site-url.com/wp-json/mp-store/v1/products-inventory/?products=' . $products_quantities;
$wp_request_post_response = wp_remote_request(
$wp_request_url,
array(
'method' => 'GET',
'headers' => $wp_request_headers
)
);
$response_code = wp_remote_retrieve_response_code( $wp_request_post_response ) . ' ' . wp_remote_retrieve_response_message( $wp_request_post_response );
$response = $wp_request_post_response;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment