Misc fixes: cleanup templates and supporting updates
This commit is contained in:
@@ -352,6 +352,21 @@ class EmailRenderer
|
||||
'payment_link' => $data['payment_link'] ?? '',
|
||||
];
|
||||
|
||||
// O1 — Derive `billing_schedule` (e.g. "Every 3 Months") and
|
||||
// `payment_method_title` (e.g. "Stripe" rather than the raw
|
||||
// gateway id "stripe"). The data is on the subscription row but
|
||||
// isn't pre-formatted. We rebuild both so email templates can
|
||||
// show the merchant-friendly string without duplicating the
|
||||
// pluralization + lookup logic.
|
||||
$sub_variables['billing_schedule'] = self::format_billing_schedule(
|
||||
isset($sub->billing_period) ? (string) $sub->billing_period : '',
|
||||
isset($sub->billing_interval) ? (int) $sub->billing_interval : 1
|
||||
);
|
||||
$sub_variables['payment_method_title'] = self::resolve_payment_method_title(
|
||||
isset($sub->payment_method) ? (string) $sub->payment_method : '',
|
||||
$data['order'] ?? null
|
||||
);
|
||||
|
||||
// Get product name if not already set
|
||||
if (!isset($variables['product_name']) && isset($data['product']) && $data['product'] instanceof \WC_Product) {
|
||||
$sub_variables['product_name'] = $data['product']->get_name();
|
||||
@@ -381,6 +396,57 @@ class EmailRenderer
|
||||
return apply_filters('woonoow_email_variables', $variables, $event_id, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* O1 — Format a billing schedule string like "Every 3 Months" from raw
|
||||
* period and interval columns. Mirrors the controller's
|
||||
* `enrich_subscription()` math so email templates show the same string
|
||||
* the customer sees in the SPA. Falls back to the period string itself
|
||||
* if the period is unknown.
|
||||
*/
|
||||
public static function format_billing_schedule($period, $interval)
|
||||
{
|
||||
$period_labels = [
|
||||
'day' => __('day', 'woonoow'),
|
||||
'week' => __('week', 'woonoow'),
|
||||
'month' => __('month', 'woonoow'),
|
||||
'year' => __('year', 'woonoow'),
|
||||
];
|
||||
$interval = max(1, (int) $interval);
|
||||
$period_label = $period_labels[$period] ?? $period;
|
||||
if ($interval > 1) {
|
||||
$period_label .= 's';
|
||||
}
|
||||
return sprintf(__('Every %s%s', 'woonoow'), $interval, $period_label);
|
||||
}
|
||||
|
||||
/**
|
||||
* O1 — Resolve a human-friendly payment method title from a stored gateway
|
||||
* id. Order of preference:
|
||||
* 1. The order's `payment_method_title` (most accurate; set by gateway
|
||||
* at checkout — e.g. "PayPal — Visa ending in 1234")
|
||||
* 2. The registered WC gateway's `get_title()` (e.g. "Stripe")
|
||||
* 3. The raw id
|
||||
*/
|
||||
public static function resolve_payment_method_title($gateway_id, $order = null)
|
||||
{
|
||||
if ($order instanceof \WC_Order) {
|
||||
$title = $order->get_payment_method_title();
|
||||
if (!empty($title)) {
|
||||
return $title;
|
||||
}
|
||||
}
|
||||
if ($gateway_id !== '' && function_exists('WC') && WC()->payment_gateways()) {
|
||||
$gateways = WC()->payment_gateways()->payment_gateways();
|
||||
if (isset($gateways[$gateway_id]) && method_exists($gateways[$gateway_id], 'get_title')) {
|
||||
$title = $gateways[$gateway_id]->get_title();
|
||||
if (!empty($title)) {
|
||||
return $title;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $gateway_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse [card] tags and convert to HTML
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user