Unsure why undefined variable notice trying to get product 'id' when adding ACF fields to custom column?

Viewed 113

I have added a custom column to WooCommerce Products page in Admin Dashboard, however when trying to populate column with adding 2 custom ACF fields setup for Post Type -> Products, I am receiving error notice on debug listed on every product in that custom column.

Notice: Undefined variable: product in /./././wp-content/themes/bpa/functions.php on line 923

Notice: Trying to get product 'id' of non-object in /./././wp-content/themes/bpa/functions.php on line 923

Could anyone please help explain what I'm doing wrong to get undefined variable?

Line 923 is :

        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

Is it because I'm using an old approach for WC or something?

Full Function:

// Populate column
function woo_product_rmreference_column_data( $column ) {
    global $post;

    if ( $column == 'rm_reference' ) {
            
        // 
            $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
        
            // Get ACF Fields
            $reference = get_field( 'property_reference', $product_id );
            $address = get_field( 'location', $product_id );

            // Output
            echo ($reference . $address) ? '<div>'.$reference.' - '.$address.'</div>' : '<div>Not found!</div>';
    }
}
add_action( 'manage_product_posts_custom_column' , 'woo_product_rmreference_column_data', 10, 2 );
1 Answers

For anyone viewing. If using $product and $post, then you need to use global $product; and global $post to get access to them.

global $product;

$product_id = $product->get_id();
Related