Getting the value of the product's id value (_product_id) after the payment in WooCommerce

邮差的信 提交于 2019-12-11 06:09:53

问题


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


回答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:

  • Woocommerce Get Orders on Thank you page and pass data javascript snippet
  • WooCommerce Conversion Tracking Script for two Pixel
  • How to Get Order Details by Order ID
  • woocommerce_thankyou hook not working

Hope this helps!



来源:https://stackoverflow.com/questions/46886177/getting-the-value-of-the-products-id-value-product-id-after-the-payment-in-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!