Created
          September 8, 2021 07:40 
        
      - 
      
 - 
        
Save coder618/46734a58c0cc587e9dd24761f7df20fd to your computer and use it in GitHub Desktop.  
    WooCommerce Order email object 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| /* | |
| This function will return easy to use email object which can be use in | |
| any email template by giving the order id only | |
| */ | |
| function coder618_get_email_obj($order_id){ | |
| $email_obj = ['order_id' => $order_id]; | |
| $hidden_order_itemmeta = apply_filters( | |
| 'woocommerce_hidden_order_itemmeta', | |
| array( | |
| '_qty', | |
| '_tax_class', | |
| '_product_id', | |
| '_variation_id', | |
| '_line_subtotal', | |
| '_line_subtotal_tax', | |
| '_line_total', | |
| '_line_tax', | |
| 'method_id', | |
| 'cost', | |
| '_reduced_stock', | |
| '_restock_refunded_items', | |
| ) | |
| ); | |
| $order = new WC_Order( $order_id ); // here | |
| $order_date = wc_format_datetime( $order->get_date_created(), "d M Y" ); | |
| $email_obj['order_date'] = $order_date; | |
| $items_count = count($order->get_items()); | |
| $email_obj['order_items_count'] = $items_count; | |
| // order items | |
| foreach ( $order->get_items() as $item_id => $item ){ | |
| $product = $item->get_product(); | |
| $sku = ''; | |
| $image = ''; | |
| if ( ! apply_filters( 'woocommerce_order_item_visible', true, $item ) ) { | |
| continue; | |
| } | |
| if ( is_object( $product ) ) { | |
| $sku = $product->get_sku(); | |
| $purchase_note = $product->get_purchase_note(); | |
| $image = $product->get_image( 'woocommerce_thumbnail', ['style'=> 'width: 100px;height: 100px;display: inline-block;object-fit: cover' ] ); | |
| } | |
| $product_image = wp_kses_post( apply_filters( 'woocommerce_order_item_thumbnail', $image, $item ) ); | |
| $product_name = wp_kses_post( apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ) ); | |
| $qty = $item->get_quantity(); | |
| $refunded_qty = $order->get_qty_refunded_for_item( $item_id ); | |
| $sku = wp_kses_post( ' (#' . $sku . ')' ); | |
| if ( $refunded_qty ) { | |
| $qty_display = '<del>' . esc_html( $qty ) . '</del> <ins>' . esc_html( $qty - ( $refunded_qty * -1 ) ) . '</ins>'; | |
| } else { | |
| $qty_display = esc_html( $qty ); | |
| } | |
| $item_sub_total = wp_kses_post( $order->get_formatted_line_subtotal( $item ) ); | |
| $item_sub_total = trim(strip_tags($item_sub_total)); | |
| $formatted_meta_data = $item->get_formatted_meta_data( '' ); | |
| $meta_str = ''; | |
| $t = 0; | |
| foreach ( $formatted_meta_data as $meta_id => $meta ) : | |
| if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) { | |
| continue; | |
| } | |
| if($t != 0 ){ | |
| $meta_str .= ', '; | |
| } | |
| $meta_str .= trim(strip_tags( sanitize_text_field( $meta->display_value))); | |
| $t = $t+1; | |
| endforeach; | |
| $email_obj['items'][] = [ | |
| 'name' => $product_name, | |
| 'image' => $product_image, | |
| 'qty' => $qty_display, | |
| 'price' => $item_sub_total, | |
| 'meta_str' => trim(strip_tags($meta_str)) | |
| ]; | |
| } | |
| // order pricing | |
| foreach ( $order->get_order_item_totals() as $key => $total ) { | |
| $label = trim( sanitize_text_field(strip_tags( $total['label'] ))); | |
| $value = trim( sanitize_text_field( strip_tags( $total['value'] ))); | |
| $email_obj['pricing'][$key] = [ | |
| 'name' => $label, | |
| 'value' => $value, | |
| ]; | |
| } | |
| // Iterating through order shipping items | |
| foreach( $order->get_items( 'shipping' ) as $item_id => $item ){ | |
| $item_data = $item->get_data(); | |
| $email_obj['pricing']['shipping']['name'] = $item->get_method_title(); | |
| $email_obj['pricing']['shipping']['value'] = trim(strip_tags(wc_price($item_data['total']))); | |
| } | |
| return $email_obj; | |
| } | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment