如何在Woocommerce 3.1中添加自定义字段

时间:2017-07-11 03:21:35

标签: wordpress woocommerce

我尝试了很多方法来制作自定义字段 在woocommerce 3.1中,但这段代码对我没用。

任何人都可以帮我弄清楚如何在下面的链接中添加这样的自定义字段 http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

祝你好运

1 个答案:

答案 0 :(得分:1)

在使用woocommerce_product_options_general_product_data操作挂钩

的“产品常规”标签中添加自定义字段

add_action('woocommerce_product_options_general_product_data','woo_add_custom_general_fields');

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field', 
            'label'       => __( 'My Text Field', 'woocommerce' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ) 
        )
    );

}

现在使用woocommerce_process_product_meta

将此字段保存到基于数据的位置
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
    $woocommerce_text_field = $_POST['_text_field'];
    if( !empty( $woocommerce_text_field ) )
        update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );

}
我现在你明白了。它与WooCommerce 3.1一起使用

相关问题