order-backend.php
325 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * All backend order sync related functions. |
| 5 | * |
| 6 | * @category Fulfillment |
| 7 | * @package commercebird |
| 8 | * @author Roadmap Studios <info@roadmapstudios.com> |
| 9 | * @license GNU General Public License v3.0 |
| 10 | * @link https://commercebird.com |
| 11 | */ |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 18 | // use CommerceBird\Admin\Actions\Ajax\ZohoInventoryAjax; |
| 19 | use CommerceBird\Admin\Actions\Ajax\SettingsAjax; |
| 20 | |
| 21 | /** |
| 22 | * Loading admin order sync script. |
| 23 | */ |
| 24 | function cmbird_load_script() { |
| 25 | if ( is_admin() ) { |
| 26 | $screen = get_current_screen(); |
| 27 | if ( $screen->id === 'product' || $screen->id === 'shop_order' || $screen->id === 'woocommerce_page_wc-orders' ) { |
| 28 | wp_enqueue_script( 'zoho-admin-order-ajax-script', CMBIRD_URL . 'admin/js/zoho_admin_order_ajax.js', array( 'jquery' ), CMBIRD_VERSION, true ); |
| 29 | wp_register_script( 'sweatAlert', CMBIRD_URL . 'admin/js/sweetalert.min.js', array( 'jquery' ), CMBIRD_VERSION, true ); |
| 30 | wp_enqueue_script( 'sweatAlert' ); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | add_action( 'admin_enqueue_scripts', 'cmbird_load_script' ); |
| 35 | |
| 36 | function cmbird_zoho_admin_metabox() { |
| 37 | $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' ); |
| 38 | if ( empty( $zoho_inventory_access_token ) ) { |
| 39 | return; |
| 40 | } |
| 41 | $screen = wc_get_container()->get( CustomOrdersTableController::class)->custom_orders_table_usage_is_enabled() |
| 42 | ? 'woocommerce_page_wc-orders' |
| 43 | : 'shop_order'; |
| 44 | |
| 45 | add_meta_box( |
| 46 | 'zoho-admin-sync', |
| 47 | 'Sync Order to Zoho', |
| 48 | 'cmbird_admin_metabox_callback', |
| 49 | $screen, |
| 50 | 'side', |
| 51 | 'high' |
| 52 | ); |
| 53 | } |
| 54 | add_action( 'add_meta_boxes', 'cmbird_zoho_admin_metabox' ); |
| 55 | |
| 56 | function cmbird_admin_metabox_callback( $post_or_order_object ) { |
| 57 | $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' ); |
| 58 | if ( empty( $zoho_inventory_access_token ) ) { |
| 59 | return; |
| 60 | } |
| 61 | // $subscription = new ZohoInventoryAjax(); |
| 62 | // $data = $subscription->get_subscription_data(); |
| 63 | $subscription = new SettingsAjax(); |
| 64 | $data = $subscription->get_subscription_data(); |
| 65 | $status = $data['status']; |
| 66 | // Flag to check if 'ZohoInventory' is found |
| 67 | $found = false; |
| 68 | // Loop through fee_lines and check for the 'name' key |
| 69 | if ( isset( $data['fee_lines'] ) && is_array( $data['fee_lines'] ) ) { |
| 70 | foreach ( $data['fee_lines'] as $fee_line ) { |
| 71 | if ( isset( $fee_line['name'] ) && ( $fee_line['name'] === 'ZohoInventory' || $fee_line['name'] === 'ZohoCRM' ) ) { |
| 72 | $found = true; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | $order = ( $post_or_order_object instanceof WP_Post ) ? wc_get_order( $post_or_order_object->ID ) : wc_get_order( $post_or_order_object->get_id() ); |
| 78 | $userid = $order->get_user_id(); |
| 79 | $order_id = $order->get_id(); |
| 80 | if ( $found && 'active' === $status ) { |
| 81 | $nonce_order = wp_create_nonce( 'zoho_admin_order_sync' ); |
| 82 | echo '<a href="javascript:void(0)" style="width:100%; text-align: center;" |
| 83 | class="button save_order button-primary" onclick="zoho_admin_order_ajax(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce_order ) . '\')">Sync Order</a>'; |
| 84 | if ( $userid ) { |
| 85 | $nonce = wp_create_nonce( 'zi_customer_unmap_hook' ); |
| 86 | echo '<br><p style="color:red;">Click on below button if you are seeing the error "Billing AddressID passed is invalid"</p>'; |
| 87 | echo '<a href="javascript:void(0)" style="width:100%; text-align: center;" |
| 88 | class="button customer_unmap" onclick="zoho_admin_customer_unmap(' . esc_attr( $order_id ) . ', \'' . esc_attr( $nonce ) . '\')">Unmap Customer</a>'; |
| 89 | } |
| 90 | } else { |
| 91 | echo '<p style="color:red;">Please activate the Zoho Inventory Integration</p>'; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Bulk-action to sync orders from WooCommerce to Zoho |
| 97 | * @param: $bulk_array |
| 98 | */ |
| 99 | add_filter( 'bulk_actions-woocommerce_page_wc-orders', 'cmbird_zi_sync_all_orders_to_zoho', 10, 1 ); |
| 100 | function cmbird_zi_sync_all_orders_to_zoho( $actions ) { |
| 101 | $actions['sync_order_to_zoho'] = __( 'Sync to Zoho', 'commercebird' ); |
| 102 | return $actions; |
| 103 | } |
| 104 | |
| 105 | add_filter( 'handle_bulk_actions-woocommerce_page_wc-orders', 'cmbird_zi_sync_all_orders_to_zoho_handler', 10, 3 ); |
| 106 | function cmbird_zi_sync_all_orders_to_zoho_handler( $redirect, $action, $object_ids ) { |
| 107 | if ( 'sync_order_to_zoho' !== $action ) { |
| 108 | return $redirect; // Exit |
| 109 | } |
| 110 | // let's remove query args first |
| 111 | $redirect = remove_query_arg( 'sync_order_to_zoho_done', $redirect ); |
| 112 | |
| 113 | // do something for "Make Draft" bulk action |
| 114 | if ( 'sync_order_to_zoho' === $action ) { |
| 115 | |
| 116 | foreach ( $object_ids as $post_id ) { |
| 117 | $order_sync = new CMBIRD_Order_Sync_ZI(); |
| 118 | $order_sync->zi_order_sync( $post_id ); |
| 119 | } |
| 120 | |
| 121 | // do not forget to add query args to URL because we will show notices later |
| 122 | $redirect = add_query_arg( 'sync_order_to_zoho_done', count( $object_ids ), $redirect ); |
| 123 | } |
| 124 | |
| 125 | return $redirect; |
| 126 | } |
| 127 | |
| 128 | // output the message of bulk action |
| 129 | add_action( 'admin_notices', 'cmbird_sync_order_to_zoho_notices' ); |
| 130 | function cmbird_sync_order_to_zoho_notices() { |
| 131 | // verify nonce |
| 132 | if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'bulk-orders' ) ) { |
| 133 | return; |
| 134 | } |
| 135 | if ( ! empty( $_REQUEST['sync_order_to_zoho_done'] ) ) { |
| 136 | echo '<div id="message" class="updated notice is-dismissible"> |
| 137 | <p>Orders Synced. If order is not synced, please click on Edit Order to see the API response.</p> |
| 138 | </div>'; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Add the product meta as line item meta to the Order Webhook Payload |
| 144 | * @param: $payload |
| 145 | * @param: $resource |
| 146 | * @param: $resource_id |
| 147 | * @param: $id |
| 148 | * @return: $payload |
| 149 | */ |
| 150 | add_filter( 'woocommerce_webhook_payload', 'cmbird_modify_order_webhook_payload', 10, 4 ); |
| 151 | function cmbird_modify_order_webhook_payload( $payload, $resource, $resource_id, $id ) { |
| 152 | $webhook = wc_get_webhook( $id ); |
| 153 | |
| 154 | // if webhook name contains 'CommerceBird Customers' using strpos, then add the customer_id to the payload |
| 155 | $customers_webhook_name = 'CommerceBird Customers'; |
| 156 | $webhook_name = $webhook->get_name(); |
| 157 | if ( $webhook && strpos( $webhook_name, $customers_webhook_name ) !== false ) { |
| 158 | // include eo_gl_account and eo_account_id in the payload |
| 159 | $customer_id = (int) $payload['id']; |
| 160 | $eo_gl_account = get_user_meta( $customer_id, 'eo_gl_account', true ); |
| 161 | $eo_account_id = get_user_meta( $customer_id, 'eo_account_id', true ); |
| 162 | $eo_contact_id = get_user_meta( $customer_id, 'eo_contact_id', true ); |
| 163 | if ( ! empty( $eo_gl_account ) ) { |
| 164 | $payload['meta_data'][] = array( |
| 165 | 'key' => 'eo_gl_account', |
| 166 | 'value' => $eo_gl_account, |
| 167 | ); |
| 168 | } |
| 169 | if ( ! empty( $eo_account_id ) ) { |
| 170 | $payload['meta_data'][] = array( |
| 171 | 'key' => 'eo_account_id', |
| 172 | 'value' => $eo_account_id, |
| 173 | ); |
| 174 | } |
| 175 | if ( ! empty( $eo_contact_id ) ) { |
| 176 | $payload['meta_data'][] = array( |
| 177 | 'key' => 'eo_contact_id', |
| 178 | 'value' => $eo_contact_id, |
| 179 | ); |
| 180 | } |
| 181 | return $payload; |
| 182 | } |
| 183 | |
| 184 | if ( $webhook && 'CommerceBird Orders' !== $webhook_name ) { |
| 185 | return $payload; |
| 186 | } |
| 187 | |
| 188 | $eo_account_id = ''; |
| 189 | $customer_id = isset( $payload['customer_id'] ) ? (int) $payload['customer_id'] : 0; |
| 190 | |
| 191 | // All guest users will have the customer_id field set to 0 |
| 192 | if ( $customer_id > 0 ) { |
| 193 | $eo_account_id = (string) get_user_meta( $customer_id, 'eo_account_id', true ); |
| 194 | if ( ! empty( $eo_account_id ) ) { |
| 195 | $payload['meta_data'][] = array( |
| 196 | 'key' => 'eo_account_id', |
| 197 | 'value' => $eo_account_id, |
| 198 | ); |
| 199 | } |
| 200 | } |
| 201 | // add eo_order_id to the meta_data array |
| 202 | $order_object = wc_get_order( $resource_id ); |
| 203 | $eo_order_id = $order_object->get_meta( 'eo_order_id', true ); |
| 204 | if ( ! empty( $eo_order_id ) ) { |
| 205 | $payload['meta_data'][] = array( |
| 206 | 'key' => 'eo_order_id', |
| 207 | 'value' => $eo_order_id, |
| 208 | ); |
| 209 | } |
| 210 | // do same for eo_invoice_id |
| 211 | $eo_invoice_id = $order_object->get_meta( 'eo_invoice_id', true ); |
| 212 | if ( ! empty( $eo_invoice_id ) ) { |
| 213 | $payload['meta_data'][] = array( |
| 214 | 'key' => 'eo_invoice_id', |
| 215 | 'value' => $eo_invoice_id, |
| 216 | ); |
| 217 | } |
| 218 | // Loop through line items in and add the eo_item_id to the line item |
| 219 | if ( isset( $payload['line_items'] ) && is_array( $payload['line_items'] ) ) { |
| 220 | foreach ( $payload['line_items'] as &$item ) { |
| 221 | // Get the product ID associated with the line item |
| 222 | $product_id = $item['product_id']; |
| 223 | $variation_id = $item['variation_id']; |
| 224 | // Get the product meta value based on the product ID and meta key |
| 225 | if ( $variation_id ) { |
| 226 | $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true ); |
| 227 | } else { |
| 228 | $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true ); |
| 229 | } |
| 230 | // Add the product meta to the line item |
| 231 | if ( ! empty( $eo_item_id ) ) { |
| 232 | $item['meta'][] = array( |
| 233 | 'key' => 'eo_item_id', |
| 234 | 'value' => $eo_item_id, |
| 235 | ); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return $payload; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Prevent Webhook delivery execution when order gets updated too often per minute |
| 245 | * @param bool $should_deliver |
| 246 | * @param WC_Webhook $webhook |
| 247 | * @param array $arg |
| 248 | * @return bool |
| 249 | */ |
| 250 | add_filter( 'woocommerce_webhook_should_deliver', 'cmbird_skip_webhook_delivery', 10, 3 ); |
| 251 | function cmbird_skip_webhook_delivery( $should_deliver, $webhook, $arg ) { |
| 252 | |
| 253 | $cmbird_orders = 'CommerceBird Orders'; |
| 254 | if ( $webhook->get_name() === $cmbird_orders ) { |
| 255 | $order = wc_get_order( $arg ); |
| 256 | // check if order status is failed, pending, on-hold or cancelled |
| 257 | $order_status = $order->get_status(); |
| 258 | if ( in_array( $order_status, array( 'failed', 'pending', 'on-hold', 'cancelled', 'auto-draft' ) ) ) { |
| 259 | $should_deliver = false; |
| 260 | } |
| 261 | // check if order contains meta data for eo_order_id |
| 262 | $eo_order_id = $order->get_meta( 'eo_order_id', true ); |
| 263 | if ( ! empty( $eo_order_id ) && 'refunded' !== $order_status ) { |
| 264 | $should_deliver = false; |
| 265 | } |
| 266 | // check if order status is processing and webhook status is disabled or paused |
| 267 | $webhook_status = $webhook->get_status(); |
| 268 | // also return false if webhoook status is disabled or paused |
| 269 | if ( $webhook_status === 'disabled' || $webhook_status === 'paused' ) { |
| 270 | $should_deliver = false; |
| 271 | } |
| 272 | // Check if the order is older than 2 months, return false if so |
| 273 | // first check if site_url is perfecthealth.nl |
| 274 | $site_url = get_site_url(); |
| 275 | if ( strpos( $site_url, 'perfecthealth.nl' ) === false ) { |
| 276 | return $should_deliver; |
| 277 | } |
| 278 | $current_date = new DateTime(); |
| 279 | $order_date = $order->get_date_created(); |
| 280 | |
| 281 | if ( $order_date instanceof WC_DateTime ) { |
| 282 | $two_months_ago = $current_date->modify( '-4 months' ); |
| 283 | if ( $order_date < $two_months_ago ) { |
| 284 | $should_deliver = false; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | $cmbird_customers = 'CommerceBird Customers Update'; |
| 289 | if ( $webhook->get_name() === $cmbird_customers ) { |
| 290 | $customer_id = $arg['customer_id']; |
| 291 | $last_order_id = wc_get_customer_last_order( $customer_id ); |
| 292 | // if last order created date is less than 5 minutes, then return false |
| 293 | if ( $last_order_id ) { |
| 294 | $last_order = wc_get_order( $last_order_id ); |
| 295 | $last_order_date = $last_order->get_date_created(); |
| 296 | $current_date = new DateTime(); |
| 297 | $interval = $current_date->diff( $last_order_date ); |
| 298 | $minutes = $interval->i; |
| 299 | if ( $minutes < 5 ) { |
| 300 | $should_deliver = false; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return $should_deliver; // Continue with normal webhook delivery |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Update Customer Meta Data when Order is updated |
| 310 | * @param $order_id |
| 311 | * |
| 312 | */ |
| 313 | function cmbird_update_customer_meta( $order_id ) { |
| 314 | $order = wc_get_order( $order_id ); |
| 315 | $customer_id = $order->get_user_id(); |
| 316 | $eo_gl_account = $order->get_meta( 'glaccount', true ); |
| 317 | if ( ! empty( $eo_gl_account ) ) { |
| 318 | // get the value of the glaccount meta, which is everything before : in the value |
| 319 | $string = $eo_gl_account; |
| 320 | $value = strstr( $string, ':', true ); |
| 321 | update_user_meta( $customer_id, 'eo_gl_account', trim( $value ) ); |
| 322 | } |
| 323 | } |
| 324 | add_action( 'woocommerce_update_order', 'cmbird_update_customer_meta' ); |
| 325 |