In WooCommerce, you can use the WC()->cart->get_cart() function to retrieve the items in the cart. The get_cart() function returns an array of cart items, where each item is an object that contains various properties and methods that you can use to access the item’s details.

Here is an example of how you can use the get_cart() function to retrieve the name, quantity, and other details of all items in the cart:

Copy code$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
    $product = $cart_item['data'];
    $product_name = $product->get_name();
    $product_quantity = $cart_item['quantity'];
    $product_price = $product->get_price();
    $product_total = $product->get_price() * $cart_item['quantity'];
    // Do something with product data
}

You can also use the WC()->cart->get_cart_contents() function to retrieve the same information.

You can also use the WC()->cart->get_item_data() function to retrieve the item data.

Copy code$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
    $item_data = WC()->cart->get_item_data( $cart_item );
    //Do something with item data
}

You can also use the WC()->cart->get_item_data() function to retrieve the item data.

Copy code$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
    $item_data = WC()->cart->get_item_data( $cart_item );
    //Do something with item data
}

You can also use the get_item_meta() function to retrieve the item meta data.

Copy code$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item_key => $cart_item ) {
    $meta_data = $cart_item['item_meta'];
    //Do something with meta data
}

It’s important to note that the above code snippets are for retrieving cart item information, and you should use them only when the cart is not empty. Also, make sure that the code is executed after the woocommerce_init action is fired, so that the WC()->cart object is available.

(Visited 53 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window