Pokud zákazník zvolí platbu převodem nebo z nějakého důvodu nedokončí platbu kartou a pokud tak neučiní ani po nějaké době, připomeneme mu, že má neuzavřenou objednávku.
Nejprve do Woocommerce -> Nastavení – > Obecná vložíme pole, kde si budeme moci nastavit po kolika dnech se připomínka odešle a kolikrát se to bude opakovat:
add_filter('woocommerce_get_settings_general', 'custom_reminder_email_settings', 10, 2);
function custom_reminder_email_settings($settings, $current_section) {
if ($current_section == '') { // nebo 'email' pro sekci E-maily, případně specifickou sekci, kde to chceš zobrazit
$settings[] = array(
'title' => __('Nastavení připomínek pro nezaplacené objednávky', 'text-domain'),
'type' => 'title',
'desc' => 'Zde můžete nastavit interval a počet opakování pro připomínky.',
'id' => 'custom_reminder_email_options'
);
$settings[] = array(
'title' => __('Interval připomenutí (dny)', 'text-domain'),
'id' => 'custom_reminder_interval',
'type' => 'number',
'desc' => __('Počet dnů po vytvoření objednávky, kdy bude zaslán první e-mail.', 'text-domain'),
'default' => '3',
'desc_tip' => true,
);
$settings[] = array(
'title' => __('Počet opakování', 'text-domain'),
'id' => 'custom_reminder_repeats',
'type' => 'number',
'desc' => __('Kolikrát se e-mail pošle, pokud zůstane objednávka nezaplacená.', 'text-domain'),
'default' => '3',
'desc_tip' => true,
);
$settings[] = array(
'type' => 'sectionend',
'id' => 'custom_reminder_email_options'
);
}
return $settings;
}
Dále musíme vytvořit CRON akci pro automatické odesílání těchto mailů:
// Nastaví vlastní cron událost
add_action('wp', 'schedule_custom_reminder_email');
function schedule_custom_reminder_email() {
if (!wp_next_scheduled('custom_reminder_email_event')) {
wp_schedule_event(time(), 'daily', 'custom_reminder_email_event');
}
}
// Akce pro odeslání e-mailu
add_action('custom_reminder_email_event', 'send_reminder_email_for_unpaid_orders');
Dále chceme do mailu vložit QR kód pro platbu, takže ho vygenerujeme na základě částky z objednávky a platebních údajů nastavených ve Woocommerce:
// Funkce pro vytvoření URL QR kódu pro platbu
function generate_payment_qr_code_url($order) {
$account_number = get_option('woocommerce_bacs_account_number'); // Číslo účtu z nastavení WooCommerce
$bank_code = get_option('woocommerce_bacs_bank_code'); // Kód banky
$amount = $order->get_total(); // Celková částka objednávky
$variable_symbol = $order->get_id(); // ID objednávky jako variabilní symbol
// Formátování QR platby (dle formátu ČR)
$qr_data = "SPD*1.0*ACC:CZ${bank_code}${account_number}*AM:${amount}*CC:CZK*X-VS:${variable_symbol}";
// Vytvoření URL pro QR kód pomocí Google Chart API
$qr_code_url = 'https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=' . urlencode($qr_data);
return $qr_code_url;
}
Vlastní funkce odeslání mailu:
function send_reminder_email_for_unpaid_orders() {
$interval = get_option('custom_reminder_interval', 3);
$repeats = get_option('custom_reminder_repeats', 3);
$args = array(
'status' => 'pending',
'date_created' => '<' . (time() - ($interval * 24 * 60 * 60)),
);
$orders = wc_get_orders($args);
foreach ($orders as $order) {
$order_id = $order->get_id();
$reminder_count = get_post_meta($order_id, '_reminder_count', true);
if (!$reminder_count) {
$reminder_count = 0;
}
if ($reminder_count < $repeats) {
$customer_email = $order->get_billing_email();
$subject = 'Připomenutí nezaplacené objednávky #' . $order_id;
$qr_code_url = generate_payment_qr_code_url($order);
$message = 'Dobrý den, připomínáme Vám, že Vaše objednávka #' . $order_id . ' je stále nezaplacená.<br>';
$message .= 'Celková částka: ' . wc_price($order->get_total()) . '<br>';
$message .= 'Můžete využít QR kód pro rychlou platbu:<br>';
$message .= '<img src="' . esc_url($qr_code_url) . '" alt="QR kód pro platbu" /><br>';
$message .= 'Děkujeme za Vaši objednávku!';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($customer_email, $subject, $message, $headers);
update_post_meta($order_id, '_reminder_count', $reminder_count + 1);
}
}
}
Zrušení CRON akce, pokud už nebude potřeba:
function unschedule_custom_reminder_email() {
$timestamp = wp_next_scheduled('custom_reminder_email_event');
if ($timestamp) {
wp_unschedule_event($timestamp, 'custom_reminder_email_event');
}
}
register_deactivation_hook(__FILE__, 'unschedule_custom_reminder_email');