在woocommerce电子邮件标题中输出产品自定义字段

时间:2019-08-02 08:55:31

标签: php wordpress woocommerce hook-woocommerce

我正在尝试在订单电子邮件标题中输出产品的自定义字段。挂钩是woocommerce_email_header ($email_heading, $email)。我尝试了以下代码,但不适用于woocommerce_email_header,在结帐时出现内部服务器错误。

add_action('woocommerce_email_header', 'wcv_ingredients_email_logo', 10, 4);
function wcv_ingredients_email_logo( $order,  $sent_to_admin,  $plain_text, $email_heading, $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
    }
}

1 个答案:

答案 0 :(得分:1)

只需用以下代码段替换您的代码-

add_action('woocommerce_email_header', 'wcv_ingredients_email_logo', 10, 2);
function wcv_ingredients_email_logo( $email_heading, $email ){
    if($email->object){
        foreach($email->object->get_items() as $item_values){
            // Get the product ID for simple products (not variable ones)
            $product        = $item_values->get_product();
            $image_id       = get_post_meta( $product->get_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
        }
    }
}