基于 Display the product image in Woocommerce email notifications我编写了以下代码以在 WooCommerce 电子邮件通知中显示产品图像。
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;
$args['image_size'] = array( 32, 32 );
$args['show_sku'] = 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 cw_edit_order_item_name( $name ) {
$name = '<br><br/>' .$name;
return $name . '<br><br /><center><strong>SKU:</strong></center>';
}
add_filter( 'woocommerce_order_item_name', 'cw_edit_order_item_name' );
我在订单电子邮件中的 WooCommerce 订单项目表中有这个问题,需要将产品缩略图移动到产品标题的左侧,并且标题应该整齐地放在右侧。
我们将不得不更改订单项目表列的宽度,以便很好地适应。这也需要移动响应。
这是我正在尝试实现的目标的屏幕截图:
这就是我所拥有的:
需要考虑的事情,
我将非常感谢这里的任何帮助。
最佳答案
您可以通过多种方式解决您的问题。
为了理解我的回答,我复制/粘贴了负责来自 templates/emails/email-order-items.php 的电子邮件中输出的一段代码。第 33 - 58 行 @version 3.7.0
if ( is_object( $product ) ) {
$sku = $product->get_sku();
$purchase_note = $product->get_purchase_note();
$image = $product->get_image( $image_size );
}
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
<td class="td" style="text-align:<?php echo esc_attr( $text_align ); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;">
<?php
// Show title/image etc.
if ( $show_image ) {
echo wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) );
}
// Product name.
echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );
// SKU.
if ( $show_sku && $sku ) {
echo wp_kses_post( ' (#' . $sku . ')' );
}
// allow other plugins to add additional product information here.
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );
$show_image
为 true,将显示产品图片。 默认情况下,这是 false ,但这可以通过 woocommerce_email_order_items_args
更改过滤钩。woocommerce_email_order_items_args
确定过滤钩 (默认为 false) ,但是没有选项可以将 HTML 标签添加到 sku 的输出中所以你可以使用几个不同的过滤器钩子(Hook)来放置从假到真的值
以及向输出添加额外的 HTML(sku 除外)。所以这只是部分可控/适应性强。
但是,在最后一行 我们找到了
woocommerce_order_item_meta_start
Action 钩。这允许其他插件添加额外的产品信息。所以我们将使用那个钩子(Hook),因为它可以保存/修改以前的过滤器钩子(Hook)将包含的任何东西。所以你得到:
// Displayed by default, so hide!
function filter_woocommerce_order_item_name( $item_name, $item, $false ) {
// Only email notifications
if ( is_wc_endpoint_url() ) return $item_name;
$item_name = '';
return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'filter_woocommerce_order_item_name', 10, 3 );
// NEW OUTPUT
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) {
// Only email notifications
if ( is_wc_endpoint_url() ) return;
// Settings
$image_size = array( 64, 64 );
// Get product
$product = $item->get_product();
if ( is_object( $product ) ) {
$sku = $product->get_sku();
$image = $product->get_image( $image_size );
}
// OUTPUT
// Show image
echo '<span style="float:left;width: fit-content;">' . $image. '</span>';
// Product name + link + SKU (if set).
if ( $sku ) {
echo '<span><a href="' . esc_url( $product->get_permalink() ). '">' . $item->get_name() . ' (#' . $sku . ')' . '</a></span>';
} else {
echo '<span><a href="' . esc_url( $product->get_permalink() ). '">' . $item->get_name() . '</a></span>';
}
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10 , 4 );
结果注意:通过在我的回答中调整带有 CSS 的 HTML,您可以根据自己的意愿完全调整输出
关于wordpress - 更改 WooCommerce 电子邮件通知中产品图片的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69058059/