AdditionalPayments.php
1 year ago
Appearance.php
2 years ago
CustomizeStore.php
2 weeks ago
ExperimentalShippingRecommendation.php
2 weeks ago
ExtendStore.php
1 year ago
GetMobileApp.php
3 years ago
LaunchYourStore.php
2 weeks ago
Marketing.php
2 weeks ago
Payments.php
2 weeks ago
Products.php
2 weeks ago
ReviewShippingOptions.php
4 years ago
Shipping.php
2 weeks ago
StoreCreation.php
4 years ago
StoreDetails.php
2 weeks ago
Tax.php
2 weeks ago
TourInAppMarketplace.php
2 years ago
WooCommercePayments.php
2 weeks ago
Shipping.php
198 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks; |
| 4 | |
| 5 | use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile; |
| 6 | use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task; |
| 7 | use WC_Data_Store; |
| 8 | |
| 9 | /** |
| 10 | * Shipping Task |
| 11 | */ |
| 12 | class Shipping extends Task { |
| 13 | |
| 14 | const ZONE_COUNT_TRANSIENT_NAME = 'woocommerce_shipping_task_zone_count_transient'; |
| 15 | |
| 16 | /** |
| 17 | * Constructor |
| 18 | * |
| 19 | * @param TaskList $task_list Parent task list. |
| 20 | */ |
| 21 | public function __construct( $task_list = null ) { |
| 22 | parent::__construct( $task_list ); |
| 23 | // wp_ajax_woocommerce_shipping_zone_methods_save_changes |
| 24 | // and wp_ajax_woocommerce_shipping_zones_save_changes get fired |
| 25 | // when a new zone is added or an existing one has been changed. |
| 26 | add_action( 'wp_ajax_woocommerce_shipping_zones_save_changes', array( __CLASS__, 'delete_zone_count_transient' ), 9 ); |
| 27 | add_action( 'wp_ajax_woocommerce_shipping_zone_methods_save_changes', array( __CLASS__, 'delete_zone_count_transient' ), 9 ); |
| 28 | add_action( 'woocommerce_shipping_zone_method_added', array( __CLASS__, 'delete_zone_count_transient' ), 9 ); |
| 29 | add_action( 'woocommerce_after_shipping_zone_object_save', array( __CLASS__, 'delete_zone_count_transient' ), 9 ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * ID. |
| 34 | * |
| 35 | * @return string |
| 36 | */ |
| 37 | public function get_id() { |
| 38 | return 'shipping'; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Contextual image URL. |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public function get_image_url() { |
| 47 | return WC()->plugin_url() . '/assets/images/task_list/shipping-illustration.svg'; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Alt text for the contextual image. |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public function get_image_alt() { |
| 56 | return __( 'Shipping illustration', 'woocommerce' ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Title. |
| 61 | * |
| 62 | * @return string |
| 63 | */ |
| 64 | public function get_title() { |
| 65 | return __( 'Select your shipping options', 'woocommerce' ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Content. |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function get_content() { |
| 74 | return __( |
| 75 | "Set your store location and where you'll ship to.", |
| 76 | 'woocommerce' |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Time. |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public function get_time() { |
| 86 | return __( '1 minute', 'woocommerce' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Task completion. |
| 91 | * |
| 92 | * @return bool |
| 93 | */ |
| 94 | public function is_complete() { |
| 95 | return self::has_shipping_zones(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Task visibility. |
| 100 | * |
| 101 | * @return bool |
| 102 | */ |
| 103 | public function can_view() { |
| 104 | if ( 'yes' === get_option( 'woocommerce_admin_created_default_shipping_zones' ) ) { |
| 105 | // If the user has already created a default shipping zone, we don't need to show the task. |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Do not display the task when: |
| 111 | * - The store sells digital products only |
| 112 | * Display the task when: |
| 113 | * - We don't know where the store's located |
| 114 | * - The store is located in the UK, Australia or Canada |
| 115 | */ |
| 116 | |
| 117 | if ( self::is_selling_digital_type_only() ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | $default_store_country = wc_format_country_state_string( get_option( 'woocommerce_default_country', '' ) )['country']; |
| 122 | |
| 123 | // Check if a store address is set so that we don't default to WooCommerce's default country US. |
| 124 | // Similar logic: https://github.com/woocommerce/woocommerce/blob/059d542394b48468587f252dcb6941c6425cd8d3/plugins/woocommerce-admin/client/profile-wizard/steps/store-details/index.js#L511-L516. |
| 125 | $store_country = ''; |
| 126 | if ( ! empty( get_option( 'woocommerce_store_address', '' ) ) || 'US' !== $default_store_country ) { |
| 127 | $store_country = $default_store_country; |
| 128 | } |
| 129 | |
| 130 | // Unknown country. |
| 131 | if ( empty( $store_country ) ) { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | return in_array( $store_country, array( 'US', 'CA', 'AU', 'NZ', 'SG', 'HK', 'GB', 'ES', 'IT', 'DE', 'FR', 'MX', 'CO', 'CL', 'AR', 'PE', 'BR', 'UY', 'GT', 'NL', 'AT', 'BE', 'IE', 'PT' ), true ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Action URL. |
| 140 | * |
| 141 | * @return string |
| 142 | */ |
| 143 | public function get_action_url() { |
| 144 | return self::has_shipping_zones() |
| 145 | ? admin_url( 'admin.php?page=wc-settings&tab=shipping' ) |
| 146 | : null; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Check if the store has any shipping zones. |
| 151 | * |
| 152 | * @return bool |
| 153 | */ |
| 154 | public static function has_shipping_zones() { |
| 155 | $zone_count = get_transient( self::ZONE_COUNT_TRANSIENT_NAME ); |
| 156 | if ( false !== $zone_count ) { |
| 157 | return (int) $zone_count > 0; |
| 158 | } |
| 159 | |
| 160 | $zone_count = count( WC_Data_Store::load( 'shipping-zone' )->get_zones() ); |
| 161 | set_transient( self::ZONE_COUNT_TRANSIENT_NAME, $zone_count ); |
| 162 | |
| 163 | return $zone_count > 0; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Check if the store has physical products. |
| 168 | * |
| 169 | * @return bool |
| 170 | */ |
| 171 | public static function has_physical_products() { |
| 172 | $profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() ); |
| 173 | $product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array(); |
| 174 | |
| 175 | return in_array( 'physical', $product_types, true ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Delete the zone count transient used in has_shipping_zones() method |
| 180 | * to refresh the cache. |
| 181 | */ |
| 182 | public static function delete_zone_count_transient() { |
| 183 | delete_transient( self::ZONE_COUNT_TRANSIENT_NAME ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Check if the store sells digital products only. |
| 188 | * |
| 189 | * @return bool |
| 190 | */ |
| 191 | private static function is_selling_digital_type_only() { |
| 192 | $profiler_data = get_option( OnboardingProfile::DATA_OPTION, array() ); |
| 193 | $product_types = isset( $profiler_data['product_types'] ) ? $profiler_data['product_types'] : array(); |
| 194 | |
| 195 | return array( 'downloads' ) === $product_types; |
| 196 | } |
| 197 | } |
| 198 |