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