Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ras-acc): add donation cancellation email template #3056

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions assets/wizards/readerRevenue/views/emails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const Emails = () => {
<>
{ emails.map( email => {
const isActive = email.status === 'publish';
const notification =
email.type === 'receipt'
? __( 'This email is not active. The default receipt will be used.', 'newspack-plugin' )
: __( 'This email is not active.', 'newspack-plugin' );
return (
<ActionCard
key={ email.post_id }
Expand Down Expand Up @@ -115,10 +119,7 @@ const Emails = () => {
{ ...( isActive
? {}
: {
notification: __(
'This email is not active. The default receipt will be used.',
'newspack'
),
notification,
notificationLevel: 'info',
} ) }
>
Expand Down
2 changes: 1 addition & 1 deletion includes/emails/class-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ public static function send_email( $config_name, $to, $placeholders = [] ) {
}
}


$switched_locale = \switch_to_locale( \get_user_locale( \wp_get_current_user() ) );

if ( 'string' === gettype( $config_name ) ) {
Expand Down Expand Up @@ -396,6 +395,7 @@ private static function serialize_email( $type = null, $post_id = 0 ) {
$edit_link = str_replace( site_url(), '', $post_link );
}
$serialized_email = [
'type' => $type,
'label' => $email_config['label'],
'description' => $email_config['description'],
'post_id' => $post_id,
Expand Down
118 changes: 73 additions & 45 deletions includes/reader-revenue/class-reader-revenue-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/
class Reader_Revenue_Emails {
const EMAIL_TYPES = [
'RECEIPT' => 'receipt',
'CANCELLATION' => 'cancellation',
'RECEIPT' => 'receipt',
];

/**
Expand Down Expand Up @@ -53,60 +54,87 @@ public static function get_from_email() {
* @param array $configs Email types.
*/
public static function add_email_configs( $configs ) {
$configs[ self::EMAIL_TYPES['RECEIPT'] ] = [
$available_placeholders = [
[
'label' => __( 'the customer billing name', 'newspack-plugin' ),
'template' => '*BILLING_NAME*',
],
[
'label' => __( 'the customer billing first name', 'newspack-plugin' ),
'template' => '*BILLING_FIRST_NAME*',
],
[
'label' => __( 'the customer billing last name', 'newspack-plugin' ),
'template' => '*BILLING_LAST_NAME*',
],
[
'label' => __( 'the billing frequency (one-time, monthly or annual)', 'newspack-plugin' ),
'template' => '*BILLING_FREQUENCY*',
],
[
'label' => __( 'the product name', 'newspack-plugin' ),
'template' => '*PRODUCT_NAME*',
],
[
'label' => __(
'the contact email to your site (same as the "From" email address)',
'newspack-plugin'
),
'template' => '*CONTACT_EMAIL*',
],
];
$configs[ self::EMAIL_TYPES['RECEIPT'] ] = [
'name' => self::EMAIL_TYPES['RECEIPT'],
'label' => __( 'Receipt', 'newspack-plugin' ),
'description' => __( "Email sent to the donor after they've donated.", 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-revenue-emails/receipt.php',
'editor_notice' => __( 'This email will be sent to a reader after they contribute to your site.', 'newspack-plugin' ),
'from_email' => self::get_from_email(),
'available_placeholders' => [
[
'label' => __( 'the customer billing name', 'newspack-plugin' ),
'template' => '*BILLING_NAME*',
],
[
'label' => __( 'the customer billing first name', 'newspack-plugin' ),
'template' => '*BILLING_FIRST_NAME*',
],
[
'label' => __( 'the customer billing last name', 'newspack-plugin' ),
'template' => '*BILLING_LAST_NAME*',
],
'available_placeholders' => array_merge(
$available_placeholders,
[
'label' => __( 'the billing frequency (one-time, monthly or annual)', 'newspack-plugin' ),
'template' => '*BILLING_FREQUENCY*',
],
[
'label' => __( 'the product name', 'newspack-plugin' ),
'template' => '*PRODUCT_NAME*',
],
[
'label' => __( 'the payment amount', 'newspack-plugin' ),
'template' => '*AMOUNT*',
],
[
'label' => __( 'payment date', 'newspack-plugin' ),
'template' => '*DATE*',
],
[
'label' => __( 'payment method (last four digits of the card used)', 'newspack-plugin' ),
'template' => '*PAYMENT_METHOD*',
],
[
'label' => __(
'the contact email to your site (same as the "From" email address)',
'newspack-plugin'
),
'template' => '*CONTACT_EMAIL*',
],
[
'label' => __( 'automatically-generated receipt link', 'newspack-plugin' ),
'template' => '*RECEIPT_URL*',
],
[
'label' => __( 'the payment amount', 'newspack-plugin' ),
'template' => '*AMOUNT*',
],
[
'label' => __( 'payment date', 'newspack-plugin' ),
'template' => '*DATE*',
],
[
'label' => __( 'payment method (last four digits of the card used)', 'newspack-plugin' ),
'template' => '*PAYMENT_METHOD*',
],
]
),
];
$configs[ self::EMAIL_TYPES['CANCELLATION'] ] = [
'name' => self::EMAIL_TYPES['CANCELLATION'],
'label' => __( 'Cancellation', 'newspack-plugin' ),
'description' => __( "Email sent to the donor after they've cancelled a recurring donation.", 'newspack-plugin' ),
'template' => dirname( NEWSPACK_PLUGIN_FILE ) . '/includes/templates/reader-revenue-emails/cancellation.php',
'editor_notice' => __( 'This email will be sent to a reader after they cancel a recurring donation.', 'newspack-plugin' ),
'from_email' => self::get_from_email(),
'available_placeholders' => array_merge(
$available_placeholders,
[
'label' => __( 'automatically-generated receipt link', 'newspack-plugin' ),
'template' => '*RECEIPT_URL*',
],
],
[
'label' => __( 'the recurring donation management url', 'newspack-plugin' ),
'template' => '*CANCELLATION_URL*',
],
[
'label' => __( 'the recurring donation end date', 'newspack-plugin' ),
'template' => '*END_DATE*',
],
]
),
];


return $configs;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static function init() {
\add_filter( 'default_option_woocommerce_subscriptions_allow_switching_nyp_price', [ __CLASS__, 'force_allow_subscription_switching' ], 10, 2 );
\add_filter( 'default_option_woocommerce_subscriptions_enable_retry', [ __CLASS__, 'force_allow_failed_payment_retry' ] );
\add_filter( 'woocommerce_email_enabled_customer_completed_order', [ __CLASS__, 'send_customizable_receipt_email' ], 10, 3 );
\add_filter( 'woocommerce_email_enabled_cancelled_subscription', [ __CLASS__, 'send_customizable_cancellation_email' ], 10, 3 );
\add_action( 'woocommerce_order_status_completed', [ __CLASS__, 'maybe_update_reader_display_name' ], 10, 2 );
\add_action( 'option_woocommerce_feature_order_attribution_enabled', [ __CLASS__, 'force_disable_order_attribution' ] );
\add_filter( 'woocommerce_related_products', [ __CLASS__, 'disable_related_products' ] );
Expand Down Expand Up @@ -522,6 +523,95 @@ public static function send_customizable_receipt_email( $enable, $order, $class
return false;
}

/**
* Send the customizable cancellation email in addition to WooCommerce Subscription's default.
* We still want to allow WCS to send its cancellation email since this targets the store admin.
*
* @param bool $enable Whether to send the cancellation email.
* @param WC_Subscription $subscription The order object for the cancellation email.
* @param WC_Email $class Instance of the WC_Email class.
*
* @return bool
*/
public static function send_customizable_cancellation_email( $enable, $subscription, $class ) {
// If we don't have a valid subscription, or the customizable email isn't enabled, bail.
if ( ! is_a( $subscription, 'WC_Subscription' ) || ! Emails::can_send_email( Reader_Revenue_Emails::EMAIL_TYPES['CANCELLATION'] ) ) {
return $enable;
}

$frequencies = [
'month' => __( 'Monthly', 'newspack-plugin' ),
'year' => __( 'Yearly', 'newspack-plugin' ),
];
$product_map = [];
foreach ( $frequencies as $frequency => $label ) {
$product_id = Donations::get_donation_product( $frequency );
if ( $product_id ) {
$product_map[ $product_id ] = $label;
}
}

$items = $subscription->get_items();
$item = array_shift( $items );
$is_donation = Donations::is_donation_product( $item->get_product_id() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When deleting and account which has active subscriptions, this method will throw with

PHP Fatal error:  Uncaught Error: Call to a member function get_product_id() on null

because the $item is null at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! I'll get a fix up for this today 👍


// Replace content placeholders.
$placeholders = [
[
'template' => '*BILLING_NAME*',
'value' => trim( $subscription->get_billing_first_name() . ' ' . $subscription->get_billing_last_name() ),
],
[
'template' => '*BILLING_FIRST_NAME*',
'value' => $subscription->get_billing_first_name(),
],
[
'template' => '*BILLING_LAST_NAME*',
'value' => $subscription->get_billing_last_name(),
],
[
'template' => '*BILLING_FREQUENCY*',
'value' => $product_map[ $item->get_product_id() ] ?? __( 'One-time', 'newspack-plugin' ),
],
[
'template' => '*PRODUCT_NAME*',
'value' => $item->get_name(),
],
[
'template' => '*END_DATE*',
'value' => wcs_format_datetime( wcs_get_datetime_from( $subscription->get_date( 'end' ) ) ),
],
[
'template' => '*BUTTON_TEXT*',
'value' => $is_donation ? __( 'Restart Donation', 'newspack-plugin' ) : __( 'Restart Subscription', 'newspack-plugin' ),
],
[
'template' => '*CANCELLATION_DATE*',
'value' => wcs_format_datetime( wcs_get_datetime_from( $subscription->get_date( 'cancelled' ) ) ),
],
[
'template' => '*CANCELLATION_TITLE*',
'value' => $is_donation ? __( 'Donation Cancelled', 'newspack-plugin' ) : __( 'Subscription Cancelled', 'newspack-plugin' ),
],
[
'template' => '*CANCELLATION_TYPE*',
'value' => $is_donation ? __( 'recurring donation', 'newspack-plugin' ) : __( 'subscription', 'newspack-plugin' ),
],
[
'template' => '*SUBSCRIPTION_URL*',
'value' => $subscription->get_view_order_url(),
],
];

$sent = Emails::send_email(
Reader_Revenue_Emails::EMAIL_TYPES['CANCELLATION'],
$subscription->get_billing_email(),
$placeholders
);

return $enable;
}

/**
* If the reader completes an order, check if they have a generic display name.
* If they do and they also have a billing first and/or last name, we can upgrade
Expand Down
Loading