In my WooCommerce webshop I am selling natural products like shampoo and soap. Currently I only have 2 products.
I would like to add a specific string of text with an hyperlink to some background information about product being bought, to stop people from being confused:
So Product A equals hyperlink A, product B equals hyperlink B.
This is what I've got so far:
$order = wc_get_order( $order_id ); // optional (to test without it)
foreach ($order->get_items() as $item_id => $item) {
$product_name = $item['Palm Oil Shampoo']; // product name
$product_id = $order->get_item_meta($item_id, '_POShamp_05', true); // product ID
$product_description = get_post($product_id)->post_content; // Product description
}
But it doesn't work.
How can I add some custom text with specific links based on the product bought, in the customer email notifications?
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(); // The product name
$product_id = $item->get_product_id(); // The product ID
$product = $item->get_product(); // The WC_Product Object
$product_description = $product->get_description(); // The Product 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 custom text hyperlinks to Order items
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 )
{
// Only for customer "Processing" and "Completed" Order email notifications
if( ! ($email->id == 'customer_processing_order' || $email->id == 'customer_completed_order') ) return;
// HERE (Below) define your product IDs, links, texts, title …
$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. …");
// Iterating through order "line items"
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id(); // The product ID
// Set all the product IDs in this order in an array
$product_ids[$product_id] = $product_id;
}
$has_product = false;
// CSS style
$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 (start)
$html_output = '<h2>'.$title.'</h2>
<p>'.$instroduction.'</p>
<table class="product-info" cellspacing="0" cellpadding="6">';
// Iterating through the product ids (when multiple products are in the order)
foreach ( $product_ids as $value_id ) {
// ==> HERE Replace links path and linked text by yours (for each product)
if ( $product_id == $product_id1 ) {
// PRODUCT 1
$html_output .= '<tr><td><a href="' . $link1 . '">' . $text1 . '</a></td></tr>'; // HTML
$has_product = true;
} elseif ( $product_id == $product_id2 ) {
// PRODUCT 2
$html_output .= '<tr><td><a href="' . $link2 . '">' . $text2 . '</a></td></tr>'; // HTML
$has_product = true;
}
}
if( $has_product ){
$html_output .= '</table><br>'; // HTML (end)
// Output CSS and HTML
echo $styles . $html_output;
}
}
Tested on WooCommerce 3+ and works. You will get something like: