我檢查了平臺上的許多執行緒。但是沒有執行緒解釋如何將條件$email>id
引數直接用于模板。
這就是我所擁有的email-header.php
:
<div style="width:600px;" id="template_header_image">
<?php
if ( $img = get_option( 'woocommerce_email_header_image' ) ) {
echo '<p style="margin-top:0;"><img width="80%" height="auto" src="' . esc_url( $img ) . '" alt="如果默認情況下不可用,如何將“$email”傳遞給 WooCommerce 電子郵件模板檔案" /></p>';
}
if( $email->id == 'customer_processing_order' ) {
echo '<img alt="如果默認情況下不可用,如何將“$email”傳遞給 WooCommerce 電子郵件模板檔案" src="https/example.com/image.png" />';
}
?>
</div>
src 就是一個例子。if( $email->id == 'customer_processing_order' )
不作業。
似乎該引數$email
沒有被拾取。我試著用global $email
;來稱呼它 但這也行不通。
有什么建議嗎?
uj5u.com熱心網友回復:
在 /includes/class-wc-emails.php 我們看到只有$email_heading
通過wc_get_template()
/**
* Get the email header.
*
* @param mixed $email_heading Heading for the email.
*/
public function email_header( $email_heading ) {
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );
}
因此,要通過$email->id
我們必須使用一種解決方法,首先我們將使變數全域可用。
1)這可以通過不同的鉤子來完成,但woocommerce_email_header
鉤子在這種特定情況下似乎是最合適的:
// Header - set global variable
function action_woocommerce_email_header( $email_heading, $email ) {
$GLOBALS['email_data'] = array(
'email_id' => $email->id, // The email ID (to target specific email notification)
'is_email' => true // When it concerns a WooCommerce email notification
);
}
add_action( 'woocommerce_email_header', 'action_woocommerce_email_header', 10, 2 );
代碼位于活動子主題(或活動主題)的 functions.php 檔案中。
2)然后在所需的模板檔案中,您可以使用:
// Getting the custom 'email_data' global variable
$ref_name_globals_var = $GLOBALS;
// Isset & NOT empty
if ( isset ( $ref_name_globals_var ) && ! empty( $ref_name_globals_var ) ) {
// Isset
$email_data = isset( $ref_name_globals_var['email_data'] ) ? $ref_name_globals_var['email_data'] : '';
// NOT empty
if ( ! empty( $email_data ) ) {
// Targeting specific email notifications - multiple statuses can be added, separated by a comma
if ( in_array( $email_data['email_id'], array( 'new_order', 'customer_processing_order' ) ) ) {
// Desired output
echo '<img alt="如果默認情況下不可用,如何將“$email”傳遞給 WooCommerce 電子郵件模板檔案" src="https/example.com/image.png" />';
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/470893.html