在Woocommerce电子邮件下载中将产品名称替换为自定义元

时间:2018-03-30 12:43:02

标签: php wordpress woocommerce orders email-notifications

由于插件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*** . '&nbsp;'; ?>

我现在想在WooCommerce的email-downloads.php中实现相同的解决方案来修复我的Woocommerce电子邮件,但我似乎无法获得&#39; get_post_meta&#39;工作在一个&#39; foreach&#39;在这个文件中。任何人都可以分享正确的格式或在下面的代码中插入这个格式吗?

我已将输出部分至少隔离到

<?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>

2 个答案:

答案 0 :(得分:0)

有两种可能的方式:

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 ); ?>

答案 1 :(得分:0)

我使用了@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;
}