Okay
  Public Ticket #4089801
Exporting orders with the seat ID and discount name
Open

Comments

  • John started the conversation

    Hi,

    How can we export the orders including both the seat ID. and discount name?

    I've tried with both via WC analytics and with the free "Advanced order export" plugin https://wordpress.org/plugins/woo-order-export-lite/ that let's you add custom filters. 

    All the best,

    /John

  •  938
    Zhivko replied

    Hi,

    I'm not sure if WC Analytics can currently display this meta.

    I checked the Advanced Order Export plugin and it looks like it can be set up to include these values.

    The problem is that seat data is serialized, so it's hard to read.

    What we can do is save the needed data as plain string meta keys during order creation. That way, the Advanced Order Export plugin can access them easily.

    Instructions:

    Open the Seat Planner main file found in

    wp-content/plugins/stachethemes-seat-planner/stachethemes-seat-planner.php

    At the bottom of the file insert this code:

    // Store seat id and discount info separately in order items
    function checkout_order_processed_custom($order) {
        if (!$order->get_meta('has_auditorium_product')) {
            return;
        }
    
        foreach ($order->get_items() as $item) {
            $seat_data = (array) $item->get_meta('seat_data');
    
            if (empty($seat_data) || empty($seat_data['seatId'])) {
                continue;
            }
    
            // Store seat ID in a separate meta field
            $item->update_meta_data('_seat_id', $seat_data['seatId']);
    
            // Process seat discount if available
            $seat_discount = $item->get_meta('seat_discount');
            if ($seat_discount) {
                $seat_discount = (array) $seat_discount;
                $item->update_meta_data('_seat_discount_name', $seat_discount['name'] ?? '');
                $item->update_meta_data('_seat_discount_value', $seat_discount['value'] ?? 0);
                $item->update_meta_data('_seat_discount_type', $seat_discount['type'] ?? 'fixed');
            }
    
            $item->save_meta_data();
        }
    }
    
    add_action('woocommerce_checkout_order_created', '\Stachethemes\SeatPlanner\checkout_order_processed_custom', 10, 1);
    add_action('woocommerce_store_api_checkout_order_processed', '\Stachethemes\SeatPlanner\checkout_order_processed_custom', 10, 1);
    
    // Hide custom meta fields from the order item meta display in the admin area
    add_filter('woocommerce_hidden_order_itemmeta', function($exclude_array) {
        $exclude_array[] = '_seat_id';
        $exclude_array[] = '_seat_discount_name';
        $exclude_array[] = '_seat_discount_value';
        $exclude_array[] = '_seat_discount_type';
        return $exclude_array;
    }, 10, 1);
    

    This code creates meta keys with plain string values for the seat id and discount data.

    With this code added your newly created orders will have these extra meta keys:

    • _seat_id
    • _seat_discount_name
    • _seat_discount_value
    • _seat_discount_type

    So now you can go to the Export Orders page of the "Advanced Order Export" plugin and set it up.

    Click on the "Setup fields to export" at the bottom to expand the settings:

    2020101998.png

    On the right side select "Product order items" 

    6929751708.png

    Click on "Add field"

    8819118157.png

    And add the _seat_id field like this:

    2469028719.png

    Click "Confirm" and you should see the field here:

    8832025559.png

    Drag it to the left column to include it in the export data.

    You can do the same for the discount fields.

    • _seat_discount_name
    • _seat_discount_value
    • _seat_discount_type

     


    Stachethemes Developer

  • John replied

    Thank you for the code and thorough step by step instructions. I added the meta keys, however they were not available in the drop down selector as in your screenshot, and needed to be added manually.

    I guess this will only work after a new order has been placed. 

    Is there a way to generate the new meta for already placed orders?

    Best,
    /John

    Attached files:  _seat_id.jpg

  •  938
    Zhivko replied

    Yes, this will work for your next orders only.

    You can run a php code that will modify the existing orders as well:

    Below the other code I sent you place this one:

    // Update existing orders meta
    // This fn can be removed once executed.
    add_action('init', function () {
    
        $should_run = $_GET['seat_order_update_meta'] ?? false;
    
        if (!$should_run) {
            return;
        }
    
        // Get orders with auditorium products
        $args = [
            'limit'      => -1,
            'status'     => 'any',
            'orderby'    => 'date',
            'order'      => 'DESC',
            'return'     => 'ids',
            'meta_key'   => 'has_auditorium_product',
            'meta_value' => '1'
        ];
    
        $order_ids = wc_get_orders($args);
    
        if (empty($order_ids)) {
            return;
        }
    
        foreach ($order_ids as $order_id) {
            $order = wc_get_order($order_id);
            
            // Validate order
            if (!$order || !$order->get_meta('has_auditorium_product')) {
                continue;
            }
            
            foreach ($order->get_items() as $item) {
                $seat_data = (array) $item->get_meta('seat_data');
                
                // Skip if no seat data or meta already exists
                if (empty($seat_data) || $item->get_meta('_seat_id')) {
                    continue;
                }
                
                // Extract seat information
                $seat_id = $seat_data['seatId'] ?? '';
                $seat_discount = (array) $item->get_meta('seat_discount');
                
                // Update meta data
                $item->update_meta_data('_seat_id', $seat_id);
                $item->update_meta_data('_seat_discount_name', $seat_discount['name'] ?? '');
                $item->update_meta_data('_seat_discount_value', $seat_discount['value'] ?? 0);  
                $item->update_meta_data('_seat_discount_type', $seat_discount['type'] ?? 'fixed');
                $item->save_meta_data();
            }
        }
    
        echo 'Done';
        exit;
    });

    And then you can run this function from the front-end by adding ?seat_order_update_meta=1 to the website url.

    E.g. https://my-website.com/?seat_order_update_meta=1

    If executed you should see a blank page with "Done" text.

    Remove this code once the function is executed.


    Stachethemes Developer

  • John replied

    Thanks again for your responsiveness Zhivko. 

    This worked great.

    Best,
    /John

  •  938
    Zhivko replied

    Great! I'm glad the solution worked. You can now remove the last code I sent.

    Regarding the first code: keep in mind that if you update the plugin, you’ll lose the custom hooks since I suggested adding them directly to the plugin's main file for simplicity.

    To make them update-safe, you can move the hooks to your child theme's functions.php file. Just remember that once you do this, the code won't be in the plugin's namespace anymore ( it will be in the global scope ), so you’ll need to remove the plugin namespace prefix from the add_action hooks like this:

    add_action('woocommerce_checkout_order_created', 'checkout_order_processed_custom', 10, 1);
    add_action('woocommerce_store_api_checkout_order_processed', 'checkout_order_processed_custom', 10, 1);
    

    Even better if you use some Code Snippets plugin you can insert the code as a PHP Snippet.


    Stachethemes Developer

  • John replied

    Updated and safe. Thanks again for this solution.

    /John