在 woocommerce 电子邮件中输出产品自定义字段(图像)

我的 WooCommerce 商店中的产品通过高级自定义字段插件添加了自定义字段store_email_logo。这个字段是一个图像字段,我不知道如何在 WooCommerce 电子邮件中输出图像。


我尝试了下面的代码,但它不起作用,它输出一些数字而不是图像。


// Tested on WooCommerce version 2.6.x and 3+ — For simple products only.

add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email_logo', 10, 4);

function wcv_ingredients_email_logo( $order,  $sent_to_admin,  $plain_text,  $email ){

    foreach($order->get_items() as $item_values){

        // Get the product ID for simple products (not variable ones)

        $product_id = $item_values['product_id'];

        $output = get_post_meta( $product_id, 'store_email_logo', true );

        echo ' ' . $output . '<br>';

    }

}


月关宝盒
浏览 170回答 2
2回答

MMTTMM

显示的“数字”是与您的产品相关联的图像的 ID。有几个函数可以为您输出实际图像。我非常偏爱这个wp_get_attachment_image_src($image_id, $size)功能。add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email_logo', 10, 4);function wcv_ingredients_email_logo( $order,&nbsp; $sent_to_admin,&nbsp; $plain_text,&nbsp; $email ){&nbsp; &nbsp; foreach($order->get_items() as $item_values){&nbsp; &nbsp; &nbsp; &nbsp; // Get the product ID for simple products (not variable ones)&nbsp; &nbsp; &nbsp; &nbsp; $product_id&nbsp; &nbsp; &nbsp;= $item_values['product_id']; //get the product ID&nbsp; &nbsp; &nbsp; &nbsp; $image_id&nbsp; &nbsp; &nbsp; &nbsp;= get_post_meta( $product_id, 'store_email_logo', true ); //get the image ID associated to the product&nbsp; &nbsp; &nbsp; &nbsp; $image_src&nbsp; &nbsp; &nbsp; = wp_get_attachment_image_src( $image_id, 'full' )[0]; //get the src of the image - you can use 'full', 'large', 'medium', or 'thumbnail' here,&nbsp; &nbsp; &nbsp; &nbsp; $image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = '<img src="'.$image_src.'">'; //create the img element&nbsp; &nbsp; &nbsp; &nbsp; echo $image . '<br>'; //echo the image&nbsp; &nbsp; }}

慕斯王

试试下面的代码。它可能会帮助你。add_action( 'woocommerce_email_order_details', 'action_wc_email_order_details' 50, 4 );function action_wc_email_order_details( $order, $sent_to_admin, $plain_text, $email ){// Get the Order ID (WooCommerce retro-compatibility)$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;// Get "store_email_logo" custom field value$store_email_logo= get_post_meta($order_id, "store_email_logo", true );// Display "serial" custom field valueecho '<img src="'.__('store_email_logo', 'woocommerce') . $store_email_logo. '" alt="image">';}
打开App,查看更多内容
随时随地看视频慕课网APP