获取产品属性名称时出错。首先,我们必须获得所有选项ID&;属性名称,而我们必须通过其id获取属性名称(&;属性slug。
请检查下面更新的代码片段,并让我知道这是否适用于您。如果没有,请告诉我,我一定会帮助你。谢谢
function add_product_column( $columns ) {
//add column
$columns[\'new_column\'] = __( \'New column\', \'woocommerce\' );
return $columns;
}
add_filter( \'manage_edit-product_columns\', \'add_product_column\', 10, 1 );
function add_product_column_content( $column, $postid ) {
if ( $column == \'new_column\' ) {
$product = wc_get_product( $postid );
$product_attributes = $product->get_attributes();
$attr_array = array();
foreach ( $product_attributes as $product_attribute ) {
$attribute_name = $product_attribute->get_name();
$attribute_options = $product_attribute->get_options();
foreach ($attribute_options as $key => $value) {
$term_data = get_term_by( \'id\',$value,$attribute_name );
$term_name = $term_data->name;
array_push($attr_array, $term_name);
}
}
echo implode(\', \', $attr_array);
}
}
add_action( \'manage_product_posts_custom_column\', \'add_product_column_content\', 10, 2 );