woocommerce.php
522 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce integration class file |
| 4 | * |
| 5 | * @package SureTriggers |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | namespace SureTriggers\Integrations\WooCommerce; |
| 10 | |
| 11 | use SureTriggers\Controllers\IntegrationsController; |
| 12 | use SureTriggers\Integrations\Integrations; |
| 13 | use SureTriggers\Traits\SingletonLoader; |
| 14 | use WC_Order; |
| 15 | use WC_Customer; |
| 16 | |
| 17 | /** |
| 18 | * Class WooCommerce |
| 19 | * |
| 20 | * @package SureTriggers\Integrations\WooCommerce |
| 21 | */ |
| 22 | class WooCommerce extends Integrations { |
| 23 | |
| 24 | use SingletonLoader; |
| 25 | |
| 26 | /** |
| 27 | * ID of the integration |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $id = 'WooCommerce'; |
| 32 | |
| 33 | /** |
| 34 | * SureTrigger constructor. |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | add_action( 'woocommerce_created_customer', [ $this, 'woo_customer_created_trigger' ], 10, 3 ); |
| 38 | add_action( 'woocommerce_thankyou', [ $this, 'woo_customer_created_order_trigger' ], 10, 1 ); |
| 39 | parent::__construct(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * On form submit. |
| 44 | * |
| 45 | * @param int $customer_id New customer (user) ID. |
| 46 | * @param array $new_customer_data Array of customer (user) data. |
| 47 | * @param string $password_generated The generated password for the account. |
| 48 | * @return void |
| 49 | */ |
| 50 | public function woo_customer_created_trigger( $customer_id, $new_customer_data, $password_generated ) { |
| 51 | // Check if customer creation is happening during checkout. |
| 52 | if ( isset( $_POST['woocommerce-process-checkout-nonce'] ) && ! empty( $_POST['woocommerce-process-checkout-nonce'] ) && ! wp_verify_nonce( sanitize_key( $_POST['woocommerce-process-checkout-nonce'] ), 'woocommerce-process_checkout' ) ) { |
| 53 | // Customer creation is happening during checkout, so return it. |
| 54 | return; |
| 55 | } |
| 56 | $customer_query = get_users( |
| 57 | [ |
| 58 | 'fields' => 'ID', |
| 59 | 'include' => [ $customer_id ], |
| 60 | ] |
| 61 | ); |
| 62 | $results = $customer_query; |
| 63 | if ( ! empty( $results ) ) { |
| 64 | $customer = new WC_Customer( $results[0] ); |
| 65 | $last_order = $customer->get_last_order(); |
| 66 | $customer_data = [ |
| 67 | 'id' => $customer->get_id(), |
| 68 | 'email' => $customer->get_email(), |
| 69 | 'first_name' => $customer->get_first_name(), |
| 70 | 'last_name' => $customer->get_last_name(), |
| 71 | 'username' => $customer->get_username(), |
| 72 | 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null, |
| 73 | 'orders_count' => $customer->get_order_count(), |
| 74 | 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ), |
| 75 | 'avatar_url' => $customer->get_avatar_url(), |
| 76 | 'billing_address' => [ |
| 77 | 'first_name' => $customer->get_billing_first_name(), |
| 78 | 'last_name' => $customer->get_billing_last_name(), |
| 79 | 'company' => $customer->get_billing_company(), |
| 80 | 'address_1' => $customer->get_billing_address_1(), |
| 81 | 'address_2' => $customer->get_billing_address_2(), |
| 82 | 'city' => $customer->get_billing_city(), |
| 83 | 'state' => $customer->get_billing_state(), |
| 84 | 'postcode' => $customer->get_billing_postcode(), |
| 85 | 'country' => $customer->get_billing_country(), |
| 86 | 'email' => $customer->get_billing_email(), |
| 87 | 'phone' => $customer->get_billing_phone(), |
| 88 | ], |
| 89 | 'shipping_address' => [ |
| 90 | 'first_name' => $customer->get_shipping_first_name(), |
| 91 | 'last_name' => $customer->get_shipping_last_name(), |
| 92 | 'company' => $customer->get_shipping_company(), |
| 93 | 'address_1' => $customer->get_shipping_address_1(), |
| 94 | 'address_2' => $customer->get_shipping_address_2(), |
| 95 | 'city' => $customer->get_shipping_city(), |
| 96 | 'state' => $customer->get_shipping_state(), |
| 97 | 'postcode' => $customer->get_shipping_postcode(), |
| 98 | 'country' => $customer->get_shipping_country(), |
| 99 | ], |
| 100 | ]; |
| 101 | if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) { |
| 102 | $created_date = $last_order->get_date_created(); |
| 103 | if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) { |
| 104 | $last_order_date = $created_date->getTimestamp(); |
| 105 | $customer_data['created_at'] = $last_order_date; |
| 106 | $customer_data['last_order_date'] = $last_order_date; |
| 107 | } |
| 108 | } |
| 109 | $context = $customer_data; |
| 110 | do_action( 'wc_customer_created_trigger', $context ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * On form submit. |
| 116 | * |
| 117 | * @param int $order_id New customer (user) ID. |
| 118 | * @return void |
| 119 | */ |
| 120 | public function woo_customer_created_order_trigger( $order_id ) { |
| 121 | if ( ! $order_id ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | $order = wc_get_order( $order_id ); |
| 126 | |
| 127 | if ( ! $order ) { |
| 128 | return; |
| 129 | } |
| 130 | if ( is_object( $order ) && method_exists( $order, 'get_customer_id' ) ) { |
| 131 | $customer = new WC_Customer( $order->get_customer_id() ); |
| 132 | if ( $customer->get_order_count() > 1 ) { |
| 133 | return; |
| 134 | } |
| 135 | $last_order = $customer->get_last_order(); |
| 136 | $customer_data = [ |
| 137 | 'id' => $customer->get_id(), |
| 138 | 'email' => $customer->get_email(), |
| 139 | 'first_name' => $customer->get_first_name(), |
| 140 | 'last_name' => $customer->get_last_name(), |
| 141 | 'username' => $customer->get_username(), |
| 142 | 'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null, |
| 143 | 'orders_count' => $customer->get_order_count(), |
| 144 | 'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ), |
| 145 | 'avatar_url' => $customer->get_avatar_url(), |
| 146 | 'billing_address' => [ |
| 147 | 'first_name' => $customer->get_billing_first_name(), |
| 148 | 'last_name' => $customer->get_billing_last_name(), |
| 149 | 'company' => $customer->get_billing_company(), |
| 150 | 'address_1' => $customer->get_billing_address_1(), |
| 151 | 'address_2' => $customer->get_billing_address_2(), |
| 152 | 'city' => $customer->get_billing_city(), |
| 153 | 'state' => $customer->get_billing_state(), |
| 154 | 'postcode' => $customer->get_billing_postcode(), |
| 155 | 'country' => $customer->get_billing_country(), |
| 156 | 'email' => $customer->get_billing_email(), |
| 157 | 'phone' => $customer->get_billing_phone(), |
| 158 | ], |
| 159 | 'shipping_address' => [ |
| 160 | 'first_name' => $customer->get_shipping_first_name(), |
| 161 | 'last_name' => $customer->get_shipping_last_name(), |
| 162 | 'company' => $customer->get_shipping_company(), |
| 163 | 'address_1' => $customer->get_shipping_address_1(), |
| 164 | 'address_2' => $customer->get_shipping_address_2(), |
| 165 | 'city' => $customer->get_shipping_city(), |
| 166 | 'state' => $customer->get_shipping_state(), |
| 167 | 'postcode' => $customer->get_shipping_postcode(), |
| 168 | 'country' => $customer->get_shipping_country(), |
| 169 | ], |
| 170 | ]; |
| 171 | if ( is_object( $last_order ) && method_exists( $last_order, 'get_date_created' ) ) { |
| 172 | $created_date = $last_order->get_date_created(); |
| 173 | if ( is_object( $created_date ) && method_exists( $created_date, 'getTimestamp' ) ) { |
| 174 | $last_order_date = $created_date->getTimestamp(); |
| 175 | $customer_data['created_at'] = $last_order_date; |
| 176 | $customer_data['last_order_date'] = $last_order_date; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | $context = $customer_data; |
| 181 | do_action( 'wc_customer_created_trigger', $context ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get product details context. |
| 187 | * |
| 188 | * @param object $item item. |
| 189 | * @param int $order_id ID. |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | public static function get_variable_subscription_product_context( $item, $order_id ) { |
| 194 | if ( ! $item instanceof \WC_Order_Item_Product ) { |
| 195 | return []; |
| 196 | } |
| 197 | $product = $item->get_product(); |
| 198 | $order_data = self::get_order_context( $order_id ); |
| 199 | $order_context = isset( $order_data ) ? $order_data : []; |
| 200 | if ( ! $product instanceof \WC_Product ) { |
| 201 | return []; |
| 202 | } |
| 203 | $product_data = $product->get_data(); |
| 204 | return array_merge( $order_context, $product_data ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get product details context. |
| 209 | * |
| 210 | * @param int $product_id ID. |
| 211 | * |
| 212 | * @return array |
| 213 | */ |
| 214 | public static function get_product_context( $product_id ) { |
| 215 | $product = wc_get_product( $product_id ); |
| 216 | if ( ! $product instanceof \WC_Product ) { |
| 217 | return []; |
| 218 | } |
| 219 | return array_merge( |
| 220 | [ |
| 221 | 'product_id' => $product_id, |
| 222 | 'product_sku' => $product->get_sku(), |
| 223 | ], |
| 224 | $product->get_data(), |
| 225 | $product->get_attributes() |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get product details context |
| 231 | * |
| 232 | * @param int $order_id order id. |
| 233 | * |
| 234 | * @return array|null |
| 235 | */ |
| 236 | public static function get_order_context( $order_id ) { |
| 237 | $order = wc_get_order( $order_id ); |
| 238 | if ( ! $order ) { |
| 239 | return null; |
| 240 | } |
| 241 | if ( $order instanceof WC_Order ) { |
| 242 | $coupon_codes = []; |
| 243 | $order_codes = $order->get_coupon_codes(); |
| 244 | if ( ! empty( $order_codes ) ) { |
| 245 | foreach ( $order_codes as $coupon_code ) { |
| 246 | $coupon = new \WC_Coupon( $coupon_code ); |
| 247 | $data = $coupon->get_data(); |
| 248 | $coupon_detail['code_name'] = $coupon_code; |
| 249 | $coupon_detail['discount_type'] = $coupon->get_discount_type(); |
| 250 | $coupon_detail['coupon_amount'] = $coupon->get_amount(); |
| 251 | $coupon_detail['meta_data'] = $data['meta_data']; |
| 252 | $coupon_codes[] = $coupon_detail; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | $product_ids = []; |
| 257 | $quantities = []; |
| 258 | $items = $order->get_items(); |
| 259 | foreach ( $items as $item ) { |
| 260 | if ( $item instanceof \WC_Order_Item_Product ) { |
| 261 | $product_ids[] = $item->get_product_id(); |
| 262 | $product = wc_get_product( $item->get_product_id() ); |
| 263 | if ( $product instanceof \WC_Product ) { |
| 264 | $product_skus[] = $product->get_sku(); |
| 265 | } |
| 266 | $quantities[] = $item->get_quantity(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | $discounts = $order->get_items( 'discount' ); |
| 271 | $line_items_fee = $order->get_items( 'fee' ); |
| 272 | $line_items_shipping = $order->get_items( 'shipping' ); |
| 273 | |
| 274 | $billing_country = $order->get_billing_country(); |
| 275 | $billing_state = $order->get_billing_state(); |
| 276 | if ( ! empty( $billing_country ) && ! empty( $billing_state ) ) { |
| 277 | $related_states = WC()->countries->get_states( $billing_country ); |
| 278 | if ( $related_states ) { |
| 279 | $countries_list = WC()->countries->get_countries(); |
| 280 | $billing_country = $countries_list[ $billing_country ]; |
| 281 | $billing_state = $related_states[ $billing_state ]; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | $shipping_country = $order->get_shipping_country(); |
| 286 | $shipping_state = $order->get_shipping_state(); |
| 287 | if ( ! empty( $shipping_country ) && ! empty( $shipping_state ) ) { |
| 288 | $related_shipping_states = WC()->countries->get_states( $shipping_country ); |
| 289 | if ( $related_shipping_states ) { |
| 290 | $countries_list = WC()->countries->get_countries(); |
| 291 | $shipping_country = $countries_list[ $shipping_country ]; |
| 292 | $shipping_state = $related_shipping_states[ $shipping_state ]; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // Get order data and filter out membership access granted meta. |
| 297 | $order_data = $order->get_data(); |
| 298 | if ( isset( $order_data['meta_data'] ) ) { |
| 299 | $filtered_meta_data = []; |
| 300 | foreach ( $order_data['meta_data'] as $meta ) { |
| 301 | if ( $meta instanceof \WC_Meta_Data ) { |
| 302 | $meta_key = $meta->get_data()['key']; |
| 303 | if ( '_wc_memberships_access_granted' !== $meta_key ) { |
| 304 | $filtered_meta_data[] = $meta; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | $order_data['meta_data'] = $filtered_meta_data; |
| 309 | } |
| 310 | |
| 311 | return array_merge( |
| 312 | [ 'product_id' => $product_ids[0] ], |
| 313 | $order_data, |
| 314 | self::get_order_metadata_array( $order ), |
| 315 | [ |
| 316 | 'billing_state_fullname' => $billing_state, |
| 317 | 'billing_country_fullname' => $billing_country, |
| 318 | 'shipping_state_fullname' => $shipping_state, |
| 319 | 'shipping_country_fullname' => $shipping_country, |
| 320 | ], |
| 321 | [ 'coupons' => $coupon_codes ], |
| 322 | [ 'products' => self::get_order_items_context_array( $items ) ], |
| 323 | [ 'line_items' => self::get_order_items_context( $items ) ], |
| 324 | [ 'quantity' => implode( ', ', $quantities ) ], |
| 325 | [ 'discounts' => implode( ', ', $discounts ) ], |
| 326 | [ 'line_items_fee' => implode( ', ', $line_items_fee ) ], |
| 327 | [ 'line_items_shipping' => json_decode( implode( ', ', $line_items_shipping ) ) ] |
| 328 | ); |
| 329 | } else { |
| 330 | return []; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Get order metadata as an associative array. |
| 336 | * |
| 337 | * @param object $order The order. |
| 338 | * @return array Order metadata with metakey as array key and metavalue as array value. |
| 339 | */ |
| 340 | public static function get_order_metadata_array( $order ) { |
| 341 | $metadata = []; |
| 342 | if ( ! $order instanceof \WC_Order ) { |
| 343 | return $metadata; |
| 344 | } |
| 345 | $meta_data = $order->get_meta_data(); |
| 346 | foreach ( $meta_data as $meta ) { |
| 347 | $meta_key = $meta->get_data()['key']; |
| 348 | // Skip WooCommerce Memberships access granted meta. |
| 349 | if ( '_wc_memberships_access_granted' === $meta_key ) { |
| 350 | continue; |
| 351 | } |
| 352 | $metadata[ $meta_key ] = $meta->get_data()['value']; |
| 353 | } |
| 354 | return $metadata; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Get order details context. |
| 359 | * |
| 360 | * @param string $order_id order_id. |
| 361 | * |
| 362 | * @return array|null |
| 363 | */ |
| 364 | public static function get_only_order_context( $order_id ) { |
| 365 | $order = wc_get_order( $order_id ); |
| 366 | if ( ! $order || ! $order instanceof \WC_Order ) { |
| 367 | return null; |
| 368 | } |
| 369 | return array_merge( |
| 370 | $order->get_data() |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get product details context. |
| 376 | * |
| 377 | * @param array $items items. |
| 378 | * |
| 379 | * @return array |
| 380 | */ |
| 381 | public static function get_order_items_context_array( $items ) { |
| 382 | $new_items = []; |
| 383 | foreach ( $items as $item ) { |
| 384 | $item_data = []; |
| 385 | $item_data = $item->get_data(); |
| 386 | $item_data['meta_data'] = $item->get_formatted_meta_data( '_', true ); |
| 387 | $new_items[] = $item_data; |
| 388 | } |
| 389 | return $new_items; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Get product details context. |
| 394 | * |
| 395 | * @param array $items items. |
| 396 | * |
| 397 | * @return array |
| 398 | */ |
| 399 | public static function get_order_items_context( $items ) { |
| 400 | $new_items = []; |
| 401 | foreach ( $items as $item ) { |
| 402 | $item_data = []; |
| 403 | $item_data = $item->get_data(); |
| 404 | unset( $item_data['meta_data'] ); |
| 405 | $item_data['meta_data'] = $item->get_formatted_meta_data( '_', true ); |
| 406 | $item_data['product_sku'] = get_post_meta( $item_data['product_id'], '_sku', true ); |
| 407 | $item_data['variation_product_sku'] = get_post_meta( $item_data['variation_id'], '_sku', true ); |
| 408 | $new_items[] = $item_data; |
| 409 | } |
| 410 | |
| 411 | $product = []; |
| 412 | |
| 413 | foreach ( $new_items[0] as $item_key => $item_value ) { |
| 414 | if ( 'meta_data' === $item_key ) { |
| 415 | $product[ $item_key ] = self::loop_over_meta_item( $item_value ); |
| 416 | } else { |
| 417 | $product[ $item_key ] = implode( |
| 418 | ', ', |
| 419 | array_map( |
| 420 | function ( $entry ) use ( $item_key ) { |
| 421 | $ent = $entry[ $item_key ]; |
| 422 | |
| 423 | $ent = self::loop_over_item( $ent ); |
| 424 | return $ent; |
| 425 | }, |
| 426 | $new_items |
| 427 | ) |
| 428 | ); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return $product; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Loop items |
| 437 | * |
| 438 | * @param array $items items. |
| 439 | * |
| 440 | * @return array |
| 441 | */ |
| 442 | public static function loop_over_meta_item( $items ) { |
| 443 | $meta = []; |
| 444 | foreach ( $items as $subitem ) { |
| 445 | foreach ( $subitem as $key => $sub ) { |
| 446 | $meta[ $key ] = implode( |
| 447 | ', ', |
| 448 | array_map( |
| 449 | function ( $entry ) use ( $key ) { |
| 450 | $ent = $entry->$key; |
| 451 | $ent = self::loop_over_item( $ent ); |
| 452 | return $ent; |
| 453 | }, |
| 454 | $items |
| 455 | ) |
| 456 | ); |
| 457 | } |
| 458 | } |
| 459 | return $meta; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Get product details context. |
| 464 | * |
| 465 | * @param array $item item. |
| 466 | * |
| 467 | * @return array|void |
| 468 | */ |
| 469 | public static function loop_over_item( $item ) { |
| 470 | if ( is_array( $item ) || is_object( $item ) ) { |
| 471 | foreach ( $item as $subitem ) { |
| 472 | self::loop_over_item( $subitem ); |
| 473 | } |
| 474 | } else { |
| 475 | return $item; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Get product details context. |
| 481 | * |
| 482 | * @param array $order order. |
| 483 | * |
| 484 | * @return array |
| 485 | */ |
| 486 | public static function get_order_items_context_products( $order ) { |
| 487 | $order_items = []; |
| 488 | foreach ( $order as $order_id ) { |
| 489 | $order_items[] = self::get_product_context( $order_id ); |
| 490 | } |
| 491 | |
| 492 | $product = []; |
| 493 | foreach ( $order_items[0] as $item_key => $item_value ) { |
| 494 | $product[ $item_key ] = implode( |
| 495 | ', ', |
| 496 | array_map( |
| 497 | function ( $entry ) use ( $item_key ) { |
| 498 | $ent = $entry[ $item_key ]; |
| 499 | |
| 500 | return $ent; |
| 501 | }, |
| 502 | $order_items |
| 503 | ) |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | return $product; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | /** |
| 512 | * Is Plugin depended on plugin is installed or not. |
| 513 | * |
| 514 | * @return bool |
| 515 | */ |
| 516 | public function is_plugin_installed() { |
| 517 | return class_exists( 'WooCommerce' ); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | IntegrationsController::register( WooCommerce::class ); |
| 522 |