Question:
How to get item details of Woo Commerce in custom email content?

To retrieve WooCommerce product details in a custom email content, you can use the WooCommerce hooks and functions. >WooCommerce provides various hooks that allow you to customize email content and include product information. 


Assuming you want to customize the content of the "New Order" email, you can use the woocommerce_email_order_details hook. Here's a basic example:


Open your theme's functions.php file or create a custom plugin.


Use the following code to customize the email content and see all suborder items:

// Send custom email notifications to sub orders only

add_action( 'woocommerce_order_status_changed', Custom_order_statuses_email_notifications', 10, 4 );

function custom_order_statuses_email_notifications ($order_id, $from_status, $to_status, $order) {

    // Targeting sub-orders only

    if ( ! $order->get_parent_id() ) return;


    if ( $to_status === 'shipped' ) {

        $status_label  = __( 'shipped', 'woocommerce' );

        $status_label_message  = __( 'shipped', 'woocommerce' );

    } 

    elseif ( $to_status === 'readytocollect' ) {

        $status_label  = __( 'readytocollect', 'woocommerce' );

        $status_label_message  = __( 'readytocollect', 'woocommerce' );

    }


    if ( isset($status_label) ) {

        $mailer        = WC()->mailer(); // load the mailer class.

        $email_subject = sprintf( __( 'Order %s %s' ), $order->get_order_number(), $status_label );


        $message_body = '<html><head></head>

        <body>

            <h1>'.$status_label.'</h1>

            <p>'.$status_label_message.'</p>

            <p></p>

            <table class="td" cellspacing="0" cellpadding="4"  style="width: 100%; color: #001a34; font-size: 14px; font-weight: normal;" border="0">

            <thead>';

        

        foreach( $order->get_items() as $item ){

            $product = $item->get_product(); // Get product

            $image   = $product->get_image(array( 32, 32 )); // Get product image 32 x 32 px


            $message_body .= '<tr>

                <th width="65%" class="td" style="text-align:left;" scope="col" >'.$image.' '.$item->get_name().'</th>

                <th width="10%" class="td" style="text-align:right;" scope="col" >'.$item->get_quantity().'</th>

                <th width="25%" class="td" style="text-align:right;" scope="col" >'.$item->get_total().'</th>

            </tr>';

        }


        $message_body .= '</thead></table>

        </body></html>';


        $message = $mailer->wrap_message( $email_subject, $message_body );

        $mailer->send( $order->get_billing_email(), $email_subject, $message ); // Send email

    }

}


Suggested blogs:

>Need input on Standalone SSRS SQL Server 2016 Report Builder

>How to fix: View not found even though it exists issue?

>Fixing C# sync over async implementations

>How to optimize getting multiple documents 'one by one' in Firestore?

>Adding EJS ForEach loop tag

>How to get pending ActiveMQ messages?

>How to pull out different elements from a text file? - Python

>Adding cooldown for user tries to farm XP-discord.py

>How to use/set up Node Express sessions?

>Can I make a specific character the center point on a header?

>Authenticating user using PassportJS local strategy during registration


Submit
0 Answers