class-api-for-cmbird.php
2 months ago
class-api-for-exact-webhooks.php
4 months ago
class-api-for-product-webhook.php
2 months ago
class-api-for-shipping-status.php
9 months ago
class-api-for-woo-order.php
4 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
5 months ago
index.php
1 year ago
trait-api-permission.php
7 months ago
class-api-for-exact-webhooks.php
176 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', 'a+' ); |
| 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 | $args = array( |
| 59 | 'limit' => 1, |
| 60 | 'sku' => $data['Code'], |
| 61 | 'status' => array( 'publish', 'draft', 'pending', 'private' ), |
| 62 | 'type' => array( 'simple', 'variable', 'variation' ), |
| 63 | 'return' => 'ids', |
| 64 | ); |
| 65 | $product_ids = wc_get_products( $args ); |
| 66 | $product_id = $product_ids ? $product_ids[0] : null; |
| 67 | // Check if category exists with $item['ItemGroupDescription'], else create it and get the ID. |
| 68 | if ( isset( $data['ItemGroupDescription'] ) ) { |
| 69 | $term = get_term_by( 'name', $data['ItemGroupDescription'], 'product_cat' ); |
| 70 | $term_id = $term->term_id; |
| 71 | if ( empty( $term_id ) ) { |
| 72 | $term = wp_insert_term( |
| 73 | $data['ItemGroupDescription'], |
| 74 | 'product_cat', |
| 75 | array( |
| 76 | 'parent' => 0, |
| 77 | ) |
| 78 | ); |
| 79 | $term_id = $term['term_id']; |
| 80 | } |
| 81 | } else { |
| 82 | $term_id = 0; |
| 83 | } |
| 84 | $product = wc_get_product( $product_id ); |
| 85 | if ( $product ) { |
| 86 | $product->set_name( $data['Description'] ); |
| 87 | $product->set_stock_quantity( $data['Stock'] ); |
| 88 | // update the product price if needed. |
| 89 | $product->set_price( $data['StandardSalesPrice'] ); |
| 90 | $product->set_regular_price( $data['StandardSalesPrice'] ); |
| 91 | if ( $data['Stock'] > 0 ) { |
| 92 | $product->set_manage_stock( true ); |
| 93 | $product->set_stock_quantity( $data['Stock'] ); |
| 94 | $product->set_stock_status( 'instock' ); |
| 95 | } else { |
| 96 | $stock_status = $product->backorders_allowed() ? 'onbackorder' : 'outofstock'; |
| 97 | $product->set_stock_status( $stock_status ); |
| 98 | } |
| 99 | // add meta data cost_price. |
| 100 | $product->update_meta_data( '_cogs_total_value', $data['CostPriceStandard'] ); |
| 101 | // set the category. |
| 102 | $product->set_category_ids( array_map( 'intval', array( $term_id ) ) ); |
| 103 | $product->save(); |
| 104 | // Clear cache. |
| 105 | wc_delete_product_transients( $product_id ); |
| 106 | } else { |
| 107 | // if product not found, create a new product if the $data['Webshop'] is set to true. |
| 108 | if ( 0 === $data['IsWebshopItem'] ) { |
| 109 | return new WP_REST_Response( 'Product is not a webshop item', 404 ); |
| 110 | } |
| 111 | $product = new WC_Product(); |
| 112 | $product->set_name( $data['Description'] ); |
| 113 | $product->set_sku( $data['Code'] ); |
| 114 | $product->set_price( $data['StandardSalesPrice'] ); |
| 115 | $product->set_regular_price( $data['StandardSalesPrice'] ); |
| 116 | if ( $data['Stock'] > 0 ) { |
| 117 | $product->set_manage_stock( true ); |
| 118 | $product->set_stock_quantity( $data['Stock'] ); |
| 119 | $product->set_stock_status( 'instock' ); |
| 120 | } |
| 121 | // set the category. |
| 122 | $product->set_category_ids( array_map( 'intval', array( $term_id ) ) ); |
| 123 | $status = get_option( 'cmbird_enable_eo_webhook_publish', false ) ? 'publish' : 'draft'; |
| 124 | $product->set_status( $status ); |
| 125 | $product->save(); |
| 126 | } |
| 127 | break; |
| 128 | case 'StockPositions': |
| 129 | // process StockPositions webhook. |
| 130 | // get the "ItemId" from $data['ItemId'] and find the product based on product meta key "eo_item_id". |
| 131 | $item_id = $data['ItemId']; |
| 132 | // find the product id based on meta key "eo_item_id" which has the value of $item_id using WP query. |
| 133 | $args = array( |
| 134 | 'post_type' => array( 'product', 'product_variation' ), |
| 135 | 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), |
| 136 | 'posts_per_page' => -1, |
| 137 | 'fields' => 'ids', |
| 138 | 'no_found_rows' => true, |
| 139 | 'update_post_meta_cache' => false, |
| 140 | 'update_post_term_cache' => false, |
| 141 | 'meta_query' => array( |
| 142 | array( |
| 143 | 'key' => 'eo_item_id', |
| 144 | 'value' => (string) $item_id, |
| 145 | ), |
| 146 | ), |
| 147 | ); |
| 148 | $query = new WP_Query( $args ); |
| 149 | |
| 150 | if ( ! empty( $query->posts ) ) { |
| 151 | foreach ( $query->posts as $product_id ) { |
| 152 | $product = wc_get_product( $product_id ); |
| 153 | if ( ! $product ) { |
| 154 | continue; |
| 155 | } |
| 156 | $product->set_manage_stock( true ); |
| 157 | $product->set_stock_quantity( absint( $data['FreeStock'] ?? 0 ) ); |
| 158 | $product->set_stock_status( |
| 159 | $product->get_stock_quantity() > 0 |
| 160 | ? 'instock' |
| 161 | : ( $product->backorders_allowed() ? 'onbackorder' : 'outofstock' ) |
| 162 | ); |
| 163 | $product->save(); |
| 164 | wc_delete_product_transients( $product_id ); |
| 165 | } |
| 166 | } |
| 167 | break; |
| 168 | default: |
| 169 | // process default webhook. |
| 170 | break; |
| 171 | } |
| 172 | // code ends here. |
| 173 | return new WP_REST_Response( 'Webhook processed', 200 ); |
| 174 | } |
| 175 | } |
| 176 |