Automattic\WooCommerce\Internal\Admin\Schedulers

MailchimpScheduler::run()publicWC 1.0

Attempt to subscribe store_email to MailChimp.

Method of the class: MailchimpScheduler{}

No Hooks.

Return

null. Nothing (null).

Usage

$MailchimpScheduler = new MailchimpScheduler();
$MailchimpScheduler->run();

MailchimpScheduler::run() code WC 9.4.2

public function run() {
	// Abort if we've already subscribed to MailChimp.
	if ( 'yes' === get_option( self::SUBSCRIBED_OPTION_NAME ) ) {
		return false;
	}

	$profile_data = get_option( 'woocommerce_onboarding_profile' );
	if ( ! isset( $profile_data['is_agree_marketing'] ) || false === $profile_data['is_agree_marketing'] ) {
		return false;
	}

	// Abort if store_email doesn't exist.
	if ( ! isset( $profile_data['store_email'] ) ) {
		return false;
	}

	// Abort if failed requests reaches the threshold.
	if ( intval( get_option( self::SUBSCRIBED_ERROR_COUNT_OPTION_NAME, 0 ) ) >= self::MAX_ERROR_THRESHOLD ) {
		return false;
	}

	$country_code = WC()->countries->get_base_country();
	$country_name = WC()->countries->countries[ $country_code ] ?? 'N/A';

	$state      = WC()->countries->get_base_state();
	$state_name = WC()->countries->states[ $country_code ][ $state ] ?? 'N/A';

	$address = array(
		// Setting N/A for addr1, city, state, zipcode and country as they are
		// required fields. Setting '' doesn't work.
		'addr1'   => 'N/A',
		'addr2'   => '',
		'city'    => 'N/A',
		'state'   => $state_name,
		'zip'     => 'N/A',
		'country' => $country_name,
	);

	$response = $this->make_request( $profile_data['store_email'], $address );

	if ( is_wp_error( $response ) || ! isset( $response['body'] ) ) {
		$this->handle_request_error();
		return false;
	}

	$body = json_decode( $response['body'] );
	if ( isset( $body->success ) && true === $body->success ) {
		update_option( self::SUBSCRIBED_OPTION_NAME, 'yes' );
		return true;
	}

	$this->handle_request_error( $body );
	return false;
}