添加自定义文本字段,存储其值并在Woocommerce产品页面上显示它们

问题描述 投票:1回答:1

我正在尝试执行以下操作:在产品页面中添加至少3个自定义文本字段,以便一起显示它们或在保存时只显示一个或两个。我尝试了以下一段代码失败了。当然,数据不会被保存,也不会显示。 :(

我哪里错了?

我试图通过将几个片段混合在一起来实现我的目标:

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo '<div class="options_group">';

    // Text Field #1
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field_1', 
            'label'       => __( 'Campo aggiuntivo #1', 'woocommerce' ), 
        )
    );

    // Text Field #2
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field_2', 
            'label'       => __( 'Campo aggiuntivo #2', 'woocommerce' ), 
        )
    );

    // Text Field #3
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field_3', 
            'label'       => __( 'Campo aggiuntivo #3', 'woocommerce' ), 
        )
    );

    echo '</div>';

}

///

function woocommerce_product_custom_fields_save($post_id){

    $woocommerce_custom_product_text_field_1 = $_POST['_text_field_1'];
    if (!empty($woocommerce_custom_product_text_field))
        update_post_meta($post_id, '_custom_product_text_field_1', esc_attr($woocommerce_custom_product_text_field_1));

    $woocommerce_custom_product_text_field_2 = $_POST['_text_field_2'];
    if (!empty($woocommerce_custom_product_number_field))
        update_post_meta($post_id, '_custom_product_text_field_2', esc_attr($woocommerce_custom_product_text_field_2));

    $woocommerce_custom_product_text_field_3 = $_POST['_text_field_3'];
    if (!empty($woocommerce_custom_procut_textarea))
        update_post_meta($post_id, '_custom_product_text_field_3', esc_html($woocommerce_custom_product_text_field_3));
 }

///

add_action( 'woocommerce_before_single_product', 'custom_action', 15 );

function custom_action() {
    // Display Custom Field Value
    echo get_post_meta($post->ID, '_custom_product_text_field_1', true);
    echo get_post_meta($post->ID, '_custom_product_text_field_2', true);
    echo get_post_meta($post->ID, '_custom_product_text_field_3', true);
}
php wordpress woocommerce product custom-fields
1个回答
1
投票

我重新审视了一下你的代码...尝试以下方法:

// Admin: Add product custom text fields
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_general_settings_fields' );
function add_custom_general_settings_fields() {

    echo '<div class="options_group">';

    woocommerce_wp_text_input( array(
        'id'          => '_text_field_1',
        'label'       => __( 'Campo aggiuntivo #1', 'woocommerce' ),
    ) );

    woocommerce_wp_text_input( array(
        'id'          => '_text_field_2',
        'label'       => __( 'Campo aggiuntivo #2', 'woocommerce' ),
    ) );

    woocommerce_wp_text_input( array(
        'id'          => '_text_field_3',
        'label'       => __( 'Campo aggiuntivo #3', 'woocommerce' ),
    ) );

    echo '</div>';
}

// Admin: Save product custom text fields values
add_action( 'woocommerce_process_product_meta', 'save_custom_general_settings_fields_values', 20, 1 );
function save_custom_general_settings_fields_values($post_id){
    if ( isset($_POST['_text_field_1']) )
        update_post_meta( $post_id, '_text_field_1', sanitize_text_field($_POST['_text_field_1']) );

    if ( isset($_POST['_text_field_2']) )
        update_post_meta( $post_id, '_text_field_2', sanitize_text_field($_POST['_text_field_2']) );

    if ( isset($_POST['_text_field_3']) )
        update_post_meta( $post_id, '_text_field_3', sanitize_text_field($_POST['_text_field_3']) );
 }


// Frontend: Display custom fields values in single product pages
add_action( 'woocommerce_before_single_product', 'display_custom_fields', 15 );
function display_custom_fields() {
    global $product;

    $fields_values = array(); // Initializing

    if( $text_field_1 = $product->get_meta('_text_field_1') )
        $fields_values[] = $text_field_1; // Set the value in the array

    if( $text_field_2 = $product->get_meta('_text_field_2') )
        $fields_values[] = $text_field_2; // Set the value in the array

    if( $text_field_3 = $product->get_meta('_text_field_3') )
        $fields_values[] = $text_field_3; // Set the value in the array

    // If the array of values is not empty
    if( sizeof( $fields_values ) > 0 ){

        echo '<div>';

        // Loop through each existing custom field value
        foreach( $fields_values as $key => $value ) {
            echo '<p>' . $value . '</p>';
        }

        echo '</div>';

    }
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

但是使用woocommerce_before_single_product钩子似乎不是最好的钩子,因为这些自定义字段值显示在产品图像上方。

© www.soinside.com 2019 - 2024. All rights reserved.