您可以使用以下内容,它会添加额外的行。通过代码中添加的注释标签进行解释。
function filter_woocommerce_display_product_attributes( $product_attributes, $product ) {
// Variable product
if ( $product->is_type('variable' ) ) {
// Get childIDs in an array
$children_ids = $product->get_children();
// Output
$output = '';
// Loop
foreach ( $children_ids as $child_id ) {
// Get product
$variation = wc_get_product( $child_id );
// Get variation name
$variation_name = ( $name = implode( ' / ', $variation->get_variation_attributes() ) ) ? $name : esc_html__( 'N/A', 'woocommerce' );
// Get product SKU
$get_sku = ( $sku = $variation->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );
// Get GTIN code
$gtin_value = ( $gtin = $variation->get_meta( '_wpm_gtin_code' ) ) ? $gtin : esc_html__( 'N/A', 'woocommerce' );
// Concatenate string
$output .= '<p>' . $variation_name . ' - EAN: ' . $get_sku . ' GTIN: ' . $gtin_value . '</p>';
}
// Add
$product_attributes[ 'custom_field' ] = array(
'label' => __( 'Custom field', 'woocommerce' ),
'value' => $output,
);
}
return $product_attributes;
}
add_filter( 'woocommerce_display_product_attributes', 'filter_woocommerce_display_product_attributes', 10, 2 );
结果:(GTIN 不可用,因为我不使用那个插件)
相关:
其他问题
要删除表格行,您可以简单地使用:
unset ( $product_attributes['weight'] );
但它也可以是我们将通过这段代码指示我们要保留哪些表行的另一种方式
// For debugging purposes - uncomment if needed
//echo '<pre>', print_r( $product_attributes, 1 ), '</pre>';
// Add the rows you want to keep, you can add multiple rows, separated by a comma
$rows_to_keep = array ( 'weight', 'dimensions' );
// Loop
foreach ( $product_attributes as $key => $product_attribute ) {
// Not in array 'rows to keep'
if ( ! in_array ( $key, $rows_to_keep ) ) {
// Remove
unset ( $product_attributes[$key] );
}
}
这两段代码都可以应用在我上面的答案中