Search code examples
phpwordpresswoocommercestripe-paymentsproduct-variations

Send Variation options to stripe as metadata with WooCommerce


I am using this code to send product data to stripe as metadata.

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 );

How can I get the selected variation of the product and add it to the metadata?


Solution

  • $attribute = $product->get_attribute('location'); didn't work for me for some reason. So I used this which works well so far.

    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;