What you need I think is the following code:
$get_order = wc_get_order( $order_id );
$items = $get_order->get_items();
foreach ( $items as $item ) {
$product = $item->get_product();
//check if this is a variation using is_type
if ( 'variation' === $product->get_type() ) {
$variation_id = $item->get_variation_id();
$variation = new WC_Product_Variation( $variation_id );
$attributes = $variation->get_attributes();
foreach ( $attributes as $key => $value ) {
if ( 'pa_my-custom-variation' === $key ) {
// whatever you want to do next
}
}
}
}
What it does it to look into each item from the order and check if it is actually a variable one. If it is, then it will get its variation ID and from there it will get the variation attributes and you can loop through each of them to find the one you need.
Here is also another way to check if the an item is variable. This way you are getting the variation ID if there is one.
$variation_id = $item->get_variation_id();
if(! empty($variation_id) {
// item is variable and we can check its variation
}
I know this is an old question but hope this might be useful for anybody who drops here.