【问题标题】:Add custom text fields, store their values and display them on Woocommerce product pages添加自定义文本字段,存储它们的值并在 Woocommerce 产品页面上显示它们
【发布时间】:2018-09-04 22:15:16
【问题描述】:

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

我哪里错了?

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

// 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】:

    我重新访问了您的代码...尝试以下操作:

    // 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 钩子似乎不是最好的钩子,因为这些自定义字段值显示在产品图片上方。

    【讨论】:

    • 感谢@LoicTheAztec 的建议,效果很好!我想知道如何通过一次迭代一个来打印自定义字段值。一种:foreach ( $product-&gt;get_meta('_text_field_x') as $text_field){ echo '&lt;div&gt;&lt;p&gt;' . $text_field_x . '&lt;/p&gt;&lt;/div&gt;'; }我怎样才能做到这一点?
    • 不能正确地将所有字段保存在一个元数据中。我需要分开的。我可能不会同时管理所有三个领域,而只管理其中的一两个。所以我在想一个循环来管理它。这些字段可以像_text_field[0] 一样调用吗?
    • 我需要将这 3 个值包装到一个 div 中,类似于

      _text_field_1

      ,所以我想知道是否有机会展示一个,两个或三个文本字段值仅在一个 if 语句中,而不是三个。
    • @alemarengo 我现在刚刚更新了最后一个函数代码......我想如果我很好理解的话,我想你想要得到。
    • 太完美了!我必须更多地练习循环。感谢您的帮助!
    猜你喜欢
    • 2018-06-12
    • 2021-01-31
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多