Automattic\WooCommerce\Internal\Admin\Schedulers
MailchimpScheduler::run
Attempt to subscribe store_email to MailChimp.
Method of the class: MailchimpScheduler{}
No Hooks.
Returns
null. Nothing (null).
Usage
$MailchimpScheduler = new MailchimpScheduler(); $MailchimpScheduler->run();
MailchimpScheduler::run() MailchimpScheduler::run code WC 10.3.5
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();
$state = WC()->countries->get_base_state();
$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 ?? 'N/A',
'zip' => 'N/A',
'country' => $country_code ?? 'N/A',
);
$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;
}