• Hi,

    I want to create a custom column for the “Important Note” on Woocommece Order page. The code now combine the Important note to Shipping Column. How can I create “Important Note” own column?
    Here’s the code.

    add_action('add_meta_boxes',function(){
        add_meta_box('greenbox_important_order_notes', 'Important Order Note (displayed only to admin)', 'greenbox_important_order_note_callback','shop_order','normal','high');
    });
    
    function greenbox_important_order_note_callback($post){
        	// Add a nonce field so we can check for it later.
    	wp_nonce_field( 'greenbox_important_order_note_meta_box', 'greenbox_important_order_note_meta_box_nonce' );
    
    	/*
    	 * Use get_post_meta() to retrieve an existing value
    	 * from the database and use the value for the form.
    	 */
    	$value = get_post_meta( $post->ID, '_greenbox_important_note', true );
    
    	echo '<label for="greenbox_important_order_notes">';
    	echo 'Note:';
    	echo '</label> ';
    	echo '<input type="text" id="greenbox_important_order_notes" name="greenbox_important_order_note" value="' . esc_attr( $value ) . '" style="width:80%; border: 1px solid green; " />';
    }
    
    function greenbox_save_meta_box_data_important_note( $post_id ) {
    
    	/*
    	 * We need to verify this came from our screen and with proper authorization,
    	 * because the save_post action can be triggered at other times.
    	 */
    
    	// Check if our nonce is set.
    	if ( ! isset( $_POST['greenbox_important_order_note_meta_box_nonce'] ) ) {
    		return;
    	}
    
    	// Verify that the nonce is valid.
    	if ( ! wp_verify_nonce( $_POST['greenbox_important_order_note_meta_box_nonce'], 'greenbox_important_order_note_meta_box' ) ) {
    		return;
    	}
    
    	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    		return;
    	}
    
    	// Check the user's permissions.
    	if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
                // its ok
            }
            else{
                return; // its not ok
            }
    
    
    
    	/* OK, it's safe for us to save the data now. */
    	
    	// Make sure that it is set.
    	if ( ! isset( $_POST['greenbox_important_order_note'] ) ) {
    		return;
    	}
    
    	// Sanitize user input.
    	$my_data = sanitize_text_field( $_POST['greenbox_important_order_note'] );
    
    	// Update the meta field in the database.
    	update_post_meta( $post_id, '_greenbox_important_note', $my_data );
    }
    add_action( 'save_post', 'greenbox_save_meta_box_data_important_note' );
    
    
    function greenbox_order_extra_columns_content($column) {
        global $post;
    
        $order_id = $post->ID;
            
        //get_post_meta( $order_id, $key);
    
        /* show all meta 
        $meta = get_post_meta($order_id);
        $greenbox_comment_for_order="";
        foreach ($meta as $k=>$v){
         $greenbox_comment_for_order .= "<br>k = $k , v = $v ";   
        }*/
        $greenbox_note_for_order = trim(get_post_meta( $post->ID, '_greenbox_important_note', true ));
        
        switch ($column) {
            //case "order_notes":
            case 'important-notes':
                
                if(isset($greenbox_note_for_order) && !empty($greenbox_note_for_order))
                    echo "<div style='background-color: red; color: white; padding: 2px;'> $greenbox_note_for_order </div>";  //$list_of_order_notes; 
                
                break;
        }
    }
    
    add_action("manage_shop_order_posts_custom_column", "greenbox_order_extra_columns_content");
  • The topic ‘Custom Column on Woocommerce Order Page’ is closed to new replies.