apis
1 year ago
purchase-orders
1 year ago
zoho-crm
1 year ago
zoho-inventory
1 year ago
class-api-handler-zoho.php
1 year ago
class-auth-zoho.php
1 year ago
class-common.php
1 year ago
class-plugin.php
1 year ago
class-wc-api.php
1 year ago
class-webhook-modify.php
1 year ago
class-webhook-modify.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * All code related to modifying the Webhook Payloads for CommerceBird API. |
| 5 | * |
| 6 | * @author Fawad Tiemoerie <info@roadmapstudios.com> |
| 7 | * @license GNU General Public License v3.0 |
| 8 | * @link https://commercebird.com |
| 9 | * @since 2.0.0 |
| 10 | * @version 2.0.0 |
| 11 | * @category Webhook |
| 12 | * @package CommerceBird |
| 13 | */ |
| 14 | |
| 15 | // namespace CommerceBird; |
| 16 | |
| 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | exit; |
| 19 | } |
| 20 | |
| 21 | class CMBIRD_Webhook_Modify { |
| 22 | |
| 23 | protected static ?self $instance = null; |
| 24 | /** |
| 25 | * Get class instance. |
| 26 | * |
| 27 | * @return object Instance. |
| 28 | */ |
| 29 | final public static function instance(): self { |
| 30 | if ( null === static::$instance ) { |
| 31 | static::$instance = new static(); |
| 32 | } |
| 33 | |
| 34 | return static::$instance; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Webhook_Modify constructor. |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | $this->init_hooks(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Initialize all hooks. |
| 46 | */ |
| 47 | public function init_hooks() { |
| 48 | add_filter( 'woocommerce_webhook_payload', array( $this, 'cm_modify_webhook_payload' ), 10, 4 ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Modify the Webhook Payload |
| 53 | * @param $payload |
| 54 | * @return mixed |
| 55 | * @since 2.0.0 |
| 56 | */ |
| 57 | public function cm_modify_webhook_payload( $payload, $resource, $resource_id, $id ) { |
| 58 | $webhook = wc_get_webhook( $id ); |
| 59 | if ( $webhook && $webhook->get_name() === 'CommerceBird Customers Update' ) { |
| 60 | $new_payload = $this->cm_modify_customer_webhook_payload( $payload ); |
| 61 | return $new_payload; |
| 62 | } |
| 63 | if ( $webhook && $webhook->get_name() !== 'CommerceBird Orders' ) { |
| 64 | $new_payload = $this->cm_modify_order_webhook_payload( $payload ); |
| 65 | return $new_payload; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Modify the Customer Webhook Payload |
| 71 | * @param $payload |
| 72 | * @return mixed |
| 73 | * @since 2.0.0 |
| 74 | * @throws \Exception |
| 75 | */ |
| 76 | public function cm_modify_customer_webhook_payload( $payload ) { |
| 77 | |
| 78 | $customer_id = $payload['arg']; |
| 79 | if ( ! $customer_id ) { |
| 80 | return $payload; |
| 81 | } |
| 82 | |
| 83 | $endpoint = '/wc/v3/customers/' . $customer_id; |
| 84 | $request = new \WP_REST_Request( 'GET', $endpoint ); |
| 85 | $response = rest_do_request( $request ); |
| 86 | $data = $response->get_data(); |
| 87 | |
| 88 | // Check if the request was successful |
| 89 | if ( is_wp_error( $response ) ) { |
| 90 | // Handle error |
| 91 | $error_message = $response->get_error_message(); |
| 92 | throw new \Exception( esc_html( $error_message ) ); |
| 93 | } else { |
| 94 | unset( $payload['action'], $payload['arg'] ); |
| 95 | // attach all customer details to the payload |
| 96 | $payload = $data; |
| 97 | } |
| 98 | |
| 99 | return $payload; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Modify the Order Webhook Payload |
| 104 | * @param $payload |
| 105 | * @return mixed |
| 106 | * @since 2.0.0 |
| 107 | * @throws \Exception |
| 108 | */ |
| 109 | public function cm_modify_order_webhook_payload( $payload ) { |
| 110 | $eo_account_id = ''; |
| 111 | $customer_id = (int) $payload['customer_id']; |
| 112 | |
| 113 | // All guest users will have the customer_id field set to 0 |
| 114 | if ( $customer_id > 0 ) { |
| 115 | $eo_account_id = (string) get_user_meta( $customer_id, 'eo_account_id', true ); |
| 116 | if ( ! empty( $eo_account_id ) ) { |
| 117 | $payload['meta_data'][] = array( |
| 118 | 'key' => 'eo_account_id', |
| 119 | 'value' => $eo_account_id, |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | // Loop through line items in and add the eo_item_id to the line item |
| 124 | foreach ( $payload['line_items'] as &$item ) { |
| 125 | // Get the product ID associated with the line item |
| 126 | $product_id = $item['product_id']; |
| 127 | $variation_id = $item['variation_id']; |
| 128 | // Get the product meta value based on the product ID and meta key |
| 129 | if ( $variation_id ) { |
| 130 | $eo_item_id = get_post_meta( $variation_id, 'eo_item_id', true ); |
| 131 | } else { |
| 132 | $eo_item_id = get_post_meta( $product_id, 'eo_item_id', true ); |
| 133 | } |
| 134 | // Add the product meta to the line item |
| 135 | if ( ! empty( $eo_item_id ) ) { |
| 136 | $item['meta'][] = array( |
| 137 | 'key' => 'eo_item_id', |
| 138 | 'value' => $eo_item_id, |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Add mapped Zoho CRM custom fields to the payload. |
| 144 | $custom_fields = get_option( 'sales_orderscustom_fields' ); |
| 145 | if ( ! empty( $custom_fields ) ) { |
| 146 | $payload['custom_fields'] = $custom_fields; |
| 147 | } |
| 148 | |
| 149 | return $payload; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return new CMBIRD_Webhook_Modify(); |
| 154 |