【问题标题】:Display all the variants with custom data in WooCommerce additional informations tab in a single table row在单个表格行中显示 WooCommerce 附加信息选项卡中具有自定义数据的所有变体
【发布时间】:2021-02-06 06:23:46
【问题描述】:

我有一个产品有多个变体,我想在附加信息选项卡中显示所有变体(不是属性)及其相关 SKU,如下所示:

基于Display in additional information tab, some product settings custom fields values我将代码添加到我的产品的信息选项卡中:

$custom_field1 应该是属性名称 + SKU 和 GTIN(由此插件添加)的结果:

注意事项:

  1. 简单的产品应该不受影响
  2. 结果应该只是现有表的额外行,或者可能替换“属性”行。
  3. GTIN 代码是产品的私有自定义字段,meta_key _wpm_gtin_code,如果你有产品对象,你可以通过这种方式获取 GTIN 代码:$gtin = $product->get_meta('_wpm_gtin_code');

【问题讨论】:

  • 一些问题:1) 它涉及具有变体的产品,简单的产品应该如何处理? 2) 您打算替换选项卡的全部内容、添加一个额外的表格还是在现有表格中添加一个额外的行? 3) 对于不使用该插件的用户,GTIN 的元密钥存储在哪里以及如何存储?
  • @7uc1f3r 1) 简单产品不受影响 2) 向现有表添加额外行或可能替换“属性”行 3) GTIN 代码是产品的私有自定义字段,带有 meta_key _wpm_gtin_code,如果你有产品对象可以通过这种方式获取GTIN码:$gtin = $product->get_meta('_wpm_gtin_code');

标签: php wordpress woocommerce tabs product


【解决方案1】:

您可以使用以下内容,它会添加额外的行。通过代码中添加的注释标签进行解释。

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] );
    }
}

这两段代码都可以应用在我上面的答案中

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 2018-09-12
    • 1970-01-01
    • 2021-02-03
    • 2022-06-29
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    相关资源
    最近更新 更多