0

I have made a filter to update how order is displayed on woocommerce. Basically I need the shop owner to be able to click the name of each product (linked now to the featured image) and also him to be able to see the URL (because the image file name is useful for them to track the product)

I need this to ONLY affect the NEW ORDER email sent to the shop owner.

My code placed in functions.php does update BUT in ALL emails and also order confirmation table at the website.

Question? How can I ONLY affect the new order email? I think I'm missing something here.

// item name link to product

add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 2 );
function display_product_title_as_link( $item_name, $item ) {

    $_product = get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );

    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->post->ID ), 'full' );

    return '<a href="'. $image[0] .'"  rel="nofollow">'. $item_name .'</a>
    <div style="color:blue;display:inline-block;clear:both;">'.$image[0].'</div>';

}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Yala Yala Herzlin
  • 552
  • 2
  • 8
  • 25

2 Answers2

1

First there is some errors in your code like:

  • The function get_product() is clearly outdated and has been replaced by wc_get_product()
  • Since Woocommerce 3+ WC_Product properties can be accessed directly, instead use available methods.

Here is the right way to get what you are expecting (in "New Order" admin notification only):

// Your custom function revisited
function display_product_title_as_link( $item_name, $item ) {
    $product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
    $image = wp_get_attachment_image_src( $product->get_image_id(), 'full' );
    $product_name = $product->get_name();
    return '<a href="'. $image[0] .'" rel="nofollow">'. $product_name .'</a>
    <div style="color:blue;display:inline-block;clear:both;">'.$image[0].'</div>';
}

// The hooked function that will enable your custom product title links for "New Order" notification only
add_action( 'woocommerce_email_order_details', 'custom_email_order_details', 1, 4 );
function custom_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
    // Only for "New Order" and admin email notification
    if ( 'new_order' != $email->id && ! $sent_to_admin ) return;
    // Here we enable the hooked function
    add_filter( 'woocommerce_order_item_name', 'display_product_title_as_link', 10, 3 );
}

Code goes in function.php file of your active child theme (active theme or in any plugin file).

Tested and Works in WooCommerce 3+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your prompt reply and correction, I really appreciate. I have tested and does work, but still shows the change on the emails that the client is receiving. Both, new order notification for admin and client emails have the modified title with link to the image. I need to only have this in the email sent to the store owner. – Yala Yala Herzlin Oct 12 '17 at 02:49
  • @GauchoCode … For me on my test server it works perfectly (Under WooCommerce Version 3.1.2) . I never publish an answer before testing it… Important: Have you removed your old code before adding this one? – LoicTheAztec Oct 12 '17 at 03:06
  • I have double-checked and there are no traces of my old code. Just the one you have crafted, both emails (client and admin) look identical, both have the modified tittle and show the image url. testing with Woo latest version. I have made several tests and all show the same in both emails... http://i63.tinypic.com/6hoo4l.png – Yala Yala Herzlin Oct 12 '17 at 12:27
  • @GauchoCode For me it's working… Sorry, but I can't guess what is going on your Wordpress installation and settings that make this not working for you… Let say that I have tried. – LoicTheAztec Oct 12 '17 at 12:40
  • 1
    I have tested with a clean WP no other plugin, and it still affects my emails for client too... thanks for trying ! I will see if I can combine your code with other pieces of code and make it work as I need. Thanks for the cleanup and update of my code. – Yala Yala Herzlin Oct 12 '17 at 12:52
  • @LoicTheAztec WooCommerce version: 3.9.2 & WordPress version: 5.3.2. Your code shows image to Customer as well. Any tweak to fix this please so that images are shown ONLY TO ADMIN? Thanks a lot! – KoolPal Feb 18 '20 at 04:54
1

@LoicTheAztec - it could not work! href as image? Full image as a thumbnail?(big email) Missing permalink to product... Repair function display_product_title_as_link:

function display_product_title_as_link( $item_name, $item ) {
    $product = wc_get_product( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] );
    $image = wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' );
    $product_name = $product->get_name();
    $product_link = get_permalink( $product->get_id() );
    return '<a href="'. $product_link .'" target="_blank"><img width="70" height="70" src="'.$image[0].'" alt="'. $product_name .'">'. $product_name .'</a> ';
}
Stieranka
  • 72
  • 3