Marko I.
Marko I.

Reputation: 562

Output product custom field in woocommerce email header

I'm trying to output the product's custom field in the order email header. The hook for this is woocommerce_email_header ($email_heading, $email). I tried the code below but it doesn't work for woocommerce_email_header, I'm getting internal server error on a checkout.

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
    }
}

Upvotes: 0

Views: 262

Answers (1)

itzmekhokan
itzmekhokan

Reputation: 2768

Just replace your code with follows code snippet -

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
        }
    }
}

Upvotes: 1

Related Questions