在WooCommerce新订单邮件中显示产品缩略图。

7

我有一个运行着的WordPress/WooCommerce网站,我想编辑管理员在收到新订单时收到的电子邮件,以显示产品的缩略图。 我将模板复制到我的主题目录下 - /themes/mytheme/woocommerce/emails/admin-new-order.php:

    <?php echo $order->email_order_items_table( true, false, true, true, array( 150, 150 )     ); ?>

以下是来自 woocommerce/classes/class-wc-order.php 文件的代码:

    /**
 * Output items for display in html emails.
 *
 * @access public
 * @param bool $show_download_links (default: false)
 * @param bool $show_sku (default: false)
 * @param bool $show_purchase_note (default: false)
 * @param bool $show_image (default: false)
 * @param array $image_size (default: array( 32, 32 )
 * @param bool plain text
 * @return string
 */
public function email_order_items_table( $show_download_links = false, $show_sku = false, $show_purchase_note = false, $show_image = false, $image_size = array( 32, 32), $plain_text = false ) {

    ob_start();

    $template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';

    woocommerce_get_template( $template, array(
        'order'                 => $this,
        'items'                 => $this->get_items(),
        'show_download_links'   => $show_download_links,
        'show_sku'              => $show_sku,
        'show_purchase_note'    => $show_purchase_note,
        'show_image'            => $show_image,
        'image_size'            => $image_size
    ) );

    $return = apply_filters( 'woocommerce_email_order_items_table', ob_get_clean() );

    return $return;
}

这是来自/plugins/woocommerce/templates/emails的代码。
<?php
 /**
  * Email Order Items
  *
  * @author         WooThemes
  * @package    WooCommerce/Templates/Emails
  * @version     2.0.3
  */

      if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

      global $woocommerce;

      foreach ($items as $item) :

// Get/prep product data
$_product = $order->get_product_from_item( $item );
$item_meta = new WC_Order_Item_Meta( $item['item_meta'] );
$image = ($show_image) ? '<img src="/wp/'. current(wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail')) .'" alt="img" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />' : '';

?>
<tr>
    <td style="text-align:left; vertical-align:middle; border: 1px solid #eee; word-wrap:break-word;"><?php

        // Show title/image etc
        echo    apply_filters( 'woocommerce_order_product_image', $image, $_product, $show_image);

        // Product name
        echo    apply_filters( 'woocommerce_order_product_title', $item['name'], $_product );

        // SKU
        echo    ($show_sku && $_product->get_sku()) ? ' (#' . $_product->get_sku() . ')' : '';

        // File URLs
        if ( $show_download_links && $_product->exists() && $_product->is_downloadable() ) {

            $download_file_urls = $order->get_downloadable_file_urls( $item['product_id'], $item['variation_id'], $item );

            $i = 0;

            foreach ( $download_file_urls as $file_url => $download_file_url ) {
                echo '<br/><small>';

                $filename = woocommerce_get_filename_from_url( $file_url );

                if ( count( $download_file_urls ) > 1 ) {
                    echo sprintf( __('Download %d:', 'woocommerce' ), $i + 1 );
                } elseif ( $i == 0 )
                    echo __( 'Download:', 'woocommerce' );

                echo ' <a href="' . $download_file_url . '" target="_blank">' . $filename . '</a></small>';

                $i++;
            }
        }

        // Variation
        echo    ($item_meta->meta) ? '<br/><small>' . nl2br( $item_meta->display( true, true ) ) . '</small>' : '';

    ?></td>
    <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty'] ;?></td>
    <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
</tr>

<?php if ($show_purchase_note && $purchase_note = get_post_meta( $_product->id, '_purchase_note', true)) : ?>
    <tr>
        <td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo apply_filters('the_content', $purchase_note); ?></td>
    </tr>
<?php endif; ?>

这就是结果,一封没有缩略图的电子邮件。我已经多次更改了alt属性和图片大小,并且它工作得很好,然而img src仍然缺失

<td style="text-align:left;vertical-align:middle;border:1px solid #eee;word-wrap:break-   word">
<img alt="img" height="150" width="150" style="vertical-align:middle;margin-right:10px">prueba test<br><small></small>
</td>

我做错了什么? 感谢您的任何帮助!
2个回答

3

WC已经在代码中实现了此功能,您只需要从模板文件中启用它。将WooCommerce目录放置在您的主题内,并前往:

/wp-content/themes/YOUR THEME NAME/woocommerce/emails

从文件email-order-details.php中找到以下代码:

<?php echo $order->email_order_items_table( array(
        'show_sku'      => $sent_to_admin,
        'show_image'    => false,
        'image_size'    => array( 50, 50 ),
        'plain_text'    => $plain_text,
        'sent_to_admin' => $sent_to_admin
    ) ); ?>

并将其替换为以下内容:

<?php echo $order->email_order_items_table( array(
        'show_sku'      => $sent_to_admin,
        'show_image'    => true,
        'image_size'    => array( 50, 50 ),
        'plain_text'    => $plain_text,
        'sent_to_admin' => $sent_to_admin
    ) ); ?>

全部完成。


1

我知道,这是一个老问题,但我想指出(以防有人想知道),问题很可能是由于在文件woocommerce/templates/emails/email-order-items.php中传递给img标签的URL格式错误导致的:

$image = ($show_image) ? '<img src="/wp/'. current(wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail')) .'" alt="img" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />' : '';

src 属性中错误地添加了 /wp/,导致 URL 如下:

'/wp/http://www.website.com/path/to/the/image.jpg'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接