/** * Get a transaction array from the current cart * * @param String $country * @param array $items * @param null|String $buyer_name * @param null|String $buyer_email * @param null|String $custom_transaction_id * @param array $transaction_extra * * @return array */ public function build_transaction($country, $items, $buyer_name = null, $buyer_email = null, $custom_transaction_id = null, $transaction_extra = array()) { // Country manager $country_manager = new WC_TA_Country_Manager(); // The transaction $transaction = array('currency_code' => get_woocommerce_currency(), 'billing_country_code' => $country, 'buyer_ip' => $country_manager->get_user_ip_address()); // Add buyer name if (null !== $buyer_name) { $transaction['buyer_name'] = $buyer_name; } // Add buyer email if (null !== $buyer_email) { $transaction['buyer_email'] = $buyer_email; } // Add custom transaction ID if (null !== $custom_transaction_id) { $transaction['custom_id'] = '' . $custom_transaction_id; } // Get the transaction lines $transaction['transaction_lines'] = $this->build_transaction_lines($items, $country); // Merge transaction extra values if (is_array($transaction_extra) && count($transaction_extra) > 0) { $transaction = array_merge_recursive($transaction, $transaction_extra); } return $transaction; }
/** * Retrieve the customer location based on their IP and set as default location. * * @param string * * @return String */ public function set_default_customer_location($default) { // Only request country when not in admin and not doing ajax requests if (is_admin() || defined('DOING_AJAX') && DOING_AJAX) { return $default; } // Country Manager $country_manager = new WC_TA_Country_Manager(); // Get the IP address $ip_address = $country_manager->get_user_ip_address(); // WC 2.3 geoip compatible transient name $country_code = get_transient('geoip_' . $ip_address); if (false === $country_code) { // Fallback for if no results are found $country_code = $default; $request_ip_location = new WC_TA_Request_IP_Location($ip_address); if ($request_ip_location->do_request()) { $response_body = $request_ip_location->get_response_body(); if (isset($response_body->country_code)) { $country_code = $response_body->country_code; set_transient('geoip_' . $ip_address, $country_code, WEEK_IN_SECONDS); } } } return $country_code; }