/** * Transfer settings from old option fields to new system based on WC Settings API * * @since 4.4 */ private static function maybe_update_settings() { // Delete deprecated "wootax_shipping_taxable" option if it still exists if (get_option('wootax_shipping_taxable')) { delete_option('wootax_shipping_taxable'); } // Transfer settings so they can be used with WooCommerce settings API $options = array('tc_id', 'tc_key', 'usps_id', 'show_exempt', 'exemption_text', 'company_name', 'show_zero_tax', 'tax_based_on', 'addresses', 'default_address'); foreach ($options as $option) { if (get_option('wootax_' . $option)) { WC_WooTax::set_option($option, get_option('wootax_' . $option)); delete_option('wootax_' . $option); } } // wootax_license_key option was deprecated in 4.5; remove it if (get_option('wootax_license_key')) { delete_option('wootax_license_key'); } }
function wt_do_template_substitutions($content) { return str_replace(array('{PLUGIN_PATH}', '{COMPANY_NAME}'), array(WT_PLUGIN_DIR_URL, WC_WooTax::get_option('company_name')), $content); }
/** * Maybe capture order immediately after checkout * * @since 4.5 */ public static function maybe_capture_order($order_id) { if (WC_WooTax::get_option('capture_immediately') == 'yes') { $res = self::capture_order($order_id, true); if ($res !== true && self::$logger) { self::$logger->add('wootax', 'Failed to capture order ' . $order_id . ' after checkout.'); } } }
/** * Updates WooTax tax item to reflect changes in cart/shipping tax totals * * @since 4.2 * @param (double) $cart_tax the total cart tax added by WooTax * @param (double) $shipping_tax the total shipping tax added by WooTax */ private function update_tax_item($cart_tax, $shipping_tax) { global $wpdb; // Add a new tax item if necessary $tax_item_id = WT_Orders::get_meta($this->order_id, 'tax_item_id'); if ($tax_item_id == 0 || $tax_item_id == NULL) { $wpdb->insert("{$wpdb->prefix}woocommerce_order_items", array('order_item_type' => 'tax', 'order_item_name' => apply_filters('wootax_rate_code', 'WOOTAX-RATE-DO-NOT-REMOVE'), 'order_id' => $this->order_id)); // Store new tax item ID $tax_item_id = $wpdb->insert_id; WT_Orders::update_meta($this->order_id, 'tax_item_id', $tax_item_id); } // Update tax item meta wc_update_order_item_meta($tax_item_id, 'rate_id', WT_RATE_ID); wc_update_order_item_meta($tax_item_id, 'label', WC_WooTax::get_rate_label(WT_RATE_ID)); wc_update_order_item_meta($tax_item_id, 'name', WC_WooTax::get_rate_label(WT_RATE_ID)); wc_update_order_item_meta($tax_item_id, 'compound', true); wc_update_order_item_meta($tax_item_id, 'tax_amount', $cart_tax); wc_update_order_item_meta($tax_item_id, 'shipping_tax_amount', $shipping_tax); if (WT_SUBS_ACTIVE) { wc_update_order_item_meta($tax_item_id, 'cart_tax', $cart_tax); wc_update_order_item_meta($tax_item_id, 'shipping_tax', $shipping_tax); } }
/** * Returns the email to which WooTax notifications should be sent * If the notification_email is not set explicitly, return first admin email * * @since 4.4 * @return (string) email address */ function wootax_get_notification_email() { $email = WC_WooTax::get_option('notification_email'); if ($email) { return $email; } $all_admins = get_users(array('role' => 'administrator', 'number' => 1)); return $all_admins[0]->user_email; }
/** * Return instance of WC_WooTax_TaxCloud class, optionally initialized with Login ID/Key * If no login ID/key are provided, use stored settings * * @since 4.4 * @param (string) $login_id optional API Login ID * @param (string) $login_key optional API Login Key */ function TaxCloud($login_id = false, $login_key = false) { if (!$login_id) { $login_id = WC_WooTax::get_option('tc_id'); } if (!$login_key) { $login_key = WC_WooTax::get_option('tc_key'); } $instance = WC_WooTax_TaxCloud::instance(); $instance->set_id($login_id); $instance->set_key($login_key); return $instance; }
/** * Process address fields so they can be stored correctly */ public function sanitize_settings($settings) { // Prevent this from running except on the main settings page if (isset($_POST['wootax_address1'])) { // Fetch all addresses and dump into array $new_addresses = array(); $address_count = count($_POST['wootax_address1']); for ($i = 0; $i < $address_count; $i++) { $address = array('address_1' => $_POST['wootax_address1'][$i], 'address_2' => $_POST['wootax_address2'][$i], 'country' => 'United States', 'state' => $_POST['wootax_state'][$i], 'city' => $_POST['wootax_city'][$i], 'zip5' => $_POST['wootax_zip5'][$i], 'zip4' => $_POST['wootax_zip4'][$i]); $new_addresses[] = $address; } $taxcloud_id = trim($_POST['woocommerce_wootax_tc_id']); $taxcloud_key = trim($_POST['woocommerce_wootax_tc_key']); $usps_id = trim($_POST['woocommerce_wootax_usps_id']); // Validate addresses using USPS Web Tools API if possible if ($taxcloud_id && $taxcloud_key && $usps_id) { $taxcloud = TaxCloud($taxcloud_id, $taxcloud_key); foreach ($new_addresses as $key => $address) { $req = array('uspsUserID' => $usps_id, 'Address1' => strtolower($address['address_1']), 'Address2' => strtolower($address['address_2']), 'Country' => 'US', 'City' => $address['city'], 'State' => $address['state'], 'Zip5' => $address['zip5'], 'Zip4' => $address['zip4']); // Attempt to verify address $response = $taxcloud->send_request('VerifyAddress', $req); if ($response !== false) { $new_address = array(); $properties = array('Address1' => 'address_1', 'Address2' => 'address_2', 'Country' => 'country', 'City' => 'city', 'State' => 'state', 'Zip5' => 'zip5', 'Zip4' => 'zip4'); foreach ($properties as $property => $k) { if (isset($response->{$property})) { $new_address[$k] = $response->{$property}; } } // Reset country field if (!isset($new_address['country'])) { $new_address['country'] = $req['Country']; } $new_addresses[$key] = $new_address; } } } // Set addresses option $settings['addresses'] = $new_addresses; // Next, update the default address $settings['default_address'] = empty($_POST['wootax_default_address']) ? 0 : $_POST['wootax_default_address']; // Set settings_changed flag to "true" so WooTax reloads settings array WC_WooTax::$settings_changed = true; } // Enforce default settings for log_requests/capture_immediately/exempt_roles if (!isset($_POST['woocommerce_wootax_log_requests'])) { $settings['log_requests'] = 'yes'; } if (!isset($_POST['woocommerce_wootax_capture_immediately'])) { $settings['capture_immediately'] = 'no'; } if (!isset($_POST['woocommerce_wootax_exempt_roles'])) { $settings['exempt_roles'] = array('exempt-customer'); } return $settings; }
/** * Set the value of a WooTax option * * @since 4.2 * @param (mixed) $key the key of the option to be updated * @param (mixed) $value the new value of the option */ public static function set_option($key, $value) { if (count(self::$settings) == 0) { self::$settings = get_option(self::$settings_key); } self::$settings[$key] = $value; update_option(self::$settings_key, self::$settings); }
/** * Update order shipping/cart tax total * * @since 4.4 * @param (string) $type "shipping" to update shipping tax total; "cart" to update cart tax total * @param (float) $new_tax new value for tax */ private function update_tax_total($type, $new_tax) { if ($type == 'shipping') { $tax_key = 'shipping_taxes'; $total_key = 'shipping_tax_total'; } else { $tax_key = 'taxes'; $total_key = 'tax_total'; } $this->cart->{$tax_key}[WT_RATE_ID] = $new_tax; if (!WT_SUBS_ACTIVE || !WC_Subscriptions_Cart::cart_contains_subscription()) { // Removing zero tax row causes display issues for subscription orders if (WC_WooTax::get_option('show_zero_tax') != 'true' && $new_tax == 0) { unset($this->cart->{$tax_key}[WT_RATE_ID]); } } // Use get_tax_total to set new tax total so we don't override other rates $this->cart->{$total_key} = version_compare(WOOCOMMERCE_VERSION, '2.2', '>=') ? WC_Tax::get_tax_total($this->cart->{$tax_key}) : $this->cart->tax->get_tax_total($this->cart->{$tax_key}); $this->{$total_key} = $new_tax; }