skip to Main Content

Products in my WooCommerce store have a custom field added through Advanced Custom Field plugin store_email_logo. This field is an image field and I can’t figure out how to output the image in a WooCommerce email.

I tried the code below but it’s not working, it outputs some number instead of an image.

// 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>';
    }
}

2

Answers


  1. try the below code. It may help you.

    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">';
    }
    
    Login or Signup to reply.
  2. The “number” that is displaying is the ID of the image that is associated to your product. There are a couple of functions that can output the actual image for you. I am quite partial to the wp_get_attachment_image_src($image_id, $size) functionality.

    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
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search