其实这并不重要,因为WooCommerce使用它来显示:
$values = wc_get_product_terms( $product->id, $attribute[\'name\'], array( \'fields\' => \'names\' ) );
但是,如果不将显示设置为true,则必须将此代码添加到模板文件中。您可能还需要做其他事情(造型等)。如果将显示设置为true,则无需执行任何操作。WooCommerce会处理好的。
WooCommerce大致就是这样做的(/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 );