Programatically added attribute, set to ‘show on product page’ automatically. Woocommerce [closed]

It actually doesn’t matter because WooCommerce uses this, to display:

$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );

However, if you don’t set the display to true, you’d have to add this code to your template files. You might have to do other things as well (styling, etc). If you just set the display to true, you don’t have to do anything. WooCommerce will take care of it.

this is how WooCommerce does it, roughly (/includes/class-wc-ajax.php):

wp_set_object_terms( $product_id, $values, $attribute_name);

// get existing attributes

$attributes = get_post_meta( $product_id, '_product_attributes' );

$attributes[ sanitize_title( $attribute_name ) ] = array(
        'name'          => wc_clean( $attribute_name ),
        'value'         => $values,
        'position'      => $attribute_position, // the order in which it is displayed
        'is_visible'    => $is_visible, // this is the one you wanted, set to true
        'is_variation'  => $is_variation, // set to true if it will be used for variations
        'is_taxonomy'   => $is_taxonomy // set to true
);


// this part sorts the attributes before saving based on the position value 
// this is defined by WooCommerce, but if your import runs before WooCommerce is loaded, this function won't be available
if ( ! function_exists( 'attributes_cmp' ) ) {
    function attributes_cmp( $a, $b ) {
        if ( $a['position'] == $b['position'] ) {
            return 0;
        }

        return ( $a['position'] < $b['position'] ) ? -1 : 1;
    }
}

uasort( $attributes, 'attributes_cmp' );

// update it back
update_post_meta( $post_id, '_product_attributes', $attributes );