Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.

Commit ecfb38b

Browse files
committed
Initial demo code commit
This included the initial commit of the demo code for this plugin
1 parent 6760e2b commit ecfb38b

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Product Data Store
4+
* Inventory with pass-thru
5+
*
6+
* @package WooCommerce Product Inventory Data Store
7+
* @author Automattic
8+
*/
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
return;
11+
}
12+
/**
13+
* WC_Product_Inventory_Datastore.
14+
*/
15+
class WC_Product_Inventory_Data_Store
16+
implements WC_Object_Data_Store_Interface, WC_Product_Data_Store_Interface {
17+
18+
public function __construct( &$parent_data_store ) {
19+
$this->parent_instance = $this->create_parent_instance( $parent_data_store );
20+
}
21+
22+
private function create_parent_instance( $store ) {
23+
if ( is_object( $store ) ) {
24+
if ( ! $store instanceof WC_Object_Data_Store_Interface ||
25+
! $store instanceof WC_Product_Data_Store_Interface ) {
26+
throw new Exception( __( 'Invalid parent product data store.', 'woocommerce' ) );
27+
}
28+
return $store;
29+
} else {
30+
if ( ! class_exists( $store ) ) {
31+
throw new Exception( __( 'Invalid parent product data store.', 'woocommerce' ) );
32+
}
33+
return new $store;
34+
}
35+
}
36+
37+
public function create( &$product ) {
38+
error_log( 'create' );
39+
$this->parent_instance->create( $product );
40+
}
41+
42+
public function read( &$product ) {
43+
error_log( 'read' );
44+
$this->parent_instance->read( $product );
45+
46+
//$product->set_stock_quantity( 20 );
47+
48+
$inventory_url = 'http://192.168.200.200:8080/api/inventory/' . $product->get_sku();
49+
$request = wp_remote_get( $inventory_url );
50+
$response = wp_remote_retrieve_body( $request );
51+
$data = json_decode( $response );
52+
$product->set_stock_quantity( $data );
53+
}
54+
55+
public function update( &$product ) {
56+
error_log( 'update' );
57+
$this->parent_instance->update( $product );
58+
}
59+
60+
public function delete( &$product, $args = array() ) {
61+
error_log( 'delete' );
62+
$this->parent_instance->delete( $product, $args );
63+
}
64+
65+
public function read_meta( &$product ) {
66+
error_log( 'read_meta' );
67+
$this->parent_instance->read_meta( $product );
68+
}
69+
70+
public function delete_meta( &$product, $meta ) {
71+
error_log( 'delete_meta' );
72+
$this->parent_instance->delete_meta( $product, $meta );
73+
}
74+
75+
public function add_meta( &$product, $meta ) {
76+
error_log( 'add_meta' );
77+
$this->parent_instance->add_meta( $product, $meta );
78+
}
79+
80+
public function update_meta( &$product, $meta ) {
81+
error_log( 'update_meta' );
82+
$this->parent_instance->update_meta( $product, $meta );
83+
}
84+
85+
public function get_on_sale_products() {
86+
error_log( 'get_on_sale_products' );
87+
return $this->parent_instance->get_on_sale_products();
88+
}
89+
90+
public function get_featured_product_ids() {
91+
error_log( 'get_featured_product_ids' );
92+
return $this->parent_instance->get_featured_product_ids();
93+
}
94+
95+
public function is_existing_sku( $product_id, $sku ) {
96+
error_log( 'is_existing_sku' );
97+
return $this->parent_instance->is_existing_sku( $product_id, $sku );
98+
}
99+
100+
public function get_product_id_by_sku( $sku ) {
101+
error_log( 'get_product_id_by_sku' );
102+
return $this->parent_instance->get_product_id_by_sku();
103+
}
104+
105+
public function get_starting_sales() {
106+
error_log( 'get_starting_sales' );
107+
return $this->parent_instance->get_starting_sales();
108+
}
109+
110+
public function get_ending_sales() {
111+
error_log( 'get_ending_sales' );
112+
return $this->parent_instance->get_ending_sales();
113+
}
114+
115+
public function find_matching_product_variation( $product, $match_attributes = array() ) {
116+
error_log( 'find_matching_product_variation' );
117+
return $this->parent_instance->find_matching_product_variation( $product, $match_attributes );
118+
}
119+
120+
public function sort_all_product_variations( $parent_id ) {
121+
error_log( 'sort_all_product_variations' );
122+
$this->parent_instance->sort_all_product_variations( $parent_id );
123+
}
124+
125+
public function get_related_products( $cats_array, $tags_array, $exclude_ids, $limit, $product_id ) {
126+
error_log( 'get_related_products' );
127+
return $this->parent_instance->get_related_products( $cats_array, $tags_array, $exclude_ids, $limit, $product_id );
128+
}
129+
130+
public function update_product_stock( $product_id_with_stock, $stock_quantity = null, $operation = 'set' ) {
131+
error_log( 'update_product_stock' );
132+
$this->parent_instance->update_product_stock( $product_id_with_stock, $stock_quantity, $operation );
133+
}
134+
135+
public function update_product_sales( $product_id, $quantity = null, $operation = 'set' ) {
136+
error_log( 'update_product_sales' );
137+
$this->parent_instance->update_product_sales( $product_id, $quantity, $operation );
138+
}
139+
140+
public function get_shipping_class_id_by_slug( $slug ) {
141+
error_log( 'get_shipping_class_id_by_slug' );
142+
return $this->parent_instance->get_shipping_class_id_by_slug( $slug );
143+
}
144+
145+
public function get_products( $args = array() ) {
146+
error_log( 'get_products' );
147+
return $this->parent_instance->get_products( $args );
148+
}
149+
150+
public function get_product_type( $product_id ) {
151+
error_log( 'get_product_type' );
152+
return $this->parent_instance->get_product_type( $product_id );
153+
}
154+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Base class for plugin
4+
*
5+
* @package WooCommerce Product Inventory Data Store
6+
* @author Automattic
7+
*/
8+
if ( ! defined( 'ABSPATH' ) ) {
9+
return;
10+
}
11+
/**
12+
* WC_Product_Inventory_Datastore_Plugin class.
13+
*/
14+
class WC_Product_Inventory_Data_Store_Plugin {
15+
16+
public function __construct() {
17+
add_filter( 'woocommerce_data_stores', array( $this, 'install_data_store' ) );
18+
}
19+
20+
public function install_data_store( $stores ) {
21+
include_once dirname( __FILE__ ) . '/class-product-inventory-data-store.php';
22+
23+
$instance = new WC_Product_Inventory_Data_Store( $stores[ 'product' ] );
24+
$stores[ 'product' ] = $instance;
25+
26+
return $stores;
27+
}
28+
}
29+
30+
new WC_Product_Inventory_Data_Store_Plugin();

inventory-server/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

inventory-server/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "inventory-server",
3+
"version": "1.0.0",
4+
"description": "Test API inventory server",
5+
"scripts": {
6+
"start": "nodemon server.js --exec babel-node"
7+
},
8+
"babel": {
9+
"presets": [
10+
"es2015",
11+
"stage-2"
12+
],
13+
"plugins": []
14+
},
15+
"author": "Automattic",
16+
"license": "GPL-2.0",
17+
"devDependencies": {
18+
"babel-cli": "^6.26.0",
19+
"babel-preset-es2015": "^6.24.1",
20+
"babel-preset-stage-2": "^6.24.1",
21+
"babel-register": "^6.26.0",
22+
"express": "^4.16.2",
23+
"nodemon": "^1.12.1"
24+
}
25+
}

inventory-server/server.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import http from 'http';
2+
import express from 'express';
3+
4+
const inventory = {
5+
[ 'p120' ]: 240,
6+
[ 'p121' ]: 252,
7+
[ 'p122' ]: 258,
8+
};
9+
10+
const app = express();
11+
const server = http.createServer( app );
12+
13+
server.listen( 8080, function() {
14+
const port = server.address().port;
15+
console.log( 'Server running on port %d', port );
16+
} );
17+
18+
app.get( '/api/inventory/:sku', getSkuInventory );
19+
20+
function getSkuInventory( req, res ) {
21+
const { sku } = req.params;
22+
23+
const count = inventory[ sku ];
24+
if ( 'undefined' !== typeof count ) {
25+
res.status( 200 ).json( count );
26+
}
27+
}
28+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Plugin Name: WooCommerce Product Inventory Datastore
4+
* Plugin URI: https://woocommerce.com/
5+
* Description: Demo Code: Overrides product inventory.
6+
* Version: 1.0.0-dev
7+
* Author: Automattic
8+
* Author URI: https://woocommerce.com
9+
* Requires at least: 4.4
10+
* Tested up to: 4.7
11+
*
12+
* Text Domain: woocommerce-product-inventory-datastore
13+
* Domain Path: /languages/
14+
*
15+
* @package WooCommerce Product Inventory Datastore
16+
* @author Automattic
17+
*/
18+
if ( ! defined( 'ABSPATH' ) ) {
19+
exit;
20+
}
21+
22+
// Instantiate the main class.
23+
include_once dirname( __FILE__ ) . '/includes/class-woocommerce-product-inventory-datastore-plugin.php';

0 commit comments

Comments
 (0)