Skip to content

Commit

Permalink
add profile creation on checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
ricvillagrana committed Jun 12, 2024
1 parent b776ae6 commit 74bd3e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion hellotext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Plugin Name: Hellotext
* Plugin URI: https://hellotext.com
* Description: Integrates Hellotext tracking to WooCommerce.
* Version: 1.0.6
* Version: 1.0.7
* Author: Hellotext Team
* Author URI: https://github.com/hellotext
* License: GPL v2
Expand Down
1 change: 1 addition & 0 deletions src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static function request ($method = 'GET', $path = '/', $data = []) {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($curl);
var_dump($result);

return array(
'request' => array(
Expand Down
5 changes: 4 additions & 1 deletion src/Events/OrderPlaced.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
add_action( 'woocommerce_after_order_details', 'hellotext_order_placed' );

function hellotext_order_placed ( $order ) {
do_action('hellotext_create_profile', $order->get_user_id());
$userId = $order->get_user_id();
$userId = $userId > 0 ? $userId : $order->data['billing'];

do_action('hellotext_create_profile', $userId ?? $order->data['billing']);

$event = new Event();
$parsedOrder = ( new OrderAdapter($order) )->get();
Expand Down
26 changes: 20 additions & 6 deletions src/Services/CreateProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ class CreateProfile {
public $hellotext_profile_id;

public $client;
public $billing;

public $user_id;
public $session;

public function __construct($user_id) {
public function __construct($user_id, $billing = []) {
$this->user_id = $user_id;
$this->session = isset($_COOKIE['hello_session']) ? sanitize_text_field($_COOKIE['hello_session']) : null;
$this->client = Client::class;
$this->billing = $billing;
}

public function process () {
if (isset($this->billing) && !empty($this->billing)) {
$this->create_hellotext_profile();
return;
}

if (! $this->user_id) {
return;
}
Expand Down Expand Up @@ -58,13 +65,16 @@ private function create_hellotext_profile () {

$response = $this->client::post('/profiles', array_filter(array(
'session' => $this->session,
'reference' => $this->user->ID,
'first_name' => $this->user->nickname,
'email' => $this->user->user_email,
'phone' => $phone,
'reference' => isset($this->user) ? $this->user->ID : null,
'first_name' => $this->user->nickname ?? $this->billing['first_name'],
'last_name' => $this->user->last_name ?? $this->billing['last_name'],
'email' => $this->user->user_email ?? $this->billing['email'],
'phone' => $phone ?? $this->billing['phone'],
'lists' => array('WooCommerce'),
)));

var_dump(json_encode($response));

add_user_meta( $this->user_id, 'hellotext_profile_id', $response['body']['id'], true );
}

Expand All @@ -78,7 +88,11 @@ private function attach_profile_to_session () {
}
}

add_action('hellotext_create_profile', function ($user_id = null) {
add_action('hellotext_create_profile', function ($payload = null) {
if (is_array($payload)) {
( new CreateProfile(null, $payload) )->process();
}

if (!is_user_logged_in() && !isset($user_id)) {
return;
}
Expand Down

0 comments on commit 74bd3e9

Please sign in to comment.