applying || Connection::get() === null) { return; } if (is_admin() && ! wp_doing_ajax()) { return; } $code = $this->cookieCode(); if ($code === null) { return; } $cart = function_exists('WC') && WC()->cart ? WC()->cart : null; if (! $cart || $cart->is_empty() || $cart->get_applied_coupons()) { return; } if ($this->alreadyTried($code)) { return; } $this->markTried($code); if (! wc_get_coupon_id_by_code($code)) { return; } $this->applying = true; $cart->apply_coupon($code); $this->applying = false; } private function cookieCode(): ?string { $raw = isset($_COOKIE[self::COOKIE]) ? sanitize_text_field(wp_unslash($_COOKIE[self::COOKIE])) : ''; return $raw === '' ? null : wc_format_coupon_code($raw); } private function alreadyTried(string $code): bool { $session = function_exists('WC') && WC()->session ? WC()->session : null; return $session ? $session->get(self::SESSION_KEY) === $code : false; } private function markTried(string $code): void { $session = function_exists('WC') && WC()->session ? WC()->session : null; if ($session) { $session->set(self::SESSION_KEY, $code); } } }