step ) : ?>
step ) : ?>
step ) : ?>
steps;
?>
$step ) : ?>
';
if ( ! empty( $this->steps[ $this->step ]['view'] ) ) {
call_user_func( $this->steps[ $this->step ]['view'], $this );
}
echo '';
}
/**
* Initial "store setup" step.
* Location, product type, page setup, and tracking opt-in.
*/
public function wc_setup_store_setup() {
$address = WC()->countries->get_base_address();
$address_2 = WC()->countries->get_base_address_2();
$city = WC()->countries->get_base_city();
$state = WC()->countries->get_base_state();
$country = WC()->countries->get_base_country();
$postcode = WC()->countries->get_base_postcode();
$currency = get_option( 'woocommerce_currency', 'GBP' );
$product_type = get_option( 'woocommerce_product_type', 'both' );
$sell_in_person = get_option( 'woocommerce_sell_in_person', 'none_selected' );
if ( empty( $country ) ) {
$user_location = WC_Geolocation::geolocate_ip();
$country = $user_location['country'];
$state = $user_location['state'];
} elseif ( empty( $state ) ) {
$state = '*';
}
$locale_info = include( WC()->plugin_path() . '/i18n/locale-info.php' );
$currency_by_country = wp_list_pluck( $locale_info, 'currency_code' );
?>
plugin_path() . '/i18n/locale-info.php' );
$country = WC()->countries->get_base_country();
// Set currency formatting options based on chosen location and currency.
if (
isset( $locale_info[ $country ] ) &&
$locale_info[ $country ]['currency_code'] === $currency_code
) {
update_option( 'woocommerce_currency_pos', $locale_info[ $country ]['currency_pos'] );
update_option( 'woocommerce_price_decimal_sep', $locale_info[ $country ]['decimal_sep'] );
update_option( 'woocommerce_price_num_decimals', $locale_info[ $country ]['num_decimals'] );
update_option( 'woocommerce_price_thousand_sep', $locale_info[ $country ]['thousand_sep'] );
}
if ( $tracking ) {
update_option( 'woocommerce_allow_tracking', 'yes' );
WC_Tracker::send_tracking_data( true );
} else {
update_option( 'woocommerce_allow_tracking', 'no' );
}
WC_Install::create_pages();
wp_redirect( esc_url_raw( $this->get_next_step_link() ) );
exit;
}
/**
* Finishes replying to the client, but keeps the process running for further (async) code execution.
*
* @see https://core.trac.wordpress.org/ticket/41358 .
*/
protected function close_http_connection() {
// Only 1 PHP process can access a session object at a time, close this so the next request isn't kept waiting.
// @codingStandardsIgnoreStart
if ( session_id() ) {
session_write_close();
}
// @codingStandardsIgnoreEnd
wc_set_time_limit( 0 );
// fastcgi_finish_request is the cleanest way to send the response and keep the script running, but not every server has it.
if ( is_callable( 'fastcgi_finish_request' ) ) {
fastcgi_finish_request();
} else {
// Fallback: send headers and flush buffers.
if ( ! headers_sent() ) {
header( 'Connection: close' );
}
@ob_end_flush();
flush();
}
}
/**
* Function called after the HTTP request is finished, so it's executed without the client having to wait for it.
*
* @see WC_Admin_Setup_Wizard::install_plugin
* @see WC_Admin_Setup_Wizard::install_theme
*/
public function run_deferred_actions() {
$this->close_http_connection();
foreach ( $this->deferred_actions as $action ) {
call_user_func_array( $action['func'], $action['args'] );
// Clear the background installation flag if this is a plugin.
if (
isset( $action['func'][1] ) &&
'background_installer' === $action['func'][1] &&
isset( $action['args'][0] )
) {
delete_option( 'woocommerce_setup_background_installing_' . $action['args'][0] );
}
}
}
/**
* Helper method to queue the background install of a plugin.
*
* @param string $plugin_id Plugin id used for background install.
* @param array $plugin_info Plugin info array containing at least main file and repo slug.
*/
protected function install_plugin( $plugin_id, $plugin_info ) {
// Make sure we don't trigger multiple simultaneous installs.
if ( get_option( 'woocommerce_setup_background_installing_' . $plugin_id ) ) {
return;
}
if ( ! empty( $plugin_info['file'] ) && is_plugin_active( $plugin_info['file'] ) ) {
return;
}
if ( empty( $this->deferred_actions ) ) {
add_action( 'shutdown', array( $this, 'run_deferred_actions' ) );
}
array_push( $this->deferred_actions, array(
'func' => array( 'WC_Install', 'background_installer' ),
'args' => array( $plugin_id, $plugin_info ),
) );
// Set the background installation flag for this plugin.
update_option( 'woocommerce_setup_background_installing_' . $plugin_id, true );
}
/**
* Helper method to queue the background install of a theme.
*
* @param string $theme_id Theme id used for background install.
*/
protected function install_theme( $theme_id ) {
if ( empty( $this->deferred_actions ) ) {
add_action( 'shutdown', array( $this, 'run_deferred_actions' ) );
}
array_push( $this->deferred_actions, array(
'func' => array( 'WC_Install', 'theme_background_installer' ),
'args' => array( $theme_id ),
) );
}
/**
* Helper method to install Jetpack.
*/
protected function install_jetpack() {
$this->install_plugin( 'jetpack', array(
'file' => 'jetpack/jetpack.php',
'name' => __( 'Jetpack', 'woocommerce' ),
'repo-slug' => 'jetpack',
) );
}
/**
* Helper method to install WooCommerce Services and its Jetpack dependency.
*/
protected function install_woocommerce_services() {
$this->install_jetpack();
$this->install_plugin( 'woocommerce-services', array(
'file' => 'woocommerce-services/woocommerce-services.php',
'name' => __( 'WooCommerce Services', 'woocommerce' ),
'repo-slug' => 'woocommerce-services',
) );
}
/**
* Get the WCS shipping carrier for a given country code.
*
* Can also be used to determine if WCS supports a given country.
*
* @param string $country_code Country Code.
* @param string $currency_code Currecy Code.
* @return bool|string Carrier name if supported, boolean False otherwise.
*/
protected function get_wcs_shipping_carrier( $country_code, $currency_code ) {
switch ( array( $country_code, $currency_code ) ) {
case array( 'US', 'USD' ):
return 'USPS';
case array( 'CA', 'CAD' ):
return 'Canada Post';
default:
return false;
}
}
/**
* Get shipping methods based on country code.
*
* @param string $country_code Country code.
* @param string $currency_code Currency code.
* @return array
*/
protected function get_wizard_shipping_methods( $country_code, $currency_code ) {
$shipping_methods = array(
'live_rates' => array(
'name' => __( 'Live Rates', 'woocommerce' ),
'description' => __( 'WooCommerce Services and Jetpack will be installed and activated for you.', 'woocommerce' ),
),
'flat_rate' => array(
'name' => __( 'Flat Rate', 'woocommerce' ),
'description' => __( 'Set a fixed price to cover shipping costs.', 'woocommerce' ),
'settings' => array(
'cost' => array(
'type' => 'text',
'default_value' => __( 'Cost', 'woocommerce' ),
'description' => __( 'What would you like to charge for flat rate shipping?', 'woocommerce' ),
'required' => true,
),
),
),
'free_shipping' => array(
'name' => __( 'Free Shipping', 'woocommerce' ),
'description' => __( "Don't charge for shipping.", 'woocommerce' ),
),
);
$live_rate_carrier = $this->get_wcs_shipping_carrier( $country_code, $currency_code );
if ( false === $live_rate_carrier || ! current_user_can( 'install_plugins' ) ) {
unset( $shipping_methods['live_rates'] );
}
return $shipping_methods;
}
/**
* Render the available shipping methods for a given country code.
*
* @param string $country_code Country code.
* @param string $currency_code Currency code.
* @param string $input_prefix Input prefix.
*/
protected function shipping_method_selection_form( $country_code, $currency_code, $input_prefix ) {
$live_rate_carrier = $this->get_wcs_shipping_carrier( $country_code, $currency_code );
$selected = $live_rate_carrier ? 'live_rates' : 'flat_rate';
$shipping_methods = $this->get_wizard_shipping_methods( $country_code, $currency_code );
?>
$method ) : ?>
$method ) : ?>
$setting ) : ?>
/>
countries->get_base_country();
$country_name = WC()->countries->countries[ $country_code ];
$prefixed_country_name = WC()->countries->estimated_for_prefix( $country_code ) . $country_name;
$currency_code = get_woocommerce_currency();
$wcs_carrier = $this->get_wcs_shipping_carrier( $country_code, $currency_code );
$existing_zones = WC_Shipping_Zones::get_zones();
$locale_info = include( WC()->plugin_path() . '/i18n/locale-info.php' );
if ( isset( $locale_info[ $country_code ] ) ) {
$dimension_unit = $locale_info[ $country_code ]['dimension_unit'];
$weight_unit = $locale_info[ $country_code ]['weight_unit'];
} else {
$dimension_unit = 'cm';
$weight_unit = 'kg';
}
if ( ! empty( $existing_zones ) ) {
$intro_text = __( 'How would you like units on your store displayed?', 'woocommerce' );
} elseif ( $wcs_carrier ) {
/* translators: %1$s: country name including the 'the' prefix, %2$s: shipping carrier name */
$intro_text = sprintf(
__( "You're all set up to ship anywhere in %1\$s, and outside of it. We recommend using live rates (which are powered by our WooCommerce Services plugin and Jetpack) to get accurate %2\$s shipping prices to cover the cost of order fulfillment.", 'woocommerce' ),
$prefixed_country_name,
$wcs_carrier
);
} else {
/* translators: %s: country name including the 'the' prefix if needed */
$intro_text = sprintf(
__( "You can choose which countries you'll be shipping to and with which methods. To get started, we've set you up with shipping inside and outside of %s.", 'woocommerce' ),
$prefixed_country_name
);
}
?>
get_next_step_link() ) );
exit;
}
// Install WooCommerce Services if live rates were selected.
if (
( $setup_domestic && 'live_rates' === $domestic_method ) ||
( $setup_intl && 'live_rates' === $intl_method )
) {
$this->install_woocommerce_services();
}
/*
* If enabled, create a shipping zone containing the country the
* store is located in, with the selected method preconfigured.
*/
if ( $setup_domestic ) {
$country = WC()->countries->get_base_country();
$zone = new WC_Shipping_Zone( null );
$zone->set_zone_order( 0 );
$zone->add_location( $country, 'country' );
if ( 'live_rates' === $domestic_method ) {
// Signal WooCommerce Services to setup the domestic zone.
update_option( 'woocommerce_setup_domestic_live_rates_zone', true, 'no' );
} else {
$instance_id = $zone->add_shipping_method( $domestic_method );
}
$zone->save();
// Save chosen shipping method settings (using REST controller for convenience).
if ( isset( $instance_id ) && ! empty( $_POST['shipping_zones']['domestic'][ $domestic_method ] ) ) {
$method_controller = new WC_REST_Shipping_Zone_Methods_Controller();
// @codingStandardsIgnoreStart
$method_controller->update_item( array(
'zone_id' => $zone->get_id(),
'instance_id' => $instance_id,
'settings' => wp_unslash( $_POST['shipping_zones']['domestic'][ $domestic_method ] ),
) );
// @codingStandardsIgnoreEnd
}
}
// If enabled, set the selected method for the "rest of world" zone.
if ( $setup_intl ) {
if ( 'live_rates' === $intl_method ) {
// Signal WooCommerce Services to setup the international zone.
update_option( 'woocommerce_setup_intl_live_rates_zone', true, 'no' );
} else {
$zone = new WC_Shipping_Zone( 0 );
$instance_id = $zone->add_shipping_method( $intl_method );
$zone->save();
}
// Save chosen shipping method settings (using REST controller for convenience).
if ( isset( $instance_id ) && ! empty( $_POST['shipping_zones']['intl'][ $intl_method ] ) ) {
$method_controller = new WC_REST_Shipping_Zone_Methods_Controller();
// @codingStandardsIgnoreStart
$method_controller->update_item( array(
'zone_id' => $zone->get_id(),
'instance_id' => $instance_id,
'settings' => wp_unslash( $_POST['shipping_zones']['intl'][ $intl_method ] ),
) );
// @codingStandardsIgnoreEnd
}
}
// Notify the user that no shipping methods are configured.
if ( ! $setup_domestic && ! $setup_intl ) {
WC_Admin_Notices::add_notice( 'no_shipping_methods' );
}
wp_redirect( esc_url_raw( $this->get_next_step_link() ) );
exit;
}
/**
* Is Stripe country supported
* https://stripe.com/global .
*
* @param string $country_code Country code.
*/
protected function is_stripe_supported_country( $country_code ) {
$stripe_supported_countries = array(
'AU',
'AT',
'BE',
'CA',
'DK',
'FI',
'FR',
'DE',
'HK',
'IE',
'JP',
'LU',
'NL',
'NZ',
'NO',
'SG',
'ES',
'SE',
'CH',
'GB',
'US',
);
return in_array( $country_code, $stripe_supported_countries );
}
/**
* Is Klarna Checkout country supported
*
* @param string $country_code Country code.
*/
protected function is_klarna_checkout_supported_country( $country_code ) {
$supported_countries = array(
'SE', //Sweden
'FI', //Finland
'NO', //Norway
'NL', //Netherlands
);
return in_array( $country_code, $supported_countries, true );
}
/**
* Is Klarna Payments country supported
*
* @param string $country_code Country code.
*/
protected function is_klarna_payments_supported_country( $country_code ) {
$supported_countries = array(
'DK', //Denmark
'DE', //Germany
'AT', //Austria
);
return in_array( $country_code, $supported_countries, true );
}
/**
* Is Square country supported
*
* @param string $country_code Country code.
*/
protected function is_square_supported_country( $country_code ) {
$square_supported_countries = array(
'US',
'CA',
'JP',
'GB',
'AU',
);
return in_array( $country_code, $square_supported_countries, true );
}
/**
* Helper method to retrieve the current user's email address.
*
* @return string Email address
*/
protected function get_current_user_email() {
$current_user = wp_get_current_user();
$user_email = $current_user->user_email;
return $user_email;
}
/**
* Array of all possible "in cart" gateways that can be offered.
*
* @return array
*/
protected function get_wizard_available_in_cart_payment_gateways() {
$user_email = $this->get_current_user_email();
$stripe_description = '
' . sprintf(
__( 'Accept debit and credit cards in 135+ currencies, methods such as Alipay, and one-touch checkout with Apple Pay. Learn more.', 'woocommerce' ),
'https://woocommerce.com/products/stripe/'
) . '
';
$paypal_ec_description = '
' . sprintf(
__( 'Safe and secure payments using credit cards or your customer\'s PayPal account. Learn more.', 'woocommerce' ),
'https://wordpress.org/plugins/woocommerce-gateway-paypal-express-checkout/'
) . '
';
$klarna_checkout_description = '
' . sprintf(
__( 'Pay now, pay later, slice it. No credit card numbers, no passwords, no worries. Learn more about Klarna.', 'woocommerce' ),
'https://woocommerce.com/products/klarna/'
) . '
';
$klarna_payments_description = '
' . sprintf(
__( 'Pay later, slice it. No credit card numbers, no passwords, no worries. Learn more about Klarna.', 'woocommerce' ),
'https://woocommerce.com/products/klarna/'
) . '
';
$square_description = '
' . sprintf(
__( 'Securely accept credit and debit cards with one low rate, no surprise fees (custom rates available). Sell online and in store and track sales and inventory in one place. Learn more about Square.', 'woocommerce' ),
'https://woocommerce.com/products/square/'
) . '
Terms of Service and to share details with WordPress.com', 'woocommerce' ) ),
'https://wordpress.com/tos',
'https://jetpack.com/support/what-data-does-jetpack-sync'
);
?>
__( "Sorry! We tried, but we couldn't connect Jetpack just now ๐ญ. Please go to the Plugins tab to connect Jetpack, so that you can finish setting up your store.", 'woocommerce' ),
'jetpack_cant_be_installed' => __( "Sorry! We tried, but we couldn't install Jetpack for you ๐ญ. Please go to the Plugins tab to install it, and finish setting up your store.", 'woocommerce' ),
'register_http_request_failed' => __( "Sorry! We couldn't contact Jetpack just now ๐ญ. Please make sure that your site is visible over the internet, and that it accepts incoming and outgoing requests via curl. You can also try to connect to Jetpack again, and if you run into any more issues, please contact support.", 'woocommerce' ),
'siteurl_private_ip_dev' => __( "Your site might be on a private network. Jetpack can only connect to public sites. Please make sure your site is visible over the internet, and then try connecting again ๐." , 'woocommerce' ),
);
}
protected function get_activate_error_message( $code = '' ) {
$errors = $this->get_all_activate_errors();
return array_key_exists( $code, $errors ) ? $errors[ $code ] : $errors['default'];
}
/**
* Activate step save.
*
* Install, activate, and launch connection flow for Jetpack.
*/
public function wc_setup_activate_save() {
check_admin_referer( 'wc-setup' );
// Leave a note for WooCommerce Services that Jetpack has been opted into.
update_option( 'woocommerce_setup_jetpack_opted_in', true );
WC_Install::background_installer( 'jetpack', array(
'file' => 'jetpack/jetpack.php',
'name' => __( 'Jetpack', 'woocommerce' ),
'repo-slug' => 'jetpack',
) );
// Did Jetpack get successfully installed?
if ( ! class_exists( 'Jetpack' ) ) {
wp_redirect( esc_url_raw( add_query_arg( 'activate_error', 'jetpack_cant_be_installed' ) ) );
exit;
}
Jetpack::maybe_set_version_option();
$register_result = Jetpack::try_registration();
if ( is_wp_error( $register_result ) ) {
$result_error_code = $register_result->get_error_code();
$jetpack_error_code = array_key_exists( $result_error_code, $this->get_all_activate_errors() ) ? $result_error_code : 'register';
wp_redirect( esc_url_raw( add_query_arg( 'activate_error', $jetpack_error_code ) ) );
exit;
}
$redirect_url = esc_url_raw( add_query_arg( array(
'page' => 'wc-setup',
'step' => 'activate',
'from' => 'wpcom',
'activate_error' => false,
), admin_url() ) );
$connection_url = Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' );
wp_redirect( esc_url_raw( $connection_url ) );
exit;
}
/**
* Final step.
*/
public function wc_setup_ready() {
// We've made it! Don't prompt the user to run the wizard again.
WC_Admin_Notices::remove_notice( 'install' );
$user_email = $this->get_current_user_email();
$videos_url = 'https://docs.woocommerce.com/document/woocommerce-guided-tour-videos/?utm_source=setupwizard&utm_medium=product&utm_content=videos&utm_campaign=woocommerceplugin';
$docs_url = 'https://docs.woocommerce.com/documentation/plugins/woocommerce/getting-started/?utm_source=setupwizard&utm_medium=product&utm_content=docs&utm_campaign=woocommerceplugin';
$help_text = sprintf(
/* translators: %1$s: link to videos, %2$s: link to docs */
__( 'Watch our guided tour videos to learn more about WooCommerce, and visit WooCommerce.com to learn more about getting started.', 'woocommerce' ),
$videos_url,
$docs_url
);
?>