PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.5.0
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.5.0
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-cmbird.php 9 months ago class-api-for-exact-webhooks.php 9 months ago class-api-for-product-webhook.php 9 months ago class-api-for-shipping-status.php 9 months ago class-api-for-woo-order.php 9 months ago class-api-for-zoho-inventory.php 9 months ago class-commercebird-list-items-api-controller.php 10 months ago class-commercebird-media-api-controller.php 10 months ago class-commercebird-metadata-controller.php 9 months ago index.php 1 year ago trait-api-permission.php 9 months ago
class-api-for-exact-webhooks.php
159 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 if ( empty( $data ) ) {
47 return new WP_REST_Response( 'Invalid JSON data', 400 );
48 }
49
50 // use php switch case to handle different webhook types. e.g. Invoice, Item and StockPositions.
51 // code starts here.
52 switch ( $data['type'] ) {
53 case 'Items':
54 // process Item webhook. Find the product based on the $data['Code'] which is the sku or post_id.
55 $product_id = wc_get_product_id_by_sku( $data['Code'] );
56 if ( ! $product_id ) {
57 $product_id = $data['Code'];
58 }
59 // Check if category exists with $item['ItemGroupDescription'], else create it and get the ID.
60 if ( isset( $data['ItemGroupDescription'] ) ) {
61 $term = get_term_by( 'name', $data['ItemGroupDescription'], 'product_cat' );
62 $term_id = $term->term_id;
63 if ( empty( $term_id ) ) {
64 $term = wp_insert_term(
65 $data['ItemGroupDescription'],
66 'product_cat',
67 array(
68 'parent' => 0,
69 )
70 );
71 $term_id = $term['term_id'];
72 }
73 } else {
74 $term_id = 0;
75 }
76 $product = wc_get_product( $product_id );
77 if ( $product ) {
78 $product->set_stock_quantity( $data['Stock'] );
79 // update the product price if needed.
80 $product->set_price( $data['StandardSalesPrice'] );
81 $product->set_regular_price( $data['StandardSalesPrice'] );
82 // add meta data cost_price.
83 $product->update_meta_data( '_cost_price', $data['CostPriceStandard'] );
84 // set the category.
85 $product->set_category_ids( array_map( 'intval', array( $term_id ) ) );
86 $product->save();
87 } else {
88 // if product not found, create a new product if the $data['Webshop'] is set to true.
89 if ( ! $data['IsWebshopItem'] ) {
90 break;
91 }
92 $product = new WC_Product();
93 $product->set_name( $data['Description'] );
94 $product->set_sku( $data['Code'] );
95 $product->set_price( $data['StandardSalesPrice'] );
96 $product->set_regular_price( $data['StandardSalesPrice'] );
97 if ( $data['Stock'] > 0 ) {
98 $product->set_manage_stock( true );
99 $product->set_stock_quantity( $data['Stock'] );
100 $product->set_stock_status( 'instock' );
101 }
102 // set the category.
103 $product->set_category_ids( array_map( 'intval', array( $term_id ) ) );
104 $product->set_status( 'draft' );
105 $product->set_category_ids( array_map( 'intval', array( $term_id ) ) );
106 $product->save();
107 }
108 break;
109 case 'StockPositions':
110 // process StockPositions webhook.
111 // get the "ItemId" from $data['ItemId'] and find the product based on product meta key "eo_item_id".
112 $item_id = $data['ItemId'];
113 // find the product id based on meta key "eo_item_id" which has the value of $item_id using WP query.
114 $args = array(
115 'post_type' => 'product',
116 'meta_query' => array(
117 array(
118 'key' => 'eo_item_id',
119 'value' => $item_id,
120 ),
121 ),
122 );
123 $query = new WP_Query( $args );
124 if ( $query->have_posts() ) {
125 while ( $query->have_posts() ) {
126 $query->the_post();
127 $product_id = get_the_ID();
128 $product = wc_get_product( $product_id );
129
130 if ( $product ) {
131 $product->set_manage_stock( true );
132 $product->set_stock_quantity( intval( $data['FreeStock'] ) );
133 $product->save();
134 // get new product instance.
135 $product = wc_get_product( $product_id );
136 // get stock quantity of product, if its 0, set stock status to outofstock.
137 if ( $product->get_stock_quantity() <= 0 ) {
138 $product->set_stock_status( 'outofstock' );
139 } else {
140 $stock_status = $product->backorders_allowed() ? 'onbackorder' : 'outofstock';
141 $product->set_stock_status( $stock_status );
142 }
143 $product->save();
144 // Clear cache.
145 wc_delete_product_transients( $product_id );
146 }
147 }
148 wp_reset_postdata();
149 }
150 break;
151 default:
152 // process default webhook.
153 break;
154 }
155 // code ends here.
156 return new WP_REST_Response( 'Webhook processed', 200 );
157 }
158 }
159