WooCommerce:从产品变体中获取自定义字段并将其显示在“附加信息区域"上;

WooCommerce: Get custom field from product variations and display it on the quot;additional information areaquot;(WooCommerce:从产品变体中获取自定义字段并将其显示在“附加信息区域上;)
本文介绍了WooCommerce:从产品变体中获取自定义字段并将其显示在“附加信息区域"上;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试向产品变体添加自定义字段,并在附加信息区域"中显示产品的自定义字段值.

I'm trying to add a custom field to product variations, and display the custom field value on products in the "additional information area".

我正在使用 "WooCommerce:为每个产品添加自定义字段变化".

自定义字段工作正常,但我无法在产品页面上显示该值.

The custom field is working fine, but I cannot seam to display the value on the product page.

这是我目前所拥有的:

// 1. Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'Add_custom_field_to_variations', 10, 3 );
function Add_custom_field_to_variations( $loop, $variation_data, $variation ) {  
    woocommerce_wp_text_input( array(
        'id' => 'custom_field[' . $loop . ']',
        'class' => 'short',
        'label' => __( 'Custom Field', 'woocommerce' ),
        'value' => get_post_meta( $variation->ID, 'custom_field', true ) 
    ));
}

// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'Save_custom_field_variations', 10, 2 );
function Save_custom_field_variations( $variation_id, $i ) {
    $custom_field = $_POST['custom_field'][$i];
    if ( isset( $custom_field ) ) {
        update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
    }
}

// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'Add_custom_field_variation_data' );
function Add_custom_field_variation_data( $variations ) {  
    $variations['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';

    return $variations;
}

// 4. Display custom field on the additional information area
function Display_product_attributes2($product_attributes, $variations){
    $product_attributes['custom_field'] = [
        'label' => __('custom', 'text-domain'),
        'value' => get_post_meta( $variation_id, '_custom_field', true ),
    ];
    return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'Display_product_attributes2', 10, 2);

推荐答案

At 'value' =>get_post_meta( $variation_id, '_custom_field', true ) 你使用了 $variation_id 而这没有定义.

At 'value' => get_post_meta( $variation_id, '_custom_field', true ) you make use of the $variation_id while this is not defined.

因此您可以使用 foreach 循环和 $variations->get_children(); 代替添加自定义标签 &附加信息区"中的值

So you can make use a foreach loop and $variations->get_children(); instead to add a custom label & value in the "additional information area"

// 4. Display custom field on the additional information area
function display_product_attributes( $product_attributes, $variations ) {
    // Get childIDs in an array
    $children_ids = $variations->get_children();

    foreach ( $children_ids as $child_id ) {
        $value = get_post_meta( $child_id, 'custom_field', true );

        // True
        if ( $value ) {
            // rows
            $product_attributes[ 'custom_field ' . $child_id ] = array(
                'label' => __('custom', 'woocommerce'),
                'value' => $value,
            );
        }
    }

    return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'display_product_attributes', 10, 2);

这篇关于WooCommerce:从产品变体中获取自定义字段并将其显示在“附加信息区域"上;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)