将自定义字段添加到产品快速编辑 - woocommerce

时间:2021-02-26 21:19:33

标签: woocommerce

我想通过产品快速编辑显示和更新我的自定义字段。 自定义字段在正常编辑中工作。它在快速编辑中显示(但为空且在快速编辑中不更新)

这就是我所拥有的:

// 1. Add custom field field input @ product edit page
  
add_action( 'woocommerce_product_options_stock', 'add_My_custum_field' );      
function add_My_custum_field() {          
        woocommerce_wp_text_input( array( 
            'id' => 'My_custum_field', 
            'class' => 'short', 
           'label' => __( 'My_custum_field', 'woocommerce' ),
            'data_type' => 'text', 
        ));      
    }
  
//add to quick edit
add_action( 'woocommerce_product_quick_edit_end', 'add_My_custum_field' );  

// -----------------------------------------
// 2. Save custom field 
  
add_action( 'save_post_product', 'save_My_custum_field' );
function save_My_custum_field( $product_id ) {
    global $pagenow, $typenow;
    if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    if ( isset( $_POST['My_custum_field'] ) ) {
    if ( $_POST['My_custum_field'] )
        update_post_meta( $product_id, 'My_custum_field', $_POST['My_custum_field'] );
    } else delete_post_meta( $product_id, 'My_custum_field' );
}

0 个答案:

没有答案