如何在WooCommerce邮件模板中获取订单收件人并按条件隐藏价格行
First, let’s make sure you don’t lose your customizations when WooCommerce updates: copy the email-order-details.php file from wp-content/plugins/woocommerce/templates/emails/ to your theme’s woocommerce/emails/ directory (create the subfolders if they don’t exist). This way, your changes stay intact through plugin updates.
Step 1: Grab the Email Recipient in the Template
At the top of your copied email-order-details.php file (or right before the order items loop), add this line to fetch the current email recipient:
// Get the recipient of this email $email_recipient = $email->recipient;
Step 2: Hide the Price Column in the Table Header
Look for the table header row that includes the "Price" (or "Subtotal") column. Wrap that <th> element in a conditional check to only show it if the recipient is test@test.com:
<?php if ( $email_recipient === 'test@test.com' ) : ?> <th class="td" scope="col" style="text-align:left; border: 1px solid #eee; border-bottom-width: 4px; padding: 12px; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th> <?php endif; ?>
Step 3: Hide Price Rows for Individual Products
Next, find the section inside the foreach ( $order->get_items() as $item_id => $item ) loop where each product’s price is displayed. Wrap the price <td> element in the same conditional check:
<?php if ( $email_recipient === 'test@test.com' ) : ?> <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" colspan="2"> <?php echo $order->get_formatted_line_subtotal( $item ); ?> </td> <?php endif; ?>
Quick Notes:
- If your template uses a separate column for quantity and subtotal, adjust the conditional wrap to target the correct price-related elements.
- For case-insensitive email matching (e.g., if recipients might use
Test@Test.com), usestrtolower( $email_recipient ) === strtolower( 'test@test.com' )instead. - This method leverages the
$emailobject that’s already available in WooCommerce email templates, so you don’t need extra hooks to get the recipient.
内容的提问来源于stack exchange,提问作者Benny

