get('woocommerce_sub', 0)) return;
if (empty($posted_data['billing_email']) || empty($posted_data['acym_regacy_sub'])) return;
// Get existing AcyMailing user or create one
$userClass = new UserClass();
$user = $userClass->getOneByEmail($posted_data['billing_email']);
if (empty($user)) {
$user = new stdClass();
$user->email = $posted_data['billing_email'];
$userName = [];
if (!empty($posted_data['billing_first_name'])) $userName[] = $posted_data['billing_first_name'];
if (!empty($posted_data['billing_last_name'])) $userName[] = $posted_data['billing_last_name'];
if (!empty($userName)) $user->name = implode(' ', $userName);
$user->source = 'woocommerce';
$user->id = $userClass->save($user);
}
if (empty($user->id)) return;
// Subscribe the user
$listsToSubscribe = $config->get('woocommerce_autolists', '');
if (empty($listsToSubscribe)) return;
$hiddenLists = explode(',', $listsToSubscribe);
$userClass->subscribe($user->id, $hiddenLists);
}
/**
* Declare the field for WooCommerce to display it in the checkout and get it on checkout validation.
*
* @param $fields (available WooCommerce fields)
*
* @return mixed
*/
public function addSubscriptionFieldWC($fields)
{
$config = acym_config();
if (!$config->get('woocommerce_sub', 0)) return $fields;
// Add our field at the end of the billing fields (where the email is mandatory)
$text = $config->get('woocommerce_text');
$displayTxt = empty($text) ? acym_translation('ACYM_SUBSCRIBE_NEWSLETTER') : $text;
$acyfield = [
'type' => 'checkbox',
'label' => $displayTxt,
'required' => false,
'class' => ['form-row-wide'],
];
$fields['billing']['acym_regacy_sub'] = $acyfield;
return $fields;
}
}