How to Retrieve Cart Item Details in WooCommerce
WooCommerce, a powerful and customizable eCommerce platform built on WordPress, allows online stores to manage their products, customers, and transactions with ease. One of the common tasks developers face is retrieving detailed information about items in the cart, such as the product name, quantity, price, and other attributes. This guide will walk you through the process of fetching these details programmatically.
Understanding WooCommerce Cart
WooCommerce handles cart operations using a class called WC_Cart
. This class contains various methods to interact with the cart, including adding, removing, and updating items. To retrieve cart item details, we primarily interact with the get_cart()
method, which returns an array of cart items.
Steps to Retrieve Cart Item Details
Access the Cart Items: To begin, you need to access the cart items. You can do this by using the
WC()->cart->get_cart()
method. This method returns an array of cart item objects.php$cart_items = WC()->cart->get_cart();
Iterate Over Cart Items: Once you have the cart items, you can iterate over this array to fetch details of each item.
phpforeach ( $cart_items as $cart_item_key => $cart_item ) { // Your code to process each item }
Retrieve Item Details: Inside the loop, you can retrieve various details of each item. Commonly accessed properties include:
Product Name:
php$product_name = $cart_item['data']->get_name();
Quantity:
php$quantity = $cart_item['quantity'];
Price:
php$product_price = $cart_item['data']->get_price();
Total Price:
php$total_price = $product_price * $quantity;
Display or Use the Details: After retrieving the necessary details, you can display them or use them in your application as required. For example, you might want to display these details on a custom cart page or use them in an order summary.
phpforeach ( $cart_items as $cart_item_key => $cart_item ) { $product_name = $cart_item['data']->get_name(); $quantity = $cart_item['quantity']; $product_price = $cart_item['data']->get_price(); $total_price = $product_price * $quantity; echo '<p>Product: ' . $product_name . '</p>'; echo '<p>Quantity: ' . $quantity . '</p>'; echo '<p>Price: ' . $product_price . '</p>'; echo '<p>Total: ' . $total_price . '</p>'; }
Additional Details
Apart from the basic details, you might also want to retrieve other product attributes or custom fields. WooCommerce products can have a wide variety of attributes, and these can be accessed similarly using the product data object ($cart_item['data']
).
Product SKU:
php$product_sku = $cart_item['data']->get_sku();
Product Categories:
php$categories = wp_get_post_terms( $cart_item['product_id'], 'product_cat' ); foreach ( $categories as $category ) { echo $category->name . ' '; }
Custom Meta Data:
php$custom_meta = get_post_meta( $cart_item['product_id'], '_custom_meta_key', true );
Retrieving cart item details in WooCommerce involves using the WC_Cart
class to access the cart items and iterating over them to fetch the desired information. This process is straightforward and allows developers to customize the cart and checkout experience to meet specific business needs.
By understanding and utilizing these methods, you can effectively manage and display cart information, enhancing the functionality and user experience of your WooCommerce store. Whether you are building a custom cart page, creating order summaries, or integrating with other systems, these techniques will be essential in your WooCommerce development toolkit.
Related code samples
Retrieve Product Attributes
To get more detailed information about the product attributes like size, color, etc., you can use the following snippet:
phpforeach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$product_name = $product->get_name();
$quantity = $cart_item['quantity'];
$product_price = $product->get_price();
// Get product attributes
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ) {
echo '<p>' . $attribute->get_name() . ': ' . $attribute->get_options() . '</p>';
}
echo '<p>Product: ' . $product_name . '</p>';
echo '<p>Quantity: ' . $quantity . '</p>';
echo '<p>Price: ' . $product_price . '</p>';
}
Apply a Discount to the Cart
You can programmatically apply a discount to the cart total. This can be useful for implementing custom discount rules.
phpadd_action( 'woocommerce_cart_calculate_fees', 'custom_discount' );
function custom_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Example: Apply a $10 discount
$discount = 10;
$cart->add_fee( 'Custom Discount', -$discount );
}
Add Custom Meta Data to Cart Item
To add custom meta data to a cart item when a product is added to the cart, use the following snippet:
phpadd_action( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 3 );
function add_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
$custom_value = 'some_value'; // Example value
$cart_item_data['custom_meta_key'] = $custom_value;
return $cart_item_data;
}
// Display custom meta data in the cart
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $item_data, $cart_item ) {
if ( isset( $cart_item['custom_meta_key'] ) ) {
$item_data[] = array(
'key' => __( 'Custom Meta Key', 'woocommerce' ),
'value' => wc_clean( $cart_item['custom_meta_key'] ),
'display' => '',
);
}
return $item_data;
}
Display Custom Fields in Order Details
To display custom fields or meta data in the order details (after the order is placed), use the following snippet:
phpadd_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_order_line_item_meta', 10, 4 );
function add_custom_order_line_item_meta( $item, $cart_item_key, $values, $order ) {
if ( isset( $values['custom_meta_key'] ) ) {
$item->add_meta_data( __( 'Custom Meta Key', 'woocommerce' ), $values['custom_meta_key'], true );
}
}
// Display custom meta data in admin order details
add_action( 'woocommerce_admin_order_item_headers', 'display_custom_order_item_meta', 10, 1 );
function display_custom_order_item_meta( $item_id ) {
$item = new WC_Order_Item_Product( $item_id );
$custom_value = $item->get_meta( 'Custom Meta Key', true );
if ( ! empty( $custom_value ) ) {
echo '<div><strong>' . __( 'Custom Meta Key', 'woocommerce' ) . ':</strong> ' . $custom_value . '</div>';
}
}
Update Cart Item Quantity
To update the quantity of a cart item programmatically, you can use the following function:
phpfunction update_cart_item_quantity( $cart_item_key, $new_quantity ) {
WC()->cart->set_quantity( $cart_item_key, $new_quantity );
}
// Example usage: Update the quantity of a specific cart item
$cart_item_key = 'your_cart_item_key'; // Replace with the actual cart item key
$new_quantity = 2; // New quantity to set
update_cart_item_quantity( $cart_item_key, $new_quantity );
Remove an Item from the Cart
To remove an item from the cart programmatically:
phpfunction remove_cart_item( $cart_item_key ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
// Example usage: Remove a specific cart item
$cart_item_key = 'your_cart_item_key'; // Replace with the actual cart item key
remove_cart_item( $cart_item_key );
These snippets provide a comprehensive toolkit for managing and customizing the WooCommerce cart. By leveraging these examples, you can tailor the shopping experience to better meet your store's specific needs and enhance the overall user experience.