| 1 |
<?php |
| 2 |
|
| 3 |
namespace Texty\Integrations; |
| 4 |
|
| 5 |
/** |
| 6 |
* Dokan Integration Class |
| 7 |
*/ |
| 8 |
class Dokan { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'woocommerce_order_status_changed', [ $this, 'order_status_changed' ], 99, 4 ); |
| 15 |
add_action( 'dokan_new_seller_created', [ $this, 'update_vendor_phone' ], 35, 2 ); |
| 16 |
add_action( 'dokan_store_profile_saved', [ $this, 'update_vendor_phone' ], 35, 2 ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Send a message when an order status changes |
| 21 |
* |
| 22 |
* @param int $order_id |
| 23 |
* @param string $old_status |
| 24 |
* @param string $order_status |
| 25 |
* @param WC_Order $order |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function order_status_changed( $order_id, $old_status, $order_status, $order ) { |
| 30 |
$vendor_id = $this->vendor_id( $order ); |
| 31 |
|
| 32 |
if ( ! $vendor_id ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
switch ( $order_status ) { |
| 37 |
case 'processing': |
| 38 |
$this->send( 'order_dokan_processing', $order, $vendor_id ); |
| 39 |
break; |
| 40 |
|
| 41 |
case 'completed': |
| 42 |
$this->send( 'order_dokan_complete', $order, $vendor_id ); |
| 43 |
break; |
| 44 |
|
| 45 |
default: |
| 46 |
// code... |
| 47 |
break; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Save texty phone number when a seller add/update their phone number |
| 53 |
* |
| 54 |
* @param int $user_id |
| 55 |
* @param array $settings |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function update_vendor_phone( $user_id, $settings ) { |
| 60 |
if ( ! isset( $settings['phone'] ) ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
update_user_meta( $user_id, 'texty_phone', $settings['phone'] ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get the vendor ID from order |
| 69 |
* |
| 70 |
* @param WC_Order $order |
| 71 |
* |
| 72 |
* @return int |
| 73 |
*/ |
| 74 |
protected function vendor_id( $order ) { |
| 75 |
return (int) $order->get_meta( '_dokan_vendor_id' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Send notification by event |
| 80 |
* |
| 81 |
* @param string $event |
| 82 |
* @param WC_Order $order |
| 83 |
* @param int $vendor_id |
| 84 |
* |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
private function send( $event, $order, $vendor_id ) { |
| 88 |
$class = texty()->notifications()->get( $event ); |
| 89 |
$notification = new $class(); |
| 90 |
|
| 91 |
$notification->set_order( $order ); |
| 92 |
$notification->set_vendor( $vendor_id ); |
| 93 |
$notification->send(); |
| 94 |
} |
| 95 |
} |
| 96 |
|