WooCommerce Product Order Emails Related Products Display

WooCommerce Product Order Emails Related Products Display
183 Views

Show Similar Items in WooCommerce Customer Order Processing Email

<?php
add_action( 'woocommerce_email_after_order_table', 'cxc_add_releted_product_specific_email_call_back', 20, 4 );

function cxc_add_releted_product_specific_email_call_back( $order, $sent_to_admin, $plain_text, $email ) {

	if ( $email->id == 'customer_processing_order' ) {

		$cxc_related = array();
		if( $order->get_items() ){
			foreach ( $order->get_items() as $item_id => $item ) {         
				$cxc_related = array_merge( wc_get_related_products( $item->get_product_id() ), $cxc_related );
			}
		}
		if ( ! $cxc_related ){ return; }
		shuffle( $cxc_related );
		$cxc_related = array_unique( $cxc_related );

		echo '<h2>You May Also Like</h2>';

		$html = '';
		$col = 1;
		$cols = 3;
		$limit = 3;
		$html .= '<div><table style="table-layout:fixed;width:100%;margin-bottom:30px;"><tbody>';      
		if( $cxc_related ){
			foreach ( array_slice( $cxc_related, 0, $limit ) as $product_id ) {
				$product = wc_get_product( $product_id );
				$html .= ( $col + $cols - 1 ) % $cols === 0 ? '<tr>' : '';
				$html .= '<td style="text-align:center;vertical-align:bottom;border:1px solid #7f54b3;">';
				$html .= $product->get_image();
				$html .= '<h3 style="text-align:center;">' . $product->get_title() . '</h3>';
				$html .= '<p>' . $product->get_price_html() . '</p>';
				$html .= '<p><a href="' . get_permalink( $product_id ) . '">' . __( 'Read more', 'woocommerce' ) . '</a></p></td>';
				$html .= $col % $cols === 0 ? '</tr>' : '';
				$col++;
			}
		}
		$html .= '</tbody></table></div>';

		echo $html;

	}
}
?>

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.