• add_shortcode('display_discount_coupon', 'display_discount_coupon_shortcode');
    
    function display_discount_coupon_shortcode() {
        // Check if the cookie is already set
        if (!isset($_COOKIE['discount_coupon'])) {
            // Generate a new discount coupon
            $discount_coupon = generate_discount_coupon();
    
            // Set the cookie for 1 year
            setcookie('discount_coupon', $discount_coupon, time() + (365 * 24 * 60 * 60), '/');
    
            // Write data to file
            write_to_file(date('Y-m-d H:i:s'), $discount_coupon, get_site_url());
        } else {
            // Get the discount coupon from the cookie
            $discount_coupon = $_COOKIE['discount_coupon'];
        }
    
        // Display the discount coupon
        return $discount_coupon;
    }
    
    function generate_discount_coupon() {
        // Generate a new discount coupon code
        return substr(md5(time()), 0, 10);
    }
    
    function write_to_file($date, $discount_coupon, $url) {
        // Write data to file
        $file = fopen('discount_coupons.txt', 'a');
        fwrite($file, "$date, $discount_coupon, $url\n");
        fclose($file);
    }
    
    [display_discount_coupon]

    Всем привет. Выше небольшой код, который генерирует уникальный купон на скидку и записывает его в куки сроком на 1 год. Если зайти повторно по идее должен показывать изначальный купон, но почему то каждый раз генерирует новый купон. Вопрос — что здесь не так ?

Просмотр 1 ответа (всего 1)
  • что здесь не так ?

    Невозможно установить куку после того как начался вывод html страницы.

Просмотр 1 ответа (всего 1)

Тема «не срабатывают куки для генерации купонов» закрыта для новых ответов.