1

I'm trying to get current product id but its returns null;

I want to use product id in below function

new WC_Product( $product_id);

note that i want get product id in "functions.php" file.

I tried

global $post;
$id = $post->ID

and

global $product;
$id = $product->id;

But that's not working

Arman Bagheri
  • 608
  • 7
  • 19

2 Answers2

2

I understood the answer in woocommerce you can't access globally product object you have a way for handle this Issue

you should use below code in function.php

wc_get_product()->get_id();
Arman Bagheri
  • 608
  • 7
  • 19
2

Since Woocommerce 3.0 you have to use:

global $product;
$id = $product->get_id();
Kalti
  • 491
  • 8
  • 19