php - Wordpress WooCommerce 文本属性

标签 php wordpress woocommerce

我在 Wordpress 中使用 WooCommerce 插件。在该插件中,我需要为每个产品添加文本输入,客户可以在其中填写其他产品详细信息。

我面临的问题是,每当我添加文本属性时,它都会变成一个选择框。请提出一些解决方案。谢谢!

最佳答案

您的意思是您的客户应该在前端提供此文本输入字段吗?

如果它只是后端,您可以将其添加到您的 functions.php 中:

if (is_admin()){     
// Add the Meta Box  
function add_textfield() {  
    add_meta_box(  
        'textfield', // $id  
        'Custom textfield', // $title   
        'show_textfield_backend', // $callback  
        'product', // $page  
        'normal', // $context  
        'high'  // $priority
    ); 
}  
add_action('add_meta_boxes', 'add_textfield');

// The Callback  
function show_textfield_backend() {  
    global $post;  
    global $wpdb;

    $meta = get_post_meta($post->ID, 'textfield', true);

    // Use nonce for verification  
    echo '<input type="hidden" name="textfield_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  

    _e('Custom textfield: ');
    echo '<input type="text" placeholder="text" id="textfield" class="textfield" name="textfield" ', $meta ? " value='$meta'" : "",' />';       
}

// Save the Data  
function save_textfield($post_id) {  
    global $wpdb;

    // verify nonce  
    if (!wp_verify_nonce($_POST['textfield_nonce'], basename(__FILE__)))   
        return $post_id;
    // check autosave  
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $post_id;
    // check permissions
    if ('page' == $_POST['post_type']) {  
        if (!current_user_can('edit_page', $post_id))  
            return $post_id;  
    } elseif (!current_user_can('edit_post', $post_id)) {  
        return $post_id;  
    }

    if ( !wp_is_post_revision( $post_id ) ) {

        $textfield = $_POST['textfield'];
        update_post_meta($post_id, 'textfield', $textfield);

    }  
}
add_action('save_post', 'save_textfield');
}

关于php - Wordpress WooCommerce 文本属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14853676/

相关文章:

wordpress - 通过 "woocommerce_init" Hook 访问已应用的优惠券数据

php - 通过前端(HTML、CSS、PHP、SQL)编辑信息和更新数据库

mysql - 为什么在服务器上找不到中文永久链接?

jquery - 将另一个不同的类附加到一组相似的类

php - 微软azure开发的wordpress网站自定义页面将非www重定向到www的错误

php - WooCommerce API : Create order with meta data on line item

php - 所有用户根据其发出的交易获得佣金

php - 重定向功能 Php

php - 新版本的 php/mysql 在 GROUP BY 子句中出现聚合错误

wordpress - WooCommerce 订单按日期状态已完成