PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to add ACF field to custom column on WooCommerce admin orders list

 May 09, 2022     advanced-custom-fields, orders, product, woocommerce, wordpress     No comments   

Issue

ACF is set up for post type on WooCommerce products. However, I am trying to add a custom column to WooCommerce orders list within Admin dashboard and add the products ACF field.

I have added the column to display after order_status, but I'm having problems getting the ACF field to display.

// ADD NEW COLUMN
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
    $reordered_columns = array();
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            $reordered_columns['my-column'] = __( 'Location','theme_domain');
        }
    }
    return $reordered_columns;
}

Here, adding ACF to new colum.

// ADD ACF FIELD TO COLUMN
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
     if ( 'Location' == $column_name ){
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    echo get_field( 'location', $product_id );
        }
    return true;
}

Still learning and not sure how to do this, any advice?


Solution

An order generally consists of several products, therefore you cannot use $product_id directly, but you have to loop through the order items.

So you get:

/**
 * Add columns
 */
function filter_manage_edit_shop_order_columns( $columns ) {
    $reordered_columns = array();
    
    foreach ( $columns as $key => $column ) {
        $reordered_columns[$key] = $column;
        
        if ( $key ==  'order_status' ) {
            $reordered_columns['my-column'] = __( 'Location','theme_domain' );
        }
    }
    
    return $reordered_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

/**
 * Populate columns
 */
function filter_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'my-column' ) {
        // Get order
        $order = wc_get_order( $post_id );

        // Is a WC_Order
        if ( is_a( $order, 'WC_Order' ) ) {
            // Get items
            $items = $order->get_items();
            
            // Loop through
            foreach ( $items as $key => $item ) {
                // Product ID
                $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
                
                // Get field
                $address = get_field( 'location', $product_id );
                
                // Output
                echo ($address) ? '<div>Address: ' . $address . '</div>' : '<div>Address: No address found!</div>';
            }
        }
    }
}
add_filter( 'manage_shop_order_posts_custom_column', 'filter_manage_shop_order_posts_custom_column', 10, 2 );


Answered By - 7uc1f3r
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing