/*
Theme Name: Flatsome Child
Description: This is a child theme for Flatsome Theme
Author: UX Themes
Template: flatsome
Version: 3.0
*/

/*************** ADD CUSTOM CSS HERE.   ***************/


@media only screen and (max-width: 48em) {
/*************** ADD MOBILE ONLY CSS HERE  ***************/


}

function show_recent_shipments() {
    $args = array(
        'post_type' => 'shop_order',
        'post_status' => array('wc-completed', 'wc-shipped'),
        'posts_per_page' => 5,
    );

    $orders = get_posts($args);
    $output = '<div class="shipment-feed">';
    foreach ($orders as $order_post) {
        $order = wc_get_order($order_post->ID);
        $tracking_items = $order->get_meta('ast_tracking_items');
        if ($tracking_items && is_array($tracking_items)) {
            foreach ($tracking_items as $track) {
                $city = $order->get_billing_city();
                $product_names = [];
                foreach ($order->get_items() as $item) {
                    $product_names[] = $item->get_name();
                }
                $output .= "<p>" . implode(', ', $product_names) . " shipped to $city via " . $track['tracking_provider'] . " — Tracking #: " . $track['tracking_number'] . "</p>";
            }
        }
    }
    $output .= '</div>';
    return $output;
}
add_shortcode('recent_shipments', 'show_recent_shipments');
