diff --git a/includes/class-rc-order.php b/includes/class-rc-order.php index ff9a2af..78c244c 100644 --- a/includes/class-rc-order.php +++ b/includes/class-rc-order.php @@ -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; @@ -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; @@ -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, @@ -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; } @@ -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'); + } } } } diff --git a/includes/class-wc-referralcandy-integration.php b/includes/class-wc-referralcandy-integration.php index 84c2cdd..df42e81 100644 --- a/includes/class-wc-referralcandy-integration.php +++ b/includes/class-wc-referralcandy-integration.php @@ -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']); @@ -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'), @@ -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' => '' @@ -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 "
"; + woocommerce_form_field( 'rc_accepts_marketing', array( + 'type' => 'checkbox', + 'label' => $this->get_option('accepts_marketing_label'), + 'required' => false, + ), false); + echo "
"; + } + + 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 = "ReferralCandy: Please make sure the following settings are configured for your integration to work properly:"; $integration_incomplete = false; @@ -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' - >"; + >"; $popup_script = ''; diff --git a/readme.txt b/readme.txt index f29c104..8c246d3 100644 --- a/readme.txt +++ b/readme.txt @@ -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 diff --git a/woocommerce-referralcandy.php b/woocommerce-referralcandy.php index a71f8f3..53f8330 100644 --- a/woocommerce-referralcandy.php +++ b/woocommerce-referralcandy.php @@ -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