Bad news first – we’ve seen how to add content to any WooCommerce order email, however I did not specify that if you use the
-
- Oferta!
- Sem categoria
Acesso Premium
- Original price was: R$350.00.R$200.00Current price is: R$200.00.
- Comprar
So, I want to fix this, and find a WooCommerce email-compatible way to show a grid of products based on a list of product IDs (for example, the list of related products based on the ordered items), and make sure I can actually see product images, titles, prices and a link. Enjoy!
My attempt at using the
-
- Oferta!
- Sem categoria
Acesso Premium
- Original price was: R$350.00.R$200.00Current price is: R$200.00.
- Comprar
PHP Snippet: Display Related Products Grid @ WooCommerce Customer Processing Order Email
Note 1: you can target any WooCommerce email. Here, I’m adding content to the Customer Processing Order email, but you could do it on the Completed one only, or all of them. Check this guide to find out the email ID you should use in the snippet below.
Note 2: in the snippet I’m calculating the Related Products from the order items. You can use a custom list of product IDs instead if you wish – simply remove the $related calculations, and edit the foreach iterable_expression from “array_slice( $related, 0, $limit )” to “array( 123, 456, 789 )” where 123, 456 and 789 are the product IDs you want to show.
Note 3: I’m displaying 3 columns and 2 rows of products. Change $cols = 3 and $limit = 6 based on your requirements ($cols = 4 and $limit = 4 would give you 4 items in 1 row).
/** * @snippet Product Grid @ WooCommerce Emails * @how-to Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 7 * @donate $9 https://businessbloomer.com/bloomer-armada/ */ add_action( ‘woocommerce_email_after_order_table’, ‘bbloomer_add_product_grid_specific_email’, 20, 4 ); function bbloomer_add_product_grid_specific_email( $order, $sent_to_admin, $plain_text, $email ) { if ( $email->id == ‘customer_processing_order’ ) { $related = array(); foreach ( $order->get_items() as $item_id => $item ) { $related = array_merge( wc_get_related_products( $item->get_product_id() ), $related ); } if ( ! $related ) return; shuffle( $related ); $related = array_unique( $related ); echo ‘
Related Products
‘; $html = ”; $col = 1; $cols = 3; $limit = 6; $html .= ‘
‘; $html .= $product->get_image(); $html .= ‘
‘ . $product->get_title() . ‘‘; $html .= ‘ ‘ . $product->get_price_html() . ‘ ‘; $html .= ‘ |
‘; echo $html; } }
You can place PHP snippets at the bottom of your child theme functions.php file (delete “?>” if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files – if you need more guidance, please take a look at my free video tutorial “Where to Place WooCommerce Customization?”
If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance 🙂
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!