Search code examples
phpwordpresswoocommerceordersemail-templates

Get the Product ID in email template: WooCommerce


I am trying to add a column mentioning the ID of the product purchased in the woocommerce mail template sent to the administrator. (Admin / order) After several failed attempts, I leave it to you in the hope of finding a quicker solution! thank you in advance!

foreach ( $order->get_items() as $item_id => $item ) :
                    // $_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
                    $_product       = $item->get_product();

                    if ( version_compare( WC_VERSION, '3.1', '>=' ) ) {
                        $item_meta    = new WC_Order_Item_Product( $item, $_product );
                    }else{
                        $item_meta    = new WC_Order_Item_Meta( $item, $_product );
                    }

                    if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
                        ?>
                        <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
                            <td style="text-align:center; width:100px; vertical-align:middle; border: 1px solid #eee; border-left:0; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style=""><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="30px" style="height:30px; width:auto; vertical-align:middle; margin-right: 10px;" /></div>', $item ); ?></td>
                            <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php

                                // Product name.
                                echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) );

                                // UGS / SKU ID
                                if ( is_object( $_product ) && $_product->get_id()!="" ) {
                                    echo ' (#' . $_product->get_id() . ')';
                                }
                                
                                // SKU
                                if ( is_object( $_product ) && $_product->get_sku()!="" ) {
                                    echo ' (#' . $_product->get_sku() . ')';
                                }


                                // allow other plugins to add additional product information here.
                                do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

                                wc_display_item_meta(
                                    $item,
                                    array(
                                        'label_before' => '<strong class="wc-item-meta-label" style="float: ' . esc_attr( $text_align ) . '; margin-' . esc_attr( $margin_side ) . ': .25em; clear: both">',
                                    )
                                );

                                // allow other plugins to add additional product information here.
                                do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );


Solution

  • Try This Work For Me

    foreach ( $order->get_items() as $item_id => $item ) {
       $product_id = $item->get_product_id();
       echo $product_id; // Here Your Product!
    }