如何用printf函数生成含WooCommerce变量的HTML链接?
Looks like you’ve got the right core idea using printf to build your order link, but the issue is you’re passing the static string 'woocommerce' to the first %s placeholder instead of the actual order number. That’s why your link is landing on /view-order/woocommerce/ instead of the correct order-specific page.
Here’s the corrected code that replaces both %s placeholders with the dynamic order number:
<?php printf( '<a href="https://www.baseclothing.nl/mijn-account/view-order/%s/">https://www.baseclothing.nl/mijn-account/view-order/%s/</a>', esc_html( $order->get_order_number() ), esc_html( $order->get_order_number() ) ); ?>
Key Fixes Explained:
- Removed the static
'woocommerce'string and replaced it withesc_html( $order->get_order_number() )for the first placeholder. Now both the link’shrefattribute and the visible text will use the actual order number from your$orderobject. - We keep using
esc_html()to sanitize the order number—this is a critical best practice to prevent potential XSS vulnerabilities in your output.
Just double-check that the $order variable is a valid WooCommerce Order object when this code runs (for example, that it’s being used in a context where the order is properly initialized, like on an order confirmation page or customer email template).
内容的提问来源于stack exchange,提问作者Rick van Baalen

