Reputation: 622
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>';
}
Upvotes: 0
Views: 1452
Reputation: 82
@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> ';
}
Upvotes: 1
Reputation: 253867
First there is some errors in your code like:
get_product()
is clearly outdated and has been replaced by wc_get_product()
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+
Upvotes: 1