使用 WooCommerce 将变体选项发送到条带化作为元数据

Send Variation options to stripe as metadata with WooCommerce

我正在使用此代码将产品数据作为元数据发送到 stripe。

function filter_wc_stripe_payment_metadata( $metadata, $order, $source ) {

    $count = 1;

    foreach( $order->get_items() as $item_id => $line_item ){
        $item_data = $line_item->get_data();
        $product = $line_item->get_product();
        $location = $product->get_attribute('location');
        $product_name = $product->get_name();
        $item_quantity = $line_item->get_quantity();
        $item_total = $line_item->get_total();
        $metadata['Item '.$count] = '';
        $count += 1;
    }

    return $metadata;
}

add_filter( 'wc_stripe_payment_metadata', 'filter_wc_stripe_payment_metadata', 10, 3 );

如何获取产品的选定变体并将其添加到元数据中?

$attribute = $product->get_attribute('location'); 由于某种原因对我不起作用。所以我使用了这个到目前为止效果很好。

foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
    $value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
    $strings[] = $value;
}

$attribute = $strings[1];
$attribute = str_ireplace('<p>','',$attribute);
$attribute = str_ireplace('</p>','',$attribute);  

$metadata['Location'] = $attribute;