【问题标题】:Display the product image in Woocommerce email notifications在 Woocommerce 电子邮件通知中显示产品图片
【发布时间】:2021-01-04 05:49:14
【问题描述】:

在 Woocommerce 上,我已将电子邮件订单详细信息 php 模板文件中的 $show_image 变量更改为 true,但我仍然无法在电子邮件通知中显示图像:

<div style="margin-bottom: 40px;">
<table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
    <thead>
        <tr>
            <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>
            <th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php
        echo wc_get_email_order_items( $order, array( // WPCS: XSS ok.
            'show_sku'      => $sent_to_admin,
            'show_image'    => true,
            'image_size'    => array( 100, 100 ),
            'plain_text'    => $plain_text,
            'sent_to_admin' => $sent_to_admin,
        ) );
        ?>
    </tbody>
    <tfoot>
        <?php
        $totals = $order->get_order_item_totals();

        if ( $totals ) {
            $i = 0;
            foreach ( $totals as $total ) {
                $i++;
                ?>
                <tr>
                    <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['label'] ); ?></th>
                    <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo wp_kses_post( $total['value'] ); ?></td>
                </tr>
                <?php
            }
        }
        if ( $order->get_customer_note() ) {
            ?>
            <tr>
                <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Personal Message:', 'woocommerce' ); ?></th>
                <td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php echo wp_kses_post( wptexturize( $order->get_customer_note() ) ); ?></td>
            </tr>
            <?php
        }
        ?>
    </tfoot>
</table>

我还需要为产品图片添加链接。一旦用户点击图片,它应该重定向到特定页面。

将消息从 false 更改为 true 仍然图像未显示在站点中。

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    要在电子邮件通知中显示图像,请将您的更改恢复为原始模板并改用:

    add_filter( 'woocommerce_email_order_items_args', 'custom_email_order_items_args', 10, 1 );
    function custom_email_order_items_args( $args ) {
        $args['show_image'] = true;
    
        return $args;
    }
    

    要将产品链接添加到图片商品名称 (可选),您将使用:

    add_filter( 'woocommerce_order_item_thumbnail', 'add_email_order_item_permalink', 10, 2 ); // Product image
    add_filter( 'woocommerce_order_item_name', 'add_email_order_item_permalink', 10, 2 ); // Product name
    function add_email_order_item_permalink( $output_html, $item, $bool = false ) {
        // Only email notifications
        if( is_wc_endpoint_url() )
            return $output_html;
    
        $product   = $item->get_product();
    
        return '<a href="'.esc_url( $product->get_permalink() ).'">' . $output_html . '</a>';
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    缩略图大小变化:

    您还可以在此挂钩中操作缩略图大小,默认情况下为 32 x 32 像素,使用 $args['show_image'] = true; 添加此行:

    $args['image_size'] = array( 48, 48 );
    

    经过测试并且也可以工作。

    【讨论】:

    • 我没有在电子邮件中获取图像超链接工作正常,但图像不工作
    • 添加了我如何在电子邮件中获取它,它显示差距但没有获取图像
    • 总代码应该添加到functions.php文件中,否则add_filter中的代码('woocommerce_order_item_thumbnail', 'add_email_order_item_permalink', 10, 2); // 产品图片这一行添加到functions.php文件中
    • 实际上我已经复制了function.php文件本身的所有代码,但我仍然无法在电子邮件中看到产品图像。我也将我的代码恢复到电子邮件模板中,但仍然无法看图片
    • 我遇到了同样的问题,图片在我的电子邮件中显示为损坏的图像。我尝试了一个不同的电子邮件帐户,结果发现它与这些设置有关,而不是与代码有关。我为我尝试的第一个电子邮件帐户启用了 html 电子邮件,但仍然如此。无论如何,这可能对遇到此问题的人有所帮助。
    猜你喜欢
    • 2021-03-26
    • 2019-10-07
    • 2020-12-07
    • 1970-01-01
    • 2019-08-27
    • 2018-06-04
    • 2021-02-10
    • 2012-09-01
    相关资源
    最近更新 更多