PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.2.15
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.2.15
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / classes / apis / class-api-for-exact-webhooks.php
commercebird / includes / classes / apis Last commit date
class-api-for-exact-webhooks.php 1 year ago class-api-for-product-webhook.php 1 year ago class-api-for-shipping-status.php 1 year ago class-api-for-woo-order.php 1 year ago class-api-for-zoho-inventory.php 1 year ago class-commercebird-list-items-api-controller.php 1 year ago class-commercebird-media-api-controller.php 1 year ago class-commercebird-metadata-controller.php 1 year ago trait-api-permission.php 1 year ago
class-api-for-exact-webhooks.php
116 lines
1 <?php
2
3 namespace CommerceBird\API;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 use WP_REST_Response;
10 use WP_REST_Server;
11 use WP_REST_Controller;
12 use WP_REST_Request;
13 use ReflectionClass;
14 use WP_Query;
15 use WC_Product;
16
17 class Exact extends WP_REST_Controller {
18
19 protected $prefix = 'wc/v3';
20 protected $rest_base = 'exact-webhooks';
21
22 public function __construct() {
23
24 register_rest_route(
25 $this->prefix,
26 '/' . $this->rest_base . '/process/',
27 array(
28 'methods' => WP_REST_Server::CREATABLE,
29 'callback' => array( $this, 'process_webhook' ),
30 'permission_callback' => function () {
31 return current_user_can( 'manage_woocommerce' );
32 },
33 )
34 );
35 }
36
37 public function process_webhook( WP_REST_Request $request ) {
38 // Access the protected body property using reflection
39 $reflection = new ReflectionClass( $request );
40 $property = $reflection->getProperty( 'body' );
41 $property->setAccessible( true );
42 $body = $property->getValue( $request );
43 // Decode the JSON body
44 $data = json_decode( $body, true );
45
46 // use php switch case to handle different webhook types. e.g. Invoice, Item and StockPositions.
47 // code starts here
48 switch ( $data['type'] ) {
49 case 'Items':
50 // process Item webhook. Find the product based on the $data['Code'] which is the sku or post_id
51 $product_id = wc_get_product_id_by_sku( $data['Code'] );
52 if ( ! $product_id ) {
53 $product_id = $data['Code'];
54 }
55 $product = wc_get_product( $product_id );
56 if ( $product ) {
57 $product->set_stock_quantity( $data['Stock'] );
58 // update the product price if needed
59 $product->set_price( $data['StandardSalesPrice'] );
60 $product->set_regular_price( $data['StandardSalesPrice'] );
61 // add meta data cost_price
62 $product->update_meta_data( '_cost_price', $data['CostPriceStandard'] );
63 $product->save();
64 } else {
65 // if product not found, create a new product if the $data['Webshop'] is set to true
66 if ( ! $data['IsWebshopItem'] ) {
67 break;
68 }
69 $product = new WC_Product();
70 $product->set_name( $data['Description'] );
71 $product->set_sku( $data['Code'] );
72 $product->set_price( $data['StandardSalesPrice'] );
73 $product->set_regular_price( $data['StandardSalesPrice'] );
74 if ( $data['Stock'] > 0 ) {
75 $product->set_manage_stock( true );
76 $product->set_stock_quantity( $data['Stock'] );
77 $product->set_stock_status( 'instock' );
78 }
79 $product->set_status( 'draft' );
80 $product->save();
81 }
82 break;
83 case 'StockPositions':
84 // process StockPositions webhook
85 // get the "ItemId" from $data['ItemId'] and find the product based on product meta key "eo_item_id"
86 $item_id = $data['ItemId'];
87 // find the product id based on meta key "eo_item_id" which has the value of $item_id using WP query
88 $args = array(
89 'post_type' => 'product',
90 'meta_query' => array(
91 array(
92 'key' => 'eo_item_id',
93 'value' => $item_id,
94 ),
95 ),
96 );
97 $query = new WP_Query( $args );
98 if ( $query->have_posts() ) {
99 while ( $query->have_posts() ) {
100 $query->the_post();
101 $product_id = get_the_ID();
102 $product = wc_get_product( $product_id );
103 $product->set_stock_quantity( $data['FreeStock'] );
104 $product->save();
105 }
106 }
107 break;
108 default:
109 // process default webhook
110 break;
111 }
112 // code ends here
113 return new WP_REST_Response( 'Webhook processed', 200 );
114 }
115 }
116