Title: Fatal error using &#8220;$product-&gt;get_id()&#8221;
Last modified: January 21, 2026

---

# Fatal error using “$product->get_id()”

 *  Resolved [jaspash](https://wordpress.org/support/users/jaspash/)
 * (@jaspash)
 * [2 months, 3 weeks ago](https://wordpress.org/support/topic/fatal-error-using-2/)
 * Hello,
 * I get a fatal error when using this code in functions.php of my child theme:
 *     ```wp-block-code
       global $product;$product_id = $product->get_id();if ( $product_id != 2029 ) { // do something}
       ```
   
 * What am I missing here?
 * Please, let me know.
 * Regards,
    -  This topic was modified 2 months, 3 weeks ago by [jaspash](https://wordpress.org/support/users/jaspash/).
    -  This topic was modified 2 months, 3 weeks ago by [jaspash](https://wordpress.org/support/users/jaspash/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Sai (woo-hc)](https://wordpress.org/support/users/saivutukuru/)
 * (@saivutukuru)
 * [2 months, 3 weeks ago](https://wordpress.org/support/topic/fatal-error-using-2/#post-18793362)
 * Hi [@jaspash](https://wordpress.org/support/users/jaspash/),
 * This error occurs because the `$product` object is not always available globally.
   Code in `functions.php` runs on every page load, but `$product` is only set on
   WooCommerce product pages and only after WooCommerce has initialized it.
 * When `$product` is `null`, calling `$product->get_id()` will trigger a fatal 
   error.
 * To avoid this, make sure the code only runs in a product context and that `$product`
   is a valid `WC_Product` object. For example:
 *     ```wp-block-code
       if ( is_product() ) {
           $product = wc_get_product( get_the_ID() );
   
           if ( $product && $product->get_id() != 2029 ) {
               // do something
           }
       }
       ```
   
 * Alternatively, if you want to use the global:
 *     ```wp-block-code
       if ( is_product() ) {
           global $product;
   
           if ( $product instanceof WC_Product && $product->get_id() != 2029 ) {
               // do something
           }
       }
       ```
   
 * Can you confirm where this logic is intended to run (single product page only,
   or elsewhere)?
 *  Plugin Support [thelmachido a11n](https://wordpress.org/support/users/thelmachido/)
 * (@thelmachido)
 * [2 months, 1 week ago](https://wordpress.org/support/topic/fatal-error-using-2/#post-18805621)
 * It’s been a while since we heard back from you for this reason we are closing
   this thread. 
 * If WooCommerce has been useful for your store and you appreciate the support 
   you’ve received, we’d truly appreciate it if you could leave us a quick review
   here: 
 *  [https://wordpress.org/support/plugin/woocommerce/reviews/#new-post](https://wordpress.org/support/plugin/woocommerce/reviews/#new-post)
 * Feel free to open a new forum topic if you run into any other problem. 

Viewing 2 replies - 1 through 2 (of 2 total)

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Ffatal-error-using-2%2F%3Foutput_format%3Dmd&locale=en_US)
to reply to this topic.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [thelmachido a11n](https://wordpress.org/support/users/thelmachido/)
 * Last activity: [2 months, 1 week ago](https://wordpress.org/support/topic/fatal-error-using-2/#post-18805621)
 * Status: resolved