@@ -415,8 +415,11 @@ public function getSecondChanceByToken($token)
415415 // Recreate quote with Second Chance suffix
416416 $ order = $ this ->orderFactory ->create ()->loadByIncrementId ($ secondChance ->getOrderId ());
417417 if ($ order ->getId ()) {
418- // Find available increment ID with suffix (e.g., orderId-1, orderId-2, etc.)
419- $ newOrderId = $ this ->setAvailableIncrementId ($ secondChance ->getOrderId (), $ order );
418+ $ newOrderId = $ secondChance ->getLastOrderId ();
419+
420+ if (!$ newOrderId ) {
421+ $ newOrderId = $ this ->setAvailableIncrementId ($ secondChance ->getOrderId (), $ order );
422+ }
420423
421424 // Recreate the quote
422425 $ quote = $ this ->quoteRecreate ->duplicate ($ order );
@@ -428,7 +431,8 @@ public function getSecondChanceByToken($token)
428431 $ this ->logging ->addDebug ('Second Chance: Order ID suffix applied to new quote ' , [
429432 'quote_id ' => $ quote ->getId (),
430433 'reserved_order_id ' => $ newOrderId ,
431- 'original_order_id ' => $ secondChance ->getOrderId ()
434+ 'original_order_id ' => $ secondChance ->getOrderId (),
435+ 'was_pre_calculated ' => !empty ($ secondChance ->getLastOrderId ())
432436 ]);
433437 }
434438 }
@@ -519,6 +523,16 @@ public function getSecondChanceCollection($step, $store)
519523 continue ;
520524 }
521525
526+ // Calculate and store the expected order ID before sending email
527+ // For step 1: Always use -1 suffix (first reminder)
528+ // For step 2: Always use -2 suffix (second reminder)
529+ // This makes it easy for customer support to identify which email led to the order
530+ $ suffix = ($ step == 1 ) ? '-1 ' : '-2 ' ;
531+ $ expectedOrderId = $ item ->getOrderId () . $ suffix ;
532+
533+ // Store it so when customer clicks, we use the same ID
534+ $ item ->setLastOrderId ($ expectedOrderId );
535+
522536 // Send email
523537 $ this ->sendMail ($ order , $ item , $ step );
524538
@@ -566,8 +580,16 @@ public function sendMail($order, $secondChance, $step)
566580 '_scope_to_url ' => true
567581 ]);
568582
583+ // Get the expected order ID (should be pre-calculated and stored in last_order_id)
584+ $ expectedOrderId = $ secondChance ->getLastOrderId ();
585+ if (!$ expectedOrderId ) {
586+ $ expectedOrderId = $ this ->setAvailableIncrementId ($ secondChance ->getOrderId (), $ order );
587+ }
588+
569589 $ this ->logging ->addDebug ('Second Chance email URL generated ' , [
570590 'order_id ' => $ order ->getIncrementId (),
591+ 'expected_order_id ' => $ expectedOrderId ,
592+ 'step ' => $ step ,
571593 'store_id ' => $ store ->getId (),
572594 'store_code ' => $ store ->getCode (),
573595 'locale ' => $ store ->getConfig ('general/locale/code ' ),
@@ -592,21 +614,34 @@ public function sendMail($order, $secondChance, $step)
592614 'email ' => $ senderEmail ,
593615 ];
594616
595- // Prepare template variables
617+ // Prepare template variables (matching original SecondChance module)
596618 try {
597619 $ paymentHtml = $ this ->getPaymentHtml ($ order );
598620 $ billingAddress = $ this ->getFormattedBillingAddress ($ order );
599621 $ shippingAddress = $ this ->getFormattedShippingAddress ($ order );
600622
601623 $ templateVars = [
602624 'order ' => $ order ,
625+ 'order_id ' => $ order ->getId (),
626+ 'expected_order_id ' => $ expectedOrderId ?: $ order ->getIncrementId (),
627+ 'billing ' => $ order ->getBillingAddress (),
628+ 'payment_html ' => $ paymentHtml ,
629+ 'formattedBillingAddress ' => $ billingAddress ,
630+ 'formattedShippingAddress ' => $ shippingAddress ,
631+ 'billing_address ' => $ billingAddress , // Keep for backward compatibility
632+ 'shipping_address ' => $ shippingAddress , // Keep for backward compatibility
603633 'checkout_url ' => $ checkoutUrl ,
604634 'store ' => $ store ,
635+ 'created_at_formatted ' => $ order ->getCreatedAtFormatted (2 ),
636+ 'secondChanceToken ' => $ secondChance ->getToken (),
605637 'customer_name ' => $ order ->getCustomerName (),
606638 'customer_email ' => $ order ->getCustomerEmail (),
607- 'payment_html ' => $ paymentHtml ,
608- 'billing_address ' => $ billingAddress ,
609- 'shipping_address ' => $ shippingAddress ,
639+ 'order_data ' => [
640+ 'customer_name ' => $ order ->getCustomerName (),
641+ 'is_not_virtual ' => $ order ->getIsNotVirtual (),
642+ 'email_customer_note ' => $ order ->getEmailCustomerNote (),
643+ 'frontend_status_label ' => $ order ->getFrontendStatusLabel ()
644+ ]
610645 ];
611646 } catch (Exception $ e ) {
612647 $ this ->logging ->addError ('Error preparing template variables: ' . $ e ->getMessage ());
0 commit comments