Skip to content

Commit

Permalink
Updates for v2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnullarc committed Nov 26, 2020
1 parent 30eaf27 commit 98d61f2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
63 changes: 35 additions & 28 deletions includes/class-rc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

class RC_Order {
private $order;
public $base_url = 'https://my.referralcandy.com/api/v1';
public $wc_pre_30 = false;
public $api_id;
Expand All @@ -22,41 +23,44 @@ class RC_Order {
public $order_timestamp;
public $browser_ip;
public $user_agent;
public $accepts_marketing;
public $referrer_id;

public function __construct($wc_order_id, WC_Referralcandy_Integration $integration) {
$this->wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<');
$wc_order = new WC_Order($wc_order_id);
$this->order = new WC_Order($wc_order_id);

if ($this->wc_pre_30) {
$this->order_timestamp = time();
if (get_option('timezone_string') != null) {
$timezone_string = get_option('timezone_string');
$this->order_timestamp = DateTime::createFromFormat('Y-m-d H:i:s', $wc_order->order_date, new DateTimeZone($timezone_string))->getTimestamp();
$this->order_timestamp = DateTime::createFromFormat('Y-m-d H:i:s', $this->order->order_date, new DateTimeZone($timezone_string))->getTimestamp();
}

$this->first_name = $wc_order->billing_first_name;
$this->last_name = $wc_order->billing_last_name;
$this->email = $wc_order->billing_email;
$this->total = $wc_order->get_total();
$this->currency = $wc_order->get_order_currency();
$this->order_number = $wc_order_id;
$this->browser_ip = $wc_order->customer_ip_address;
$this->user_agent = $wc_order->customer_user_agent;
$this->referrer_id = get_post_meta($wc_order_id, 'rc_referrer_id', true);
$this->first_name = $this->order->billing_first_name;
$this->last_name = $this->order->billing_last_name;
$this->email = $this->order->billing_email;
$this->total = $this->order->get_total();
$this->currency = $this->order->get_order_currency();
$this->order_number = $wc_order_id;
$this->browser_ip = $this->order->customer_ip_address;
$this->user_agent = $this->order->customer_user_agent;
$this->accepts_marketing = get_post_meta($wc_order_id, 'rc_accepts_marketing', true) ? 'true' : 'false';
$this->referrer_id = get_post_meta($wc_order_id, 'rc_referrer_id', true);
} else {
$order_data = $wc_order->get_data();

$this->first_name = $order_data['billing']['first_name'];
$this->last_name = $order_data['billing']['last_name'];
$this->email = $order_data['billing']['email'];
$this->total = $order_data['total'];
$this->currency = $order_data['currency'];
$this->order_number = $wc_order_id;
$this->order_timestamp = $order_data['date_created']->getTimestamp();
$this->browser_ip = $order_data['customer_ip_address'];
$this->user_agent = $order_data['customer_user_agent'];
$this->referrer_id = $wc_order->get_meta('rc_referrer_id', true, 'view');
$order_data = $this->order->get_data();

$this->first_name = $order_data['billing']['first_name'];
$this->last_name = $order_data['billing']['last_name'];
$this->email = $order_data['billing']['email'];
$this->total = $order_data['total'];
$this->currency = $order_data['currency'];
$this->order_number = $wc_order_id;
$this->order_timestamp = $order_data['date_created']->getTimestamp();
$this->browser_ip = $order_data['customer_ip_address'];
$this->user_agent = $order_data['customer_user_agent'];
$this->accepts_marketing = $this->order->get_meta('rc_accepts_marketing', true, 'view') ? 'true' : 'false';
$this->referrer_id = $this->order->get_meta('rc_referrer_id', true, 'view');
}

$this->api_id = $integration->api_id;
Expand All @@ -66,6 +70,7 @@ public function __construct($wc_order_id, WC_Referralcandy_Integration $integrat
private function generate_post_fields($specific_keys = [], $additional_keys = []) {
$post_fields = [
'accessID' => $this->api_id,
'accepts_marketing' => $this->accepts_marketing,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'email' => $this->email,
Expand Down Expand Up @@ -106,6 +111,7 @@ private function generate_post_fields($specific_keys = [], $additional_keys = []
// sort keys
ksort($post_fields);

// return array_merge(['accessID' => $this->api_id], $post_fields);
return $post_fields;
}

Expand Down Expand Up @@ -135,12 +141,13 @@ public function submit_purchase() {
$endpoint = join('/', [$this->base_url, 'purchase.json']);

if (!empty($this->secret_key) && !empty($this->api_id)) {
$params = $this->generate_request_body($this->generate_post_fields());
$response = wp_safe_remote_post($endpoint, $params);
$params = $this->generate_request_body($this->generate_post_fields());
$response = wp_safe_remote_post($endpoint, $params);
$response_body = json_decode($response['body']);

# only used to cross reference requests
// error_log('ReferralCandy API#purchase params: ' . print_r($params['body'], true));
// error_log('ReferralCandy API#purchase response: ' . print_r(json_decode($response['body']), true));
if ($response_body->message == 'Success' && !empty($response_body->referralcorner_url)) {
$this->order->add_order_note('Order sent to ReferralCandy');
}
}
}
}
36 changes: 32 additions & 4 deletions includes/class-wc-referralcandy-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function __construct() {
add_action('save_post', [$this, 'add_referrer_id']);
add_action('woocommerce_thankyou', [$this, 'render_post_purchase_popup']);
add_action('woocommerce_order_status_' . $this->status_to, [$this, 'rc_submit_purchase'], 10, 1);
add_action('woocommerce_review_order_before_submit', [$this, 'render_accepts_marketing_field']);
add_action('woocommerce_checkout_update_order_meta', [$this, 'capture_accepts_marketing_value']);

// Filters.
add_filter('woocommerce_settings_api_sanitized_fields_' . $this->id, [$this, 'sanitize_settings']);
Expand Down Expand Up @@ -88,6 +90,14 @@ public function init_form_fields() {
'desc_tip' => true,
'default' => 'checkout'
],
'accepts_marketing_label' => [
'title' => __('Accepts marketing checkbox label', 'woocommerce-referralcandy'),
'type' => 'text',
'css' => 'width: 50%',
'desc' => __('Render the tracking code on the selected pages', 'woocommerce-referralcandy'),
'desc_tip' => true,
'default' => 'I would like to receive referral marketing and promotional emails'
],
'popup' => [
'title' => __('Post-purchase Popup', 'woocommerce-referralcandy'),
'label' => __('Enable post-purchase Popup', 'woocommerce-referralcandy'),
Expand All @@ -97,9 +107,11 @@ public function init_form_fields() {
],
'popup_quickfix' => [
'title' => __('Post-purchase Popup Quickfix', 'woocommerce-referralcandy'),
'label' => __('Popup is breaking the checkout page?'.'
Try enabling this option to apply the quickfix!',
'woocommerce-referralcandy'),
'label' => __(
'Popup is breaking the checkout page?
Try enabling this option to apply the quickfix!',
'woocommerce-referralcandy'
),
'type' => 'checkbox',
'desc_tip' => false,
'default' => ''
Expand All @@ -115,6 +127,22 @@ private function is_option_enabled($option_name) {
return $this->get_option($option_name) == 'yes'? true : false;
}

public function render_accepts_marketing_field( $checkout ) {
echo "<div style='width: 100%;'>";
woocommerce_form_field( 'rc_accepts_marketing', array(
'type' => 'checkbox',
'label' => $this->get_option('accepts_marketing_label'),
'required' => false,
), false);
echo "</div>";
}

public function capture_accepts_marketing_value( $order_id ) {
if ( ! empty( $_POST['rc_accepts_marketing'] ) ) {
update_post_meta( $order_id, 'rc_accepts_marketing', sanitize_text_field( $_POST['rc_accepts_marketing'] ) );
}
}

public function check_plugin_requirements() {
$message = "<strong>ReferralCandy</strong>: Please make sure the following settings are configured for your integration to work properly:";
$integration_incomplete = false;
Expand Down Expand Up @@ -184,7 +212,7 @@ public function render_post_purchase_popup($order_id) {
data-lname='$rc_order->last_name'
data-email='$rc_order->email'
data-accepts-marketing='false'
></div>";
></div><style>iframe[src*='portal.referralcandy.com']{ height: 100% !important; }</style>";

$popup_script = '<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.defer=true;js.src="//portal.referralcandy.com/assets/widgets/refcandy-lollipop.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","refcandy-lollipop-js");</script>';

Expand Down
6 changes: 6 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ We maintain a list of FAQs on our [help page](http://answers.referralcandy.com/)

== Changelog ==

= 2.3.0 =
* Added marketing acceptance checkbox on checkout
* Added option to customize marketing acceptance label
* Updated order class to include `accepts_marketing` data taken from marketing acceptance upon checkout
* Added feature that adds a note on the order if a purchase was successfully submitted to ReferralCandy

= 2.2.4 =
* Fixed issue where there the tracking code is rendered before the html

Expand Down
2 changes: 1 addition & 1 deletion woocommerce-referralcandy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Author: ReferralCandy
* Author URI: http://www.referralcandy.com
* Text Domain: woocommerce-referralcandy
* Version: 2.2.4
* Version: 2.3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 98d61f2

Please sign in to comment.