Get product IDs while using the "woocommerce_payment_complete" hook - php

My hooks are in theme folder/functions.php
I want tie hook with product_id from order.
tryin this method but they return nothing.
$product->get_id()
or
$product = wc_get_product( $product_id );
Full code
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete($order_id)
{
$order = wc_get_order($order_id);
$billingEmail = $order->billing_email;
$billingName = $order->billing_first_name;
$product_id = $order->product_id;
if ($product_id == 980) {
......
}

If you use Woocommerce 3.0+ version, then it should be this.
I found the answer with this link: https://wordpress.stackexchange.com/questions/97176/get-product-id-from-order-id-in-woocommerce
In an order can be multiple products, so you have to loop through them. In your code it would look like this:
add_action( 'woocommerce_payment_complete', 'so_payment_complete' );
function so_payment_complete($order_id)
{
$order = wc_get_order($order_id);
$billingEmail = $order->billing_email;
$billingName = $order->billing_first_name;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$product_variation_id = $item->get_variation_id();
if ($product_id == 980) {
// ....
}
}
}

Related

Update user meta field after WooCommerce checkout

I use WooCommerce with WordPress and I would like to update a custom field for the purchase of a single product at the time of payment validation with the display of the thank you page.
I started from this code but it does not work for the moment.
add_action( 'woocommerce_thankyou', 'checkout_update_user_meta' );
function checkout_update_user_meta( $order_id ) {
$order = new WC_Order( '$order_id' );
$user_id = $order->get_user_id();
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( $product_id == '777' ) {
update_user_meta( $user_id, 'masterclass-1', 'ok' );
}
}
}
add_action( 'woocommerce_thankyou', 'checkout_update_user_meta' );
function checkout_update_user_meta( $order_id ) {
$order = new WC_Order( $order_id );
$user_id = $order->get_user_id();
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( $product_id === 777 ) {
update_user_meta( $user_id, 'masterclass-1', 'ok' );
}
}
}
The issue with your code was that you wrapped the $order_id in single quotes. This line $order = new WC_Order( $order_id ); can also be replaced as $order = wc_get_order( $order_id );
I just modified your code to Update user meta field after woocommerce checkout according to your need. Please check my changes and hope it will work 👍
add_action( 'woocommerce_thankyou', 'checkout_update_user_meta' );
function checkout_update_user_meta( $order_id ) {
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
if ( $product_id == 777 ) {
update_user_meta( $user_id, 'masterclass-1', 'ok' );
}
}
}

Adding prefix to WooCommerce order number if order has items from a specific product category

In a webshop that can only have one item in the cart, I need to add a prefix to the order number when the order contains an item from a specific category
For this I wrote the following code:
add_action( 'woocommerce_prefix', 'check_product_category_in_order', 5 );
function check_product_category_in_order( $order_id ) {
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
$category_in_order = false;
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( has_term( 'MY-PRODUCT-CATEGORY', 'product_cat', $product_id ) ) {
$category_in_order = true;
break;
}
}
if ( $category_in_order ) {
*New funtion here*
}
}
Now I need the following function to run if $category_in_order:
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$prefix = 'AB-';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
But I cant seem to find out. Can I add a filter and function whitin an if statement?
There is no need to use a filter hook in the if condition.
You can immediately add all logic in the correct filter hook.
So to add a prefix to the order number when the order contains an item from a specific category you only have to use:
function filter_woocommerce_order_number( $order_id, $order ) {
// Prefix
$prefix = 'AB-';
// Specific categories: the term name/term_id/slug. Several could be added, separated by a comma
$categories = array( 'categorie-1', 'categorie-2', 15, 16 );
// Flag
$found = false;
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
// Has term (product category)
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$found = true;
break;
}
}
// true
if ( $found ) {
$order_number = $prefix . $order_id;
} else {
$order_number = $order_id;
}
return $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );
Related: Adding prefix to WooCommerce order number based on multiple categories

WooCoomerce: Automatically add product line item if another category product is present on order creation

Im trying to add products from an ACF field automatically when I manually create an order and Im adding a product within a given category, but not sure where to start.
This is what I have so far:
add_action( 'woocommerce_order_status_processing', 'add_unique_id', 10, 1 );
function add_unique_id( $order_id ) {
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item_id => $item_obj ) {
$category = 'my_product_category';
// If the targeted product ID or the product category is found in the order items.
if( $item_obj->get_product_id() == $targeted_product_id || has_term( $category,'product_cat', $item_obj->get_product_id() ) ) {
$product_id = get_sub_field('product', false);
$productextra = wc_get_product( $product_id );
$new_product_price = 0;
$productextra->set_price( $new_product_price );
$item_id = wc_add_order_item( $productextra );
}
}
}

Get category ID from order item inside woocommerce hook

In functions.php we have:
add_action( 'woocommerce_order_status_processing', 'mysite_processing', 10, 1);
then inside this function we get order items:
function mysite_processing($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
}
}
After getting the product id, we want to get the category id that product belongs. We already try with get_the_terms and has_term functions, both didn't work.
Solved, added to items for: $product = wc_get_product( $product_id );
Try below code
function mysite_processing($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$term_list = wp_get_post_terms($product_id,'product_cat',array('fields'=>'ids'));
print_r($term_list);
$cat_id = (int) $term_list[0];
echo $cat_id;
die();
}
}

Get the product ID in WooCommerce 3+ Order items

I am trying to get woocommerce thank you page order_id. Using the code below.
But unfortunately I can't get it.
add_action( 'woocommerce_thankyou', 'bbloomer_check_order_product_id');
function bbloomer_check_order_product_id( $order_id ){
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item['product_id'];
if ( $product_id == XYZ ) {
// do something
}
}
}
This code is outdated for WooCommerce version 3+. You should use instead:
add_action( 'woocommerce_thankyou', 'check_order_product_id', 10, 1);
function check_order_product_id( $order_id ){
# Get an instance of WC_Order object
$order = wc_get_order( $order_id );
# Iterating through each order items (WC_Order_Item_Product objects in WC 3+)
foreach ( $order->get_items() as $item_id => $item_values ) {
// Product_id
$product_id = $item_values->get_product_id();
// OR the Product id from the item data
$item_data = $item_values->get_data();
$product_id = $item_data['product_id'];
# Targeting a defined product ID
if ( $product_id == 326 ) {
// do something
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 3+
Reference: How to get WooCommerce order details
I was not satisfied with current answers, as sometimes you need to check multiple products. If you do the same search for each product, it's really a waste, so I put it into a dispatcher format.
add_action('woocommerce_order_status_completed', 'onItemCheckout',10,1);
function onItemCheckout($order_id){
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_key => $item_values){
$product_id = $item_values->get_product_id();
switch($item_values->get_product_id()){
case 9999 : FreeShipping($order, $product_id); break;
case 1010 : RequireValidation($order, $product_id); break;
default: break;
}
}
}
Alternatively,...
$ItemCheckoutHandler=[];
$ItemCheckoutHandler[9999]='FreeShipping';
$ItemCheckoutHandler[1010]='RequireValidation';
add_action('woocommerce_order_status_completed', 'onItemCheckout',10,1);
function onItemCheckout($order_id){
global $ItemCheckoutHandler;
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_key => $item_values){
$product_id=$item_values->get_product_id();
$ItemCheckoutHandler[ $product_id ]( $order, $product_id );
} //Call the function assigned to that product id in the array
}
In either case, the assigned functions would take the order object, rather than the id, and the product_id as an argument:
function FreeShipping($order, $product_id){ ... }
function RequireValidation($order, $product_id){ ... }
You can of course customize these inputs to your liking.
Try this, hope it will work
add_action( 'woocommerce_thankyou', 'your_function_name', 10);
function your_function_name($order_id)
{
$order = wc_get_order($order_id);
foreach($order->get_items() as $order_key => $order_value)
{
$product_id = $order_value->get_data()['product_id'];
if($product_id == '123')
{
//do what you wnat and 123 is random product id, you can match product id with other as you want
}
}
}
Thank you.

Categories