【问题标题】:Output product custom field (image) in woocommerce emails在 woocommerce 电子邮件中输出产品自定义字段(图像)
【发布时间】: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


【解决方案1】:

显示的“数字”是与您的产品关联的图像的 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,  $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']; //get the product ID
        $image_id       = get_post_meta( $product_id, 'store_email_logo', true ); //get the image ID associated to the product
        $image_src      = wp_get_attachment_image_src( $image_id, 'full' )[0]; //get the src of the image - you can use 'full', 'large', 'medium', or 'thumbnail' here,
        $image          = '<img src="'.$image_src.'">'; //create the img element
        echo $image . '<br>'; //echo the image
    }
}

【讨论】:

  • 谢谢我尝试了代码,但现在我在邮件中收到损坏的图像图标而不是图像。
  • @MarkoI。啊 - 我忘了在wp_get_attachment_image_src() 的末尾添加一个[0] (它返回一个[源,宽度,高度]的数组) - 我已经编辑了我的答案 - 再试一次?
  • 嗯,现在什么都没有显示 - 甚至没有损坏的图像图标
  • 啊抱歉,它有效,我在格式化方面遇到了问题
  • 哦拉德 - 我很高兴听到 :)
【解决方案2】:

试试下面的代码。它可能会帮助你。

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 value
echo '<img src="'.__('store_email_logo', 'woocommerce') . $store_email_logo. '" alt="image">';
}

【讨论】:

  • 感谢我尝试了代码,但邮件中的图像图标损坏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2020-12-07
  • 2021-03-26
  • 1970-01-01
  • 2018-08-29
  • 2018-08-30
  • 1970-01-01
相关资源
最近更新 更多