Commit ba4d2a7e70 for woocommerce

commit ba4d2a7e703532b9871402f400d5bcfef6e2f757
Author: Yuliyan Slavchev <yuliyan.slavchev@gmail.com>
Date:   Wed May 21 17:31:15 2025 +0300

    Extend personalization tags for order (#58117)

    * Extend personalization tags for order and customer

    * Add changelog entry

diff --git a/plugins/woocommerce/changelog/wooplug-4331-extend-personalization-tags-for-order-and-customer b/plugins/woocommerce/changelog/wooplug-4331-extend-personalization-tags-for-order-and-customer
new file mode 100644
index 0000000000..0241b34566
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooplug-4331-extend-personalization-tags-for-order-and-customer
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Add personalization tags for Order Discount, Shipping Cost, Shipping Method, Shipping Address, Billing Address, Transaction Id, Order View URL, Order Admin URL, Order Custom Field.
diff --git a/plugins/woocommerce/src/Internal/Admin/EmailPreview/EmailPreview.php b/plugins/woocommerce/src/Internal/Admin/EmailPreview/EmailPreview.php
index 304d2ba34e..70d396ac3c 100644
--- a/plugins/woocommerce/src/Internal/Admin/EmailPreview/EmailPreview.php
+++ b/plugins/woocommerce/src/Internal/Admin/EmailPreview/EmailPreview.php
@@ -15,6 +15,7 @@ use WC_Order;
 use WC_Product;
 use WC_Product_Variation;
 use WP_User;
+use WC_Order_Item_Shipping;

 defined( 'ABSPATH' ) || exit;

@@ -343,7 +344,19 @@ class EmailPreview {
 		$order->set_shipping_total( 5 );
 		$order->set_total( 65 );
 		$order->set_payment_method_title( __( 'Direct bank transfer', 'woocommerce' ) );
-		$order->set_customer_note( __( "This is a customer note. Customers can add a note to their order on checkout.\n\nIt can be multiple lines. If there’s no note, this section is hidden.", 'woocommerce' ) );
+		$order->set_transaction_id( '999999999' );
+		$order->set_customer_note( __( "This is a customer note. Customers can add a note to their order on checkout.\n\nIt can be multiple lines. If there's no note, this section is hidden.", 'woocommerce' ) );
+
+		// Add shipping method.
+		$shipping_item = new WC_Order_Item_Shipping();
+		$shipping_item->set_props(
+			array(
+				'method_title' => __( 'Flat rate', 'woocommerce' ),
+				'method_id'    => 'flat_rate',
+				'total'        => '5.00',
+			)
+		);
+		$order->add_item( $shipping_item );

 		$address = $this->get_dummy_address();
 		$order->set_billing_address( $address );
@@ -477,8 +490,6 @@ class EmailPreview {
 		add_filter( 'woocommerce_order_item_product', array( $this, 'get_dummy_product_when_not_set' ), 10, 1 );
 		// Enable email preview mode - this way transient values are fetched for live preview.
 		add_filter( 'woocommerce_is_email_preview', array( $this, 'enable_preview_mode' ) );
-		// Get shipping method without needing to save it in the order.
-		add_filter( 'woocommerce_order_shipping_method', array( $this, 'get_shipping_method' ) );
 		// Use placeholder image included in WooCommerce files.
 		add_filter( 'woocommerce_order_item_thumbnail', array( $this, 'get_placeholder_image' ) );
 	}
@@ -490,19 +501,9 @@ class EmailPreview {
 		remove_filter( 'woocommerce_order_needs_shipping_address', array( $this, 'enable_shipping_address' ) );
 		remove_filter( 'woocommerce_order_item_product', array( $this, 'get_dummy_product_when_not_set' ), 10 );
 		remove_filter( 'woocommerce_is_email_preview', array( $this, 'enable_preview_mode' ) );
-		remove_filter( 'woocommerce_order_shipping_method', array( $this, 'get_shipping_method' ) );
 		remove_filter( 'woocommerce_order_item_thumbnail', array( $this, 'get_placeholder_image' ) );
 	}

-	/**
-	 * Get the shipping method for the preview email.
-	 *
-	 * @return string
-	 */
-	public function get_shipping_method() {
-		return __( 'Flat rate', 'woocommerce' );
-	}
-
 	/**
 	 * Enable shipping address in the preview email. Not using __return_true so
 	 * we don't accidentally remove the same filter used by other plugin or theme.
diff --git a/plugins/woocommerce/src/Internal/EmailEditor/PersonalizationTags/OrderTagsProvider.php b/plugins/woocommerce/src/Internal/EmailEditor/PersonalizationTags/OrderTagsProvider.php
index e77d473090..9f3eedf748 100644
--- a/plugins/woocommerce/src/Internal/EmailEditor/PersonalizationTags/OrderTagsProvider.php
+++ b/plugins/woocommerce/src/Internal/EmailEditor/PersonalizationTags/OrderTagsProvider.php
@@ -102,6 +102,34 @@ class OrderTagsProvider extends AbstractTagProvider {
 			)
 		);

+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Discount', 'woocommerce' ),
+				'woocommerce/order-discount',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return wc_price( $context['order']->get_discount_total(), array( 'currency' => $context['order']->get_currency() ) );
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Shipping', 'woocommerce' ),
+				'woocommerce/order-shipping',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return wc_price( $context['order']->get_shipping_total(), array( 'currency' => $context['order']->get_currency() ) );
+				},
+			)
+		);
+
 		$registry->register(
 			new Personalization_Tag(
 				__( 'Order Total', 'woocommerce' ),
@@ -143,5 +171,107 @@ class OrderTagsProvider extends AbstractTagProvider {
 				},
 			)
 		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Transaction ID', 'woocommerce' ),
+				'woocommerce/order-transaction-id',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return $context['order']->get_transaction_id() ?? '';
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Shipping Method', 'woocommerce' ),
+				'woocommerce/order-shipping-method',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return $context['order']->get_shipping_method() ?? '';
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Shipping Address', 'woocommerce' ),
+				'woocommerce/order-shipping-address',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return $context['order']->get_formatted_shipping_address() ?? '';
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Billing Address', 'woocommerce' ),
+				'woocommerce/order-billing-address',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return $context['order']->get_formatted_billing_address() ?? '';
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order View URL', 'woocommerce' ),
+				'woocommerce/order-view-url',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return $context['order']->get_view_order_url() ?? '';
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Admin URL', 'woocommerce' ),
+				'woocommerce/order-admin-url',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context ): string {
+					if ( ! isset( $context['order'] ) ) {
+						return '';
+					}
+					return $context['order']->get_edit_order_url() ?? '';
+				},
+			)
+		);
+
+		$registry->register(
+			new Personalization_Tag(
+				__( 'Order Custom Field', 'woocommerce' ),
+				'woocommerce/order-custom-field',
+				__( 'Order', 'woocommerce' ),
+				function ( array $context, array $parameters = array() ): string {
+					if ( ! isset( $context['order'] ) || ! isset( $parameters['key'] ) ) {
+						return '';
+					}
+					$field_key = sanitize_text_field( $parameters['key'] );
+					return $context['order']->get_meta( $field_key ) ?? '';
+				},
+				array(
+					'key' => '',
+				),
+			)
+		);
 	}
 }