在单个 table 行的 WooCommerce 附加信息选项卡中显示所有带有自定义数据的变体
Display all the variants with custom data in WooCommerce additional informations tab in a single table row
我有一个产品有多个变体,我想在附加信息选项卡中显示所有变体(不是属性)及其相关 SKU,如下所示:
基于 我将代码添加到我的产品的信息选项卡中:
其中 $custom_field1
应该是属性名称 + SKU 和 GTIN(由该插件添加)的结果:
备注:
- 简单的产品应该不受影响
- 结果应该只是现有 table 的额外一行,或者可能替换“属性”行。
- GTIN码是meta_key_wpm_gtin_code产品的私有自定义字段,如果您有产品对象可以通过以下方式获取GTIN码:
$gtin = $product->get_meta('_wpm_gtin_code');
您可以使用以下内容,它会添加额外的行。通过代码中添加的注释标签进行解释。
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 不可用,因为我不使用该插件)
相关:
补充问题
要删除 table 行,您可以简单地使用:
unset ( $product_attributes['weight'] );
但它也可以是另一种方式,我们将通过这段代码指示我们要保留哪些 table 行
// 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] );
}
}
这两段代码都可以应用在我上面的回答中
我有一个产品有多个变体,我想在附加信息选项卡中显示所有变体(不是属性)及其相关 SKU,如下所示:
基于
其中 $custom_field1
应该是属性名称 + SKU 和 GTIN(由该插件添加)的结果:
备注:
- 简单的产品应该不受影响
- 结果应该只是现有 table 的额外一行,或者可能替换“属性”行。
- GTIN码是meta_key_wpm_gtin_code产品的私有自定义字段,如果您有产品对象可以通过以下方式获取GTIN码:
$gtin = $product->get_meta('_wpm_gtin_code');
您可以使用以下内容,它会添加额外的行。通过代码中添加的注释标签进行解释。
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 不可用,因为我不使用该插件)
相关:
补充问题
要删除 table 行,您可以简单地使用:
unset ( $product_attributes['weight'] );
但它也可以是另一种方式,我们将通过这段代码指示我们要保留哪些 table 行
// 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] );
}
}
这两段代码都可以应用在我上面的回答中