PHP Warning: Attempt to read property “id” on null

$product->id only works if $product has a value, and sometimes it does not! E.g. on a login page which product would it be?

So you need to check if it’s valid/exists/safe with an if check:

global $product;
if ( ! empty( $product ) ) {
    // code that uses $product goes here
}