Your code is just a bit outdated regarding Woocommerce 3+, it should be something like:
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product = $item->get_product();
$product_description = $product->get_description();
}
Now you can display your linked text in some customer email notifications based on product IDs, defining in this custom hooked function your product IDs, links, texts, title… Also remember that you can have many different products in an order.
Here is the code:
add_action( 'woocommerce_email_after_order_table', 'add_product_custom_link', 9, 4 );
function add_product_custom_link( $order, $sent_to_admin, $plain_text, $email )
{
if( ! ($email->id == 'customer_processing_order' || $email->id == 'customer_completed_order') ) return;
$product_id1 = 37;
$product_id2 = 53;
$link1 = home_url( '/some-path/product-info1/' );
$link2 = home_url( '/some-path/product-info1/' );
$text1 = __( 'Your linked text1' );
$text2 = __( 'Your linked text1' );
$title = __("Product Documentation Link");
$instroduction = __("Some introduction text paragraph here, blabla bla blabla blablabla bla blabla. …");
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$product_ids[$product_id] = $product_id;
}
$has_product = false;
$styles = '<style>
table.product-info{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
table.product-info th, table.product-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
table.product-info td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>
';
$html_output = '<h2>'.$title.'</h2>
<p>'.$instroduction.'</p>
<table class="product-info" cellspacing="0" cellpadding="6">';
foreach ( $product_ids as $value_id ) {
if ( $product_id == $product_id1 ) {
$html_output .= '<tr><td><a href="' . $link1 . '">' . $text1 . '</a></td></tr>';
$has_product = true;
} elseif ( $product_id == $product_id2 ) {
$html_output .= '<tr><td><a href="' . $link2 . '">' . $text2 . '</a></td></tr>';
$has_product = true;
}
}
if( $has_product ){
$html_output .= '</table><br>';
echo $styles . $html_output;
}
}
Tested on WooCommerce 3+ and works. You will get something like:
