phpxmlwordpresswoocommerceexport

How to exclude empty tags exporting orders as XML file?


I am using PHP code in the plugin WP ALL EXPORT PRO to filter out specific orders from WooCommerce that contain relevant products with relevant details & options and then export them via XML / FTP to my fulfillment partner.

The values for details & options are different for each product. Therefore I need a code snippet to generally exclude empty tags when exporting in the XML file (Concerning the pink marked area in overview below).

How to exclude empty tags

USED CURRENT PHP CODE. THE SECTION //add options to the output IS TO BE CONSIDERED

<?php
    function my_get_order_items( $order_id ) {
        $order = wc_get_order( absint($order_id) ); // Get the WC_Order object
    
        if ( ! is_a($order, 'WC_Order') ) {
            return false; // Exit if not an order
        }
        $order_details = ""; // Initialize variable as string to store order details
    
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            if ( ! ( strpos($item->get_name(), 'KERAMIKTASSE') !== false
            || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false
            || strpos($item->get_name(), 'SWEATSHIRT') !== false
            || strpos($item->get_name(), 'HOODIE') !== false
            || strpos($item->get_name(), 'T-SHIRT') !== false ) ) { 
                continue; 
            }
            $product    = $item->get_product(); // Get the product object
            $product_id = $item->get_product_id(); // Get the product object
    
            $order_details .= "**LT**item**GT**";
            $order_details .= "**LT**ID**GT**" . $product->get_sku() . "**LT**/ID**GT**";
            $order_details .= "**LT**produktname**GT**" . $item->get_name() . "**LT**/produktname**GT**";
            $order_details .= "**LT**amount**GT**" . $item->get_quantity() . "**LT**/amount**GT**";
            $order_details .= "**LT**upload**GT**" . maybe_serialize( get_field( 'upload', $product_id ) ) . "**LT**/upload**GT**";
            $order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product_id ) ) . "**LT**/size**GT**";
            $order_details .= "**LT**groesse**GT**" . $product->get_attribute('pa_groesse')  . "**LT**/groesse**GT**";
            $order_details .= "**LT**material**GT**" . maybe_serialize( get_field( 'material', $product_id ) ) . "**LT**/material**GT**";
            $order_details .= "**LT**print**GT**" . maybe_serialize( get_field( 'print', $product_id ) ) . "**LT**/print**GT**";
            $order_details .= "**LT**variante**GT**" . maybe_serialize( get_field( 'variante', $product_id ) ) . "**LT**/variante**GT**";
            $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product_id ) ) . "**LT**/category**GT**";
    
            //add options to the output
            $order_details .= "**LT**Options**GT**";
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . maybe_serialize( get_field( 'groupid_115', $product_id ) ) . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_115', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**";
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . maybe_serialize( get_field( 'groupid_117', $product_id ) ) . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_117', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**";
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . maybe_serialize( get_field( 'groupid_118', $product_id ) ) . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_118', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**";
            $order_details .= "**LT**/Options**GT**";
            $order_details .= "**LT**/item**GT**";
        }
        return $order_details;
    }
?>

Solution

    1. Check if each field has content before adding it to the $order_details string.

    2. Only add non-empty fields to the XML string.

    function my_get_order_items($order_id) { $order = wc_get_order(absint($order_id)); // Get the WC_Order object

    if (!is_a($order, 'WC_Order')) {
        return false; // Exit if not an order
    }
    
    $order_details = ""; // Initialize variable as string to store order details
    
    // Loop through order items
    foreach ($order->get_items() as $item) {
        if (!(strpos($item->get_name(), 'KERAMIKTASSE') !== false
            || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false
            || strpos($item->get_name(), 'SWEATSHIRT') !== false
            || strpos($item->get_name(), 'HOODIE') !== false
            || strpos($item->get_name(), 'T-SHIRT') !== false)) { 
            continue; 
        }
        
        $product = $item->get_product(); // Get the product object
        $product_id = $item->get_product_id(); // Get the product ID
    
        $order_details .= "**LT**item**GT**";
        
        // Conditionally add each field only if it's not empty
        if ($sku = $product->get_sku()) {
            $order_details .= "**LT**ID**GT**" . $sku . "**LT**/ID**GT**";
        }
        
        if ($name = $item->get_name()) {
            $order_details .= "**LT**produktname**GT**" . $name . "**LT**/produktname**GT**";
        }
    
        if ($quantity = $item->get_quantity()) {
            $order_details .= "**LT**amount**GT**" . $quantity . "**LT**/amount**GT**";
        }
        
        if ($upload = maybe_serialize(get_field('upload', $product_id))) {
            $order_details .= "**LT**upload**GT**" . $upload . "**LT**/upload**GT**";
        }
    
        if ($size = maybe_serialize(get_field('size', $product_id))) {
            $order_details .= "**LT**size**GT**" . $size . "**LT**/size**GT**";
        }
    
        if ($groesse = $product->get_attribute('pa_groesse')) {
            $order_details .= "**LT**groesse**GT**" . $groesse . "**LT**/groesse**GT**";
        }
    
        if ($material = maybe_serialize(get_field('material', $product_id))) {
            $order_details .= "**LT**material**GT**" . $material . "**LT**/material**GT**";
        }
    
        if ($print = maybe_serialize(get_field('print', $product_id))) {
            $order_details .= "**LT**print**GT**" . $print . "**LT**/print**GT**";
        }
    
        if ($variante = maybe_serialize(get_field('variante', $product_id))) {
            $order_details .= "**LT**variante**GT**" . $variante . "**LT**/variante**GT**";
        }
    
        if ($category = maybe_serialize(get_field('category', $product_id))) {
            $order_details .= "**LT**category**GT**" . $category . "**LT**/category**GT**";
        }
        
        // Add options conditionally
        $order_details .= "**LT**Options**GT**";
        
        if ($groupid_115 = maybe_serialize(get_field('groupid_115', $product_id))) {
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_115 . "**LT**/ID**GT**";
            $order_details .= "**LT**Value**GT**" . maybe_serialize(get_field('value_115', $product_id)) . "**LT**/Value**GT****LT**/Option**GT**";
        }
        
        if ($groupid_117 = maybe_serialize(get_field('groupid_117', $product_id))) {
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_117 . "**LT**/ID**GT**";
            $order_details .= "**LT**Value**GT**" . maybe_serialize(get_field('value_117', $product_id)) . "**LT**/Value**GT****LT**/Option**GT**";
        }
        
        if ($groupid_118 = maybe_serialize(get_field('groupid_118', $product_id))) {
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_118 . "**LT**/ID**GT**";
            $order_details .= "**LT**Value**GT**" . maybe_serialize(get_field('value_118', $product_id)) . "**LT**/Value**GT****LT**/Option**GT**";
        }
        
        $order_details .= "**LT**/Options**GT**";
        $order_details .= "**LT**/item**GT**";
    }
    
    return $order_details;
    

    } ?>

    Each field is only added if it contains data (i.e., it’s not empty). This prevents empty XML tags from being included in the exported XML.

    Each option group (e.g., groupid_115, groupid_117, groupid_118) is only added if it has content.

    This approach ensures only non-empty tags are included in the final XML output.