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