Skip to content

Commit

Permalink
fix: match thankyou template when using existing customer email
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoo committed Oct 10, 2023
1 parent 9779a33 commit 48f4857
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
27 changes: 20 additions & 7 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,27 @@ public static function get_checkout_template( $template ) {
/**
* Return URL for modal checkout "thank you" page.
*
* @param string $url The URL to redirect to.
* @param string $url The URL to redirect to.
* @param WC_Order $order The order related to the transaction.
*
* @return string
*/
public static function woocommerce_get_return_url( $url ) {
public static function woocommerce_get_return_url( $url, $order ) {
if ( ! isset( $_REQUEST['modal_checkout'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $url;
}
$args = [
'modal_checkout' => '1',
'email' => isset( $_REQUEST['billing_email'] ) ? rawurlencode( sanitize_email( wp_unslash( $_REQUEST['billing_email'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
];

// Pass order ID for modal checkout templates.
if ( $order && is_a( $order, 'WC_Order' ) ) {
$args['order_id'] = $order->get_id();
}

return add_query_arg(
[
'modal_checkout' => '1',
'email' => isset( $_REQUEST['billing_email'] ) ? rawurlencode( sanitize_email( wp_unslash( $_REQUEST['billing_email'] ) ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Recommended
],
$args,
$url
);
}
Expand All @@ -337,13 +345,18 @@ public static function woocommerce_get_return_url( $url ) {
* @return string Template file.
*/
public static function wc_get_template( $located, $template_name ) {
if ( ! isset( $_REQUEST['modal_checkout'] ) || ! boolval( $_REQUEST['modal_checkout'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return $located;
}

$custom_templates = [
'checkout/form-checkout.php' => 'src/modal-checkout/templates/checkout-form.php',
'checkout/form-billing.php' => 'src/modal-checkout/templates/billing-form.php',
'global/form-login.php' => 'src/modal-checkout/templates/form-login.php',
];

foreach ( $custom_templates as $original_template => $custom_template ) {
if ( $template_name === $original_template && isset( $_REQUEST['modal_checkout'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $template_name === $original_template ) {
$located = NEWSPACK_BLOCKS__PLUGIN_DIR . $custom_template;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/modal-checkout/checkout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
transform: scale( 0 );
width: 40px;
}
+ .woocommerce-info {
display: none; // Hide the "Please log in to view this order" message on the thank you page.
}
}
.woocommerce-order-overview {
color: colors.$color__text-light;
Expand Down
67 changes: 67 additions & 0 deletions src/modal-checkout/templates/form-login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Login form. WooCommerce by default doesn't allow order details to be shown
* if the order is completed using a customer email address that doesn't match
* the currently logged in user.
*
* Details: https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/shortcodes/class-wc-shortcode-checkout.php#L302-L321
*
* For Newspack sites, we don't want to emphasize user account flows outside of
* RAS. This custom login template replaces the login form that appears on the
* order-received.php template with an order details summary so the experience
* matches whether or not the email address used is already associated with an
* existing customer account.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 8.1.0
*
* @var WC_Order $order
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

$order = isset( $_GET['order_id'] ) ? \wc_get_order( \absint( \wp_unslash( $_GET['order_id'] ) ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.WP.GlobalVariablesOverride.Prohibited
?>

<div class="woocommerce-order">
<?php if ( $order ) : ?>

<h4><?php esc_html_e( 'Summary', 'newspack-blocks' ); ?></h4>

<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">

<li class="woocommerce-order-overview__date date">
<?php esc_html_e( 'Date:', 'newspack-blocks' ); ?>
<strong><?php echo wc_format_datetime( $order->get_date_created() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>

<?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
<li class="woocommerce-order-overview__email email">
<?php esc_html_e( 'Email:', 'newspack-blocks' ); ?>
<strong><?php echo $order->get_billing_email(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php endif; ?>

<li class="woocommerce-order-overview__total total">
<?php esc_html_e( 'Total:', 'newspack-blocks' ); ?>
<strong><?php echo $order->get_formatted_order_total(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>

<?php if ( $order->get_payment_method_title() ) : ?>
<li class="woocommerce-order-overview__payment-method method">
<?php esc_html_e( 'Payment method:', 'newspack-blocks' ); ?>
<strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
</li>
<?php endif; ?>

<li class="woocommerce-order-overview__order order">
<?php esc_html_e( 'Transaction:', 'newspack-blocks' ); ?>
<strong><?php echo $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>

</ul>
<?php endif; ?>
</div>

0 comments on commit 48f4857

Please sign in to comment.