Not sure why undefined variable notification trying to get product 'id'; when adding ACF field to custom column?
P粉450744515
P粉450744515 2024-02-21 13:30:15
0
1
412

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粉450744515
P粉450744515

reply all(1)
P粉668113768

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();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!