installed) return; ?>
get('woocommerce_sub', 0)) return; if (empty($posted_data['billing_email']) || empty($posted_data['acym_regacy_sub'])) return; $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']; $name = implode(' ', $userName); $this->subscribeFromCheckout($posted_data['billing_email'], $name); } /** * Subscribe user when the WooCommerce gutenberg checkout is processed * * @param $order : WooCommerce order */ public function subscribeUserOnCheckoutWCApi($order, $request) { $config = acym_config(); if (!$config->get('woocommerce_sub', 0)) return; $body = json_decode($request->get_body(), true); if (empty($body['billing_address']['email']) || empty($body['extensions'][self::WC_ACY_SUBSCRIBE_KEY]['is-subscribing'])) { return; } $userName = []; if (!empty($body['billing_address']['first_name'])) $userName[] = $body['billing_address']['first_name']; if (!empty($body['billing_address']['last_name'])) $userName[] = $body['billing_address']['last_name']; $name = implode(' ', $userName); $this->subscribeFromCheckout($body['billing_address']['email'], $name); } private function subscribeFromCheckout($email, $name = '') { $config = acym_config(); $userClass = new UserClass(); $user = $userClass->getOneByEmail($email); if (empty($user)) { $user = new stdClass(); $user->email = $email; if (!empty($name)) { $user->name = $name; } $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; } }