AdditionalPayments.php
1 year ago
Appearance.php
2 years ago
CustomizeStore.php
5 months ago
ExperimentalShippingRecommendation.php
5 months ago
ExtendStore.php
1 year ago
GetMobileApp.php
3 years ago
LaunchYourStore.php
2 years ago
Marketing.php
7 months ago
Payments.php
10 months ago
Products.php
1 month ago
ReviewShippingOptions.php
3 years ago
Shipping.php
3 months ago
StoreCreation.php
4 years ago
StoreDetails.php
3 years ago
Tax.php
1 year ago
TourInAppMarketplace.php
2 years ago
WooCommercePayments.php
5 months ago
Tax.php
269 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks; |
| 4 | |
| 5 | use Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore as TaxDataStore; |
| 6 | use Automattic\WooCommerce\Admin\Features\Features; |
| 7 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task; |
| 8 | use Automattic\WooCommerce\Admin\PluginsHelper; |
| 9 | use Automattic\WooCommerce\Internal\Admin\WCAdminAssets; |
| 10 | |
| 11 | /** |
| 12 | * Tax Task |
| 13 | */ |
| 14 | class Tax extends Task { |
| 15 | |
| 16 | private const TAX_RATE_EXISTS_CACHE_KEY = 'woocommerce_onboarding_task_tax_rates_exist'; |
| 17 | |
| 18 | /** |
| 19 | * Used to cache is_complete() method result. |
| 20 | * |
| 21 | * @var null |
| 22 | */ |
| 23 | private $is_complete_result = null; |
| 24 | |
| 25 | /** |
| 26 | * Constructor |
| 27 | * |
| 28 | * @param TaskList $task_list Parent task list. |
| 29 | */ |
| 30 | public function __construct( $task_list ) { |
| 31 | parent::__construct( $task_list ); |
| 32 | add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_return_notice_script' ) ); |
| 33 | add_action( 'woocommerce_tax_rate_added', array( $this, 'on_tax_rate_added' ) ); |
| 34 | add_action( 'woocommerce_tax_rate_deleted', array( $this, 'on_tax_rate_deleted' ) ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Adds a return to task list notice when completing the task. |
| 39 | */ |
| 40 | public function possibly_add_return_notice_script() { |
| 41 | $page = isset( $_GET['page'] ) ? $_GET['page'] : ''; // phpcs:ignore csrf ok, sanitization ok. |
| 42 | $tab = isset( $_GET['tab'] ) ? $_GET['tab'] : ''; // phpcs:ignore csrf ok, sanitization ok. |
| 43 | |
| 44 | if ( $page !== 'wc-settings' || $tab !== 'tax' ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if ( ! $this->is_active() || $this->is_complete() ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | WCAdminAssets::register_script( 'wp-admin-scripts', 'onboarding-tax-notice', true ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * ID. |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | public function get_id() { |
| 61 | return 'tax'; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Title. |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | public function get_title() { |
| 70 | return __( 'Collect sales tax', 'woocommerce' ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Content. |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function get_content() { |
| 79 | return self::can_use_automated_taxes() |
| 80 | ? __( |
| 81 | 'Good news! WooCommerce Tax can automate your sales tax calculations for you.', |
| 82 | 'woocommerce' |
| 83 | ) |
| 84 | : __( |
| 85 | 'Set your store location and configure tax rate settings.', |
| 86 | 'woocommerce' |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Time. |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | public function get_time() { |
| 96 | return __( '1 minute', 'woocommerce' ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Action label. |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function get_action_label() { |
| 105 | return self::can_use_automated_taxes() |
| 106 | ? __( 'Yes please', 'woocommerce' ) |
| 107 | : __( "Let's go", 'woocommerce' ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Task completion. |
| 112 | * |
| 113 | * @return bool |
| 114 | */ |
| 115 | public function is_complete() { |
| 116 | if ( $this->is_complete_result === null ) { |
| 117 | $wc_connect_taxes_enabled = get_option( 'wc_connect_taxes_enabled' ); |
| 118 | $is_wc_connect_taxes_enabled = ( $wc_connect_taxes_enabled === 'yes' ) || ( $wc_connect_taxes_enabled === true ); // seems that in some places boolean is used, and other places 'yes' | 'no' is used |
| 119 | |
| 120 | // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- We will replace this with a formal system by WC 9.6 so lets not advertise it yet. |
| 121 | $third_party_complete = apply_filters( 'woocommerce_admin_third_party_tax_setup_complete', false ); |
| 122 | |
| 123 | /** |
| 124 | * Ideally we would check against `wc_tax_enabled()` instead of `false !== get_option( 'woocommerce_no_sales_tax' )`, |
| 125 | * however, tax is disabled by default making this task complete by default if we use it. If we change taxes |
| 126 | * to be enabled by default in the future, this can be updated to check against `wc_tax_enabled()` which is |
| 127 | * more accurate for this evaluation. |
| 128 | */ |
| 129 | $this->is_complete_result = $is_wc_connect_taxes_enabled || |
| 130 | $third_party_complete || |
| 131 | false !== get_option( 'woocommerce_no_sales_tax' ) || |
| 132 | $this->has_existing_tax_rates(); |
| 133 | } |
| 134 | |
| 135 | return $this->is_complete_result; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Determines if a tax rate exists in the database. Result is indefinitely cached. |
| 140 | * |
| 141 | * @return bool |
| 142 | */ |
| 143 | private function has_existing_tax_rates() { |
| 144 | global $wpdb; |
| 145 | $has_existing_tax_rates = wp_cache_get( self::TAX_RATE_EXISTS_CACHE_KEY ); |
| 146 | if ( false === $has_existing_tax_rates ) { |
| 147 | $rate_exists = (bool) $wpdb->get_var( "SELECT 1 FROM {$wpdb->prefix}woocommerce_tax_rates limit 1" ); |
| 148 | $has_existing_tax_rates = $rate_exists ? 'yes' : 'no'; |
| 149 | wp_cache_set( self::TAX_RATE_EXISTS_CACHE_KEY, $has_existing_tax_rates ); |
| 150 | } |
| 151 | |
| 152 | return 'yes' === $has_existing_tax_rates; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Marks the task as actioned any time a tax rate has been added. Called from the `woocommerce_tax_rate_added` hook. |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function on_tax_rate_added() { |
| 161 | $this->mark_actioned(); |
| 162 | wp_cache_set( self::TAX_RATE_EXISTS_CACHE_KEY, 'yes' ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Clears the tax rate exists cache when a tax rate is deleted. Called from the `woocommerce_tax_rate_added` hook. |
| 167 | * |
| 168 | * @return void |
| 169 | */ |
| 170 | public function on_tax_rate_deleted() { |
| 171 | wp_cache_delete( self::TAX_RATE_EXISTS_CACHE_KEY ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Additional data. |
| 176 | * |
| 177 | * @return array |
| 178 | */ |
| 179 | public function get_additional_data() { |
| 180 | return array( |
| 181 | 'avalara_activated' => PluginsHelper::is_plugin_active( 'woocommerce-avatax' ), |
| 182 | 'tax_jar_activated' => class_exists( 'WC_Taxjar' ), |
| 183 | 'stripe_tax_activated' => PluginsHelper::is_plugin_active( 'stripe-tax-for-woocommerce' ), |
| 184 | 'woocommerce_tax_activated' => PluginsHelper::is_plugin_active( 'woocommerce-tax' ), |
| 185 | 'woocommerce_shipping_activated' => PluginsHelper::is_plugin_active( 'woocommerce-shipping' ), |
| 186 | 'woocommerce_tax_countries' => self::get_automated_support_countries(), |
| 187 | 'stripe_tax_countries' => self::get_stripe_tax_support_countries(), |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Check if the store has any enabled gateways. |
| 193 | * |
| 194 | * @return bool |
| 195 | */ |
| 196 | public static function can_use_automated_taxes() { |
| 197 | if ( ! class_exists( 'WC_Taxjar' ) ) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | return in_array( WC()->countries->get_base_country(), self::get_automated_support_countries(), true ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get an array of countries that support automated tax. |
| 206 | * |
| 207 | * @return array |
| 208 | */ |
| 209 | public static function get_automated_support_countries() { |
| 210 | // https://developers.taxjar.com/api/reference/#countries . |
| 211 | $tax_supported_countries = array_merge( |
| 212 | array( 'US', 'CA', 'AU', 'GB' ), |
| 213 | WC()->countries->get_european_union_countries() |
| 214 | ); |
| 215 | |
| 216 | return $tax_supported_countries; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get an array of countries that support Stripe tax. |
| 221 | * |
| 222 | * @return array |
| 223 | */ |
| 224 | private static function get_stripe_tax_support_countries() { |
| 225 | // https://docs.stripe.com/tax/supported-countries#supported-countries accurate as of 2024-08-26. |
| 226 | // countries with remote sales not included. |
| 227 | return array( |
| 228 | 'AU', |
| 229 | 'AT', |
| 230 | 'BE', |
| 231 | 'BG', |
| 232 | 'CA', |
| 233 | 'HR', |
| 234 | 'CY', |
| 235 | 'CZ', |
| 236 | 'DK', |
| 237 | 'EE', |
| 238 | 'FI', |
| 239 | 'FR', |
| 240 | 'DE', |
| 241 | 'GR', |
| 242 | 'HK', |
| 243 | 'HU', |
| 244 | 'IE', |
| 245 | 'IT', |
| 246 | 'JP', |
| 247 | 'LV', |
| 248 | 'LT', |
| 249 | 'LU', |
| 250 | 'MT', |
| 251 | 'NL', |
| 252 | 'NZ', |
| 253 | 'NO', |
| 254 | 'PL', |
| 255 | 'PT', |
| 256 | 'RO', |
| 257 | 'SG', |
| 258 | 'SK', |
| 259 | 'SI', |
| 260 | 'ES', |
| 261 | 'SE', |
| 262 | 'CH', |
| 263 | 'AE', |
| 264 | 'GB', |
| 265 | 'US', |
| 266 | ); |
| 267 | } |
| 268 | } |
| 269 |