| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Integrations; |
| 4 |
|
| 5 |
/** |
| 6 |
* WooCommerce Integration Class |
| 7 |
*/ |
| 8 |
class WooCommerce { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'woocommerce_order_status_changed', [ $this, 'order_status_changed' ], 10, 4 ); |
| 15 |
add_action( 'texty_register_notifications', [ $this, 'register_notifications' ] ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Register WooCommerce notification types. |
| 20 |
* |
| 21 |
* @param \Texty\Notifications $notifications The notifications manager |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function register_notifications( $notifications ) { |
| 26 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$namespace = 'Texty\Notifications\WC\\'; |
| 31 |
|
| 32 |
// WC Admin |
| 33 |
$notifications->register( 'order_admin_processing', $namespace . 'ProcessingAdmin' ); |
| 34 |
$notifications->register( 'order_admin_complete', $namespace . 'CompleteAdmin' ); |
| 35 |
$notifications->register( 'order_admin_cancelled', $namespace . 'CancelledAdmin' ); |
| 36 |
$notifications->register( 'order_admin_failed', $namespace . 'FailedAdmin' ); |
| 37 |
$notifications->register( 'order_admin_refunded', $namespace . 'RefundedAdmin' ); |
| 38 |
|
| 39 |
// WC Customers |
| 40 |
$notifications->register( 'order_customer_hold', $namespace . 'HoldCustomer' ); |
| 41 |
$notifications->register( 'order_customer_processing', $namespace . 'ProcessingCustomer' ); |
| 42 |
$notifications->register( 'order_customer_complete', $namespace . 'CompleteCustomer' ); |
| 43 |
$notifications->register( 'order_customer_cancelled', $namespace . 'CancelledCustomer' ); |
| 44 |
$notifications->register( 'order_customer_failed', $namespace . 'FailedCustomer' ); |
| 45 |
$notifications->register( 'order_customer_refunded', $namespace . 'RefundedCustomer' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Send a message when an order status changes |
| 50 |
* |
| 51 |
* @param int $order_id |
| 52 |
* @param string $old_status |
| 53 |
* @param string $order_status |
| 54 |
* @param WC_Order $order |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public function order_status_changed( $order_id, $old_status, $order_status, $order ) { |
| 59 |
// don't process sub-orders |
| 60 |
if ( $order->get_parent_id() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
switch ( $order_status ) { |
| 65 |
case 'on-hold': |
| 66 |
$this->send( 'order_customer_hold', $order ); |
| 67 |
break; |
| 68 |
|
| 69 |
case 'processing': |
| 70 |
$this->send( 'order_admin_processing', $order ); |
| 71 |
$this->send( 'order_customer_processing', $order ); |
| 72 |
break; |
| 73 |
|
| 74 |
case 'completed': |
| 75 |
$this->send( 'order_admin_complete', $order ); |
| 76 |
$this->send( 'order_customer_complete', $order ); |
| 77 |
break; |
| 78 |
|
| 79 |
case 'cancelled': |
| 80 |
$this->send( 'order_admin_cancelled', $order ); |
| 81 |
$this->send( 'order_customer_cancelled', $order ); |
| 82 |
break; |
| 83 |
|
| 84 |
case 'failed': |
| 85 |
$this->send( 'order_admin_failed', $order ); |
| 86 |
$this->send( 'order_customer_failed', $order ); |
| 87 |
break; |
| 88 |
|
| 89 |
case 'refunded': |
| 90 |
$this->send( 'order_admin_refunded', $order ); |
| 91 |
$this->send( 'order_customer_refunded', $order ); |
| 92 |
break; |
| 93 |
|
| 94 |
default: |
| 95 |
// code... |
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Send notification by event |
| 102 |
* |
| 103 |
* @param string $event |
| 104 |
* @param WC_Order $order |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
private function send( $event, $order ) { |
| 109 |
$class = texty()->notifications()->get( $event ); |
| 110 |
$notification = new $class(); |
| 111 |
|
| 112 |
$notification->set_order( $order ); |
| 113 |
$notification->send(); |
| 114 |
} |
| 115 |
} |
| 116 |
|