Home  >  Q&A  >  body text

Not sure why undefined variable notification trying to get product 'id'; when adding ACF field to custom column?

I have added a custom column in my WooCommerce product page in the admin dashboard, but when trying to populate the column by adding 2 custom ACF field settings for post type -> Product I am getting the error message Debug error notifications are listed in a custom column on each of these products.

NOTE: Undefined variable: product is located at /./././wp-content/themes/bpa/functions.php line 923

Note: Trying to get the product "id" of a non-object /./././wp-content/themes/bpa/functions.php line 923

Can someone help explain what I'm doing wrong to get an undefined variable?

Line 923 is:

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

Is it because I used the old toilet method or something?

Full functionality:

// 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 );

P粉450744515P粉450744515264 days ago419

reply all(1)I'll reply

  • P粉668113768

    P粉6681137682024-02-22 10:56:00

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

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

    reply
    0
  • Cancelreply