在 Woocommerce 电子邮件下载中用自定义元替换产品名称
Replace product name with custom meta in Woocommerce email downloads
由于插件 Essential Grid 只是将产品标题传递给它的灯箱标题,为了显示足够的产品描述,我被迫在我的 Woocommerce 商店中使用更长的产品标题。这些较长的标题依次输出到发送给客户的 WooCommerce 订单电子邮件中,包括 HTML 标签 - 如下所示。
Emails displaying long titles including HTML code
我已经在我的迷你购物车模式中修复了这个行为,方法是调用每个产品的自定义元 simple_title 输出而不是默认标题,如下面我编辑的 mini-cart 中所示。php(*** 表示我唯一的更改)
<?php
do_action( 'woocommerce_before_mini_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
***$meta_title = get_post_meta( $product_id, 'simple_title', true)***;
...
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . ***$meta_title*** . ' '; ?>
我现在想在 WooCommerce 的 email-downloads.php 中实施相同的解决方案来修复我的 Woocommerce 电子邮件,但我似乎无法让 'get_post_meta' 在 'foreach' 在此文件中。任何人都可以分享正确的格式或在下面的代码中插入的位置吗?
我至少已经隔离了输出部分,到
<?php echo esc_html( $download['product_name'] ); ?>
-- 此输出需要替换为显示产品的 simple_title 自定义元数据。
非常感谢任何想法。
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; margin-bottom: 40px;" border="1">
<thead>
<tr>
<?php foreach ( $columns as $column_id => $column_name ) : ?>
<th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html( $column_name ); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<?php foreach ( $downloads as $download ) : ?>
<tr>
<?php foreach ( $columns as $column_id => $column_name ) : ?>
<td class="td" style="text-align:<?php echo $text_align; ?>;"><?php
if ( has_action( 'woocommerce_email_downloads_column_' . $column_id ) ) {
do_action( 'woocommerce_email_downloads_column_' . $column_id, $download );
} else {
switch ( $column_id ) {
case 'download-product' : ?>
<?php echo esc_html( $download['product_name'] ); ?>
<?php
break;
case 'download-file' : ?>
<a href="<?php echo esc_url( $download['download_url'] ); ?>" class="woocommerce-MyAccount-downloads-file button alt"><?php echo esc_html( $download['download_name'] ); ?></a>
<?php
break;
case 'download-expires' : ?>
<?php if ( ! empty( $download['access_expires'] ) ) : ?>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $download['access_expires'] ) ); ?>" title="<?php echo esc_attr( strtotime( $download['access_expires'] ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $download['access_expires'] ) ); ?></time>
<?php else : ?>
<?php _e( 'Never', 'woocommerce' ); ?>
<?php endif;
break;
}
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
有两种可能的方法:
1) 使用这个过滤器挂钩:
add_filter( 'woocommerce_order_get_downloadable_items', 'custom_order_get_downloadable_items', 30, 2 );
function custom_order_get_downloadable_items( $downloads, $orders ){
if( is_admin() )
$downloads['product_name'] = get_post_meta( $downloads['product_id'], 'simple_title', true );
return $downloads;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
2) Override directly the template via your theme 并在 emails/email-downloads.php
文件中替换:
<?php echo esc_html( $download['product_name'] ); ?>
作者:
<?php echo get_post_meta( $downloads['product_id'], 'simple_title', true ); ?>
我使用了@loicTheAztec 的解决方案并做了一些修改
add_filter( 'woocommerce_order_get_downloadable_items', 'custom_order_get_downloadable_items', 30, 2 );
function custom_order_get_downloadable_items( $downloads, $orders ){
$new_download = array();
foreach($downloads as $dl){
$new_name = sprintf( __('New Name %s' , 'my_project') , $dl['product_name'] );
$dl['download_name'] = $new_name;
$new_download[] = $dl;
}
return $new_download;
}
由于插件 Essential Grid 只是将产品标题传递给它的灯箱标题,为了显示足够的产品描述,我被迫在我的 Woocommerce 商店中使用更长的产品标题。这些较长的标题依次输出到发送给客户的 WooCommerce 订单电子邮件中,包括 HTML 标签 - 如下所示。
Emails displaying long titles including HTML code
我已经在我的迷你购物车模式中修复了这个行为,方法是调用每个产品的自定义元 simple_title 输出而不是默认标题,如下面我编辑的 mini-cart 中所示。php(*** 表示我唯一的更改)
<?php
do_action( 'woocommerce_before_mini_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );
***$meta_title = get_post_meta( $product_id, 'simple_title', true)***;
...
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . ***$meta_title*** . ' '; ?>
我现在想在 WooCommerce 的 email-downloads.php 中实施相同的解决方案来修复我的 Woocommerce 电子邮件,但我似乎无法让 'get_post_meta' 在 'foreach' 在此文件中。任何人都可以分享正确的格式或在下面的代码中插入的位置吗?
我至少已经隔离了输出部分,到
<?php echo esc_html( $download['product_name'] ); ?>
-- 此输出需要替换为显示产品的 simple_title 自定义元数据。
非常感谢任何想法。
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; margin-bottom: 40px;" border="1">
<thead>
<tr>
<?php foreach ( $columns as $column_id => $column_name ) : ?>
<th class="td" scope="col" style="text-align:<?php echo $text_align; ?>;"><?php echo esc_html( $column_name ); ?></th>
<?php endforeach; ?>
</tr>
</thead>
<?php foreach ( $downloads as $download ) : ?>
<tr>
<?php foreach ( $columns as $column_id => $column_name ) : ?>
<td class="td" style="text-align:<?php echo $text_align; ?>;"><?php
if ( has_action( 'woocommerce_email_downloads_column_' . $column_id ) ) {
do_action( 'woocommerce_email_downloads_column_' . $column_id, $download );
} else {
switch ( $column_id ) {
case 'download-product' : ?>
<?php echo esc_html( $download['product_name'] ); ?>
<?php
break;
case 'download-file' : ?>
<a href="<?php echo esc_url( $download['download_url'] ); ?>" class="woocommerce-MyAccount-downloads-file button alt"><?php echo esc_html( $download['download_name'] ); ?></a>
<?php
break;
case 'download-expires' : ?>
<?php if ( ! empty( $download['access_expires'] ) ) : ?>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $download['access_expires'] ) ); ?>" title="<?php echo esc_attr( strtotime( $download['access_expires'] ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $download['access_expires'] ) ); ?></time>
<?php else : ?>
<?php _e( 'Never', 'woocommerce' ); ?>
<?php endif;
break;
}
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
有两种可能的方法:
1) 使用这个过滤器挂钩:
add_filter( 'woocommerce_order_get_downloadable_items', 'custom_order_get_downloadable_items', 30, 2 );
function custom_order_get_downloadable_items( $downloads, $orders ){
if( is_admin() )
$downloads['product_name'] = get_post_meta( $downloads['product_id'], 'simple_title', true );
return $downloads;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
2) Override directly the template via your theme 并在 emails/email-downloads.php
文件中替换:
<?php echo esc_html( $download['product_name'] ); ?>
作者:
<?php echo get_post_meta( $downloads['product_id'], 'simple_title', true ); ?>
我使用了@loicTheAztec 的解决方案并做了一些修改
add_filter( 'woocommerce_order_get_downloadable_items', 'custom_order_get_downloadable_items', 30, 2 );
function custom_order_get_downloadable_items( $downloads, $orders ){
$new_download = array();
foreach($downloads as $dl){
$new_name = sprintf( __('New Name %s' , 'my_project') , $dl['product_name'] );
$dl['download_name'] = $new_name;
$new_download[] = $dl;
}
return $new_download;
}