برای نمایش محصولات مرتبط در ایمیل ارسالی ووکامرس کافیست کد زیر را در فایل فانکشن قالب وب سایت خود قرار دهید.
/**
* @snippet Product Grid @ WooCommerce Emails
* @Source https://wpwave.ir/
*/
add_action( 'woocommerce_email_after_order_table', 'wpwave_add_product_grid_specific_email', 20, 4 );
function wpwave_add_product_grid_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
$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 '<h2>محصولات مرتبط</h2>';
$html = '';
$col = 1;
$cols = 3;
$limit = 6;
$html .= '<div><table style="table-layout:fixed;width:100%;"><tbody>';
foreach ( array_slice( $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">';
$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;
}
آموزش ویرایش فایل فانکشن وردپرس [Function.php]
در صورتی که می خواهید محصولات مرتبط صرفاً به یکی از ایمیل های ارسالی اعمال شود می بایست در کد ID ایمیل را نیز مشخص کنید. بنابراین می توانید از کد زیر استفاده نمایید.
/**
* @snippet Product Grid @ WooCommerce Emails
* @Source https://wpwave.ir/
*/
add_action( 'woocommerce_email_after_order_table', 'wpwave_add_product_grid_specific_email', 20, 4 );
function wpwave_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 '<h2>محصولات مرتبط</h2>';
$html = '';
$col = 1;
$cols = 3;
$limit = 6;
$html .= '<div><table style="table-layout:fixed;width:100%;"><tbody>';
foreach ( array_slice( $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">';
$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;
}
}
افزودن متن دلخواه به ایمیل های ووکامرس
در کد بالا بجای if ( $email->id == ‘customer_processing_order’ ) می توانید از ID های دیگر استفاده کنید. لیست ID ایمیل های ووکامرسی را در ادامه مشاهده خواهید کرد.
if ( $email->id == 'cancelled_order' )
if ( $email->id == 'customer_completed_order' )
if ( $email->id == 'customer_invoice' )
if ( $email->id == 'customer_new_account' )
if ( $email->id == 'customer_note' )
if ( $email->id == 'customer_on_hold_order' )
if ( $email->id == 'customer_refunded_order' )
if ( $email->id == 'customer_reset_password' )
if ( $email->id == 'failed_order' )
if ( $email->id == 'new_order' )
می توانید عناوین و متون دلخواه خود را در کد جایگزین کنید. همچنین می توانید با تغییر مقادیر $cols و $limit تعداد ستون ها و تعداد محصولات نمایش را تغییر دهید. در صورتی که با کدهای CSS آشنایی دارید می توانید CSS دلخواه خود را به صورت Inline در کد فوق قرار دهید یا به کمک ترفند افزودن CSS دلخواه به ایمیل های ارسالی ووکامرس، استایل دلخواه خود را به ایمیل های ارسالی اعمال نمایید.