1

I want to get the value of _product_id on the payout page (thankyou.php), can somebody help me with this.

I have attached an image of the field I need exactly that value

value into database

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97

1 Answers1

1

You can use woocommerce_thankyou hook to get the product ID's in thankyou page

function wh_getProductIds($order_id)
{
    //getting order object
    $order = wc_get_order($order_id);
    $product_id_arr = [];
    //getting all line items
    foreach ($order->get_items() as $item)
    {
        $product = $item->get_product();
        $product_id_arr = $product->get_id(); //storing the product ID
        //$product_sku = $product->get_sku();
    }
    //$product_id_arr has all the order's product IDs
    //now you can do your stuff here.
}

add_action('woocommerce_thankyou', 'wh_getProductIds', 10, 1);

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works. version 3.x or above

Related:

Hope this helps!

Machavity
  • 30,841
  • 27
  • 92
  • 100
Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
  • Thank you very much for your answer, should this code be added to the file (functions.php) only, or do I need to add the same code to the thankyou.php file? – srdjan_stojanovic Oct 23 '17 at 11:32
  • @srdjan_stojanovic: glad that it helped you, Only add this code to your `functions.php` file. – Raunak Gupta Oct 23 '17 at 11:58
  • Unfortunately i do not succeed, i have entered this code (see link) but there is no change.. https://gist.github.com/anonymous/3674ef10efa13a73782347bb89caff11 – srdjan_stojanovic Oct 23 '17 at 12:12
  • @srdjan_stojanovic: what you want to achieve after getting the product ID? If you add this `var_dump($product_id_arr);die('im here, for test');` just before the functions end. and place a order you 'll see that product ID(s) is/are printed. – Raunak Gupta Oct 23 '17 at 12:25
  • Now I get always reuzultat :(NULL im here, for test), I think I'm doing something wrong .. I set up a function and this call all in the file (function.php). is it enough or do I have to do something else? – srdjan_stojanovic Oct 23 '17 at 12:36
  • @srdjan_stojanovic: I again tested my code and it is working at my end, I think either plugin or any theme not allowing the code to work without priority, I have update my code pls try with that. I changed `add_action`. – Raunak Gupta Oct 23 '17 at 15:22