Γέφυρα e-shop με erp – Διασύνδεση erp με e-shop
This blog provides information and code that may help achieving an integration between e-shop and erp and vice versa. As a result a bridge between them can be established, an erp to e-shop bridge.
Erp to e-shop connection.
This is the 1st part of the connection and is briefly covered at this webpage. The connection must be two way and the first part is the erp to e-shop direction. The 2nd part which is e-shop to erp connection is covered in detail at the other webpage.
The erp to e-shop connection part is simpler than the e-shop to erp connection because it is handled via rest api keys. Obviously all CMS platforms use rest api keys. So first step is to enable rest api from the settings of your CMS and acquire api key. In fact there are two different keys used and are both needed for the integration to function properly. The first is “consumer key” and the second is “consumer pass”.
Second step is to start making a php application.
I managed to succeed a connection between an erp that is online, uses php and wp. Most erps should probably be on the cloud or at least web based nowdays and not on local servers. But eventhough locally based erps can easily connect to an e-shop provided that specific settings are applied to the router of the unit.
The following php code is the 1st example to achieve the above objective. It efficiently updates regular price, stock quantity and stock status of a product, based on fields of a form that the user fills with the desired values. The id of the product is filled in the form. So apparently this is a simple one way bridge from erp to e-shop. Someone could add whatever features of the e-shop to update he wants provided that the variables are written correctly. For example stock_quantity variable is a standard name at wp and should not be changed.
1st example – erp to e-shop connection
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php
/**If submit button*/
if (isset($_POST['submit'])) {
$id=$_POST['product_id'];
$price=$_POST['price'];
$quantity=$_POST['quantity'];
$stockstatus=$_POST['stockstatus'];
require_once( './wp-load.php' );
error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
$consumer_key = 'your consumer key'; /**write your key*/
$consumer_secret = 'your secret key'; /**write your key*/
$response = wp_remote_request(
'https://your website url/wp-json/wc/v3/products/'.$id, /**the variable $id is taken from the form input*/
array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "$consumer_key:$consumer_secret" )
),
'body' => array(
'regular_price' => $price,
'stock_quantity' => $quantity,
'stock_status' => $stockstatus, /**Here I can insert whatever options I want to get updated. I' ve presented price, stock quantity and stock status update way*/
)
)
);
if( 'OK' === wp_remote_retrieve_response_message( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ) );
echo 'The product ' . $body->name . ' has been updated';
}
}else{}
?>
<H2>Set product data</H2>
<form method="post">
<label for="product_id">product_id</label>
<input type="number" name="product_id" id="product_id">
<label for="price">price</label>
<input type="number" name="price" id="price">
<label for="quantity">quantity</label>
<input type="number" name="quantity" id="quantity">
<label for="stockstatus">stockstatus</label>
<input type="text" name="stockstatus" id="stockstatus">
</p>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
So the result of this php webpage is to accomplish an erp to e-shop connection and the ability to use it on different options of the e-shop.
2nd example -erp to e-shop connection
For this php integration which is simpler than the 1st example I needed to download the
wc-api-php package. It contains Client.php and the HttpClient folder which contains: BasicAuth.php, HttpClient.php, HttpClientException.php, OAuth.php, Options.php, Request.php, Response.php. I found all of them at https://github.com/woocommerce/wc-api-php/tree/trunk/src/WooCommerce. You can use a form for the variables insertion as in the example1.
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<H2>Set product data</H2>
<form method="post">
<label for="product_id">product_id</label>
<input type="number" name="product_id" id="product_id">
<label for="price">price</label>
<input type="number" name="price" id="price">
<label for="quantity">quantity</label>
<input type="number" name="quantity" id="quantity">
<label for="stockstatus">stockstatus</label>
<input type="text" name="stockstatus" id="stockstatus">
</p>
<input type="submit" name="submit" value="submit">
</form>
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
require __DIR__ .'/wp-content/plugins/woocommerce/vendor/autoload.php';
require __DIR__ .'/wp-content/plugins/woocommerce/Client.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/BasicAuth.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/HttpClient.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/HttpClientException.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/OAuth.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/Options.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/Request.php';
require __DIR__ .'/wp-content/plugins/woocommerce/HttpClient/Response.php';
use Automattic\WooCommerce\Client;
/**If submit button*/
if (isset($_POST['submit'])) {
$id=$_POST['product_id'];
$price=$_POST['price'];
$quantity=$_POST['quantity'];
$stockstatus=$_POST['stockstatus'];
$consumer_key = 'your ck';
$consumer_secret = 'your cs';
$woocommerce = new Client(
'url of e-shop',
$consumer_key,
$consumer_secret,
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true, // Force Basic Authentication as query string true and using under HTTPS
]
);
print_r($woocommerce->get('products'));
$data = [
'stock_quantity'=> $quantity,
'regular_price' => $price
];
print_r($woocommerce->put('products/'.$id, $data)); /**the variable $id is taken from the form input*/
}
?>
</body>
</html>
This php prints the products and updates a product quantity and price based on form inputs. I can perform other functions by adding different code at “$data” variable.
Γέφυρα e-shop με erp – Σύνδεση erp με e-shop
Feel free to comment or contact me. I am available to provide you with any further information should it be required.
To continue the integration you can read part 2 which describes the reverse connection from e-shop to erp which is more important.