【发布时间】:2019-07-31 15:46:36
【问题描述】:
我的 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>';
}
}
【问题讨论】:
-
它输出什么? “一些数字”可以是任何东西
-
我刚刚检查了它的数据ID:“42”
-
get_post_meta检索帖子元字段,在本例中为您的产品。store_email_logo元字段正在输出42。因此,您需要采取另一个步骤来获取具有该 ID 的徽标,否则该字段中的值不正确。 -
我打赌“42”是媒体 ID,您只需要这样做。
get_media_item()获取文件本身。 developer.wordpress.org/reference/functions/get_media_item -
是的,但我应该在哪里添加 get_media_item()?
标签: php wordpress woocommerce hook-woocommerce