Get in WooCommerce cart the product ID of a cart item
To get the product ID
of each cart item in the foreach loop (for a simple product):
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
}
If it's a variable product, to get the variation ID
:
foreach( WC()->cart->get_cart() as $cart_item ){
$variation_id = $cart_item['variation_id'];
}
Or for both cases (where $cart_item['data']
is the WC_Product
Object in Woocommerce 3+):
foreach( WC()->cart->get_cart() as $cart_item ){
// compatibility with WC +3
if( version_compare( WC_VERSION, '3.0', '<' ) ){
$product_id = $cart_item['data']->id; // Before version 3.0
} else {
$product_id = $cart_item['data']->get_id(); // For version 3 or more
}
}
Update: Using Product ID outside the loop
1) Breaking the loop (Just to get the first item ID (product ID) of cart):
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
break;
}
You can use directly $product_id
variable of the first item in cart.
2) Using an array of product IDs (one for each item in cart).
$products_ids_array = array();
foreach( WC()->cart->get_cart() as $cart_item ){
$products_ids_array[] = $cart_item['product_id'];
}
- To get the 1st item product ID:
$products_ids_array[0];
- To get the 2nd item product ID:
$products_ids_array[1];
etc…
To check product categories or product tags in cart item use WordPress has_term()
like:
foreach( WC()->cart->get_cart() as $cart_item ){
// For product categories (term IDs, term slugs or term names)
if( has_term( array('clothing','music'), 'product_cat', $cart_item['product_id'] ) ) {
// DO SOMETHING
}
// For product Tags (term IDs, term slugs or term names)
if( has_term( array('clothing','music'), 'product_tag', $cart_item['product_id'] ) ) {
// DO SOMETHING ELSE
}
}
We always use
$cart_item['product_id']
as we get the parent variable product when a cart item is a product variation.Product variations don't handle any custom taxonomy as product categories and product tags
Add the product ID after the cart item name in WooCommerce cart page for specific products
The answer, given by jpneey does not work (HTTP ERROR 500) because:
echo
was used instead ofreturn
So you get:
function filter_woocommerce_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
// The targeted product ids, multiple product IDs can be entered, separated by a comma
$targeted_ids = array( 30, 815 );
// Product ID
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
if ( in_array( $product_id, $targeted_ids ) ) {
return $item_name . ' (' . $product_id . ')';
}
return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'filter_woocommerce_cart_item_name', 10, 3 );
How to check if variable product ID is in the woocommerce cart?
This is the final version of the code, after reading the references provided by '7uc1f3r', above. Thanks for the help.
add_action( 'woocommerce_review_order_before_submit', 'add_custom_checkbox' );
function add_custom_checkbox() {
## ----- CHECK IF CERTAIN PRODUCTS (COULD ALSO BE VARIABLE PRODUCTS) ARE IN CART ----- ##
$product_ids = array (9145, 9151, 9152, 9153, 9155, 9156); // Search for this products (PARENT ID)
// Loop though cart items searching for the defined products
foreach( WC()->cart->get_cart() as $cart_item ) {
// Product id
$product_id = $cart_item['product_id'];
// Display checkbox if product found in cart
if ( in_array( $product_id, $product_ids) ) {
echo add_my_checkout_tickbox();
}
}
}
Get the cart item quantity by item id in Woocommerce
May be you should look first in the woocommerce template code cart/mini-cart.php where you will find the official related code.
Note: The "item ID" is only available in WC_Orders items loop, but not in WC_Cart which is a "Cart item Key". So you are surely talking about the product ID. But if you look to the code of the official template cart/mini_cart you will need to use the WC_Product
object instead of the $product_id
…
So you can always build a custom function like (with a $product
argument, the WC_Product object) that you can use in the corresponding template code or in your custom code:
function get_item_qty( $product ){
foreach( WC()->cart->get_cart() as $cart_item )
// for variable products (product varations)
$product_id = $product->get_parent_id();
if( $product_id == 0 || empty( $product_id ) )
$product_id = $product->get_id();
if ( $product_id == $cart_item['product_id'] ){
return $cart_item['quantity'];
// break;
}
return;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
USAGE (example): Here we will output the quantity of the $product
(WC_Product
object):
// Output the quantity based on the $product object
echo __('Quantity'). ': ' . get_item_qty( $product );
Official Documentation: Template Structure + Overriding Templates via a Theme
Add the product ID after the cart item name in Woocommerce cart page
There are some missing arguments in your hooked function and you should need to make some changes to get the product Id this way:
add_filter( 'woocommerce_cart_item_name', 'just_a_test', 10, 3 );
function just_a_test( $item_name, $cart_item, $cart_item_key ) {
// Display name and product id here instead
echo $item_name.' ('.$cart_item['product_id'].')';
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.
Related Topics
Using Default Arguments in a Function
Difference Between 2 Dates in Seconds
Sanitizing Strings to Make Them Url and Filename Safe
Notice: Array to String Conversion In
Laravel-5 'Like' Equivalent (Eloquent)
How to Write Specific CSS For Mozilla, Chrome and Ie
Generating a Drop Down List of Timezones With PHP
PHP-Intl Installation on Xampp
Does MySQL_Real_Escape_String() Fully Protect Against SQL Injection
Get Absolute Path of Initially Run Script
PHP: Check If an Array Contains All Array Values from Another Array