• Resolved FoxMulder84

    (@foxmulder84)


    Hello I’m trying to display the number of items added to cart in Woocommerce.
    Every time a specific quantity of items is added to cart, Woocommerce adds it to the message (i.e. “4 x Tshirts are added to your cart). I would need to display this value inside a custom message. I tried to use:

    global $woocommerce; 
    
    foreach ( WC()->cart->get_cart() as $cart_item ){
        
        $item = $cart_item['data'];
        //print_r($item);
        if(!empty($item)){
            $product = new WC_product($item->id);
            $quantity = $item->get_quantity;
            echo $quantity;
            
        }
    }

    echoing it but nothing is displayed.

    How can I get that specific value?

    Thank you in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @foxmulder84 ,

    You are unable to access the cart item quantity or ID in this way. It will give you an error. You can rather take this approach –

    global $woocommerce; 
    
    	foreach ( WC()->cart->get_cart() as $cart_item ) {
    	    
    	    $item = $cart_item['data'];
    
    	    if(!empty($item)) {
    	        $quantity = $cart_item['quantity'];
    	        echo $quantity;	        
    	    }
    	}

    You can find more example code for cart information here – https://www.businessbloomer.com/woocommerce-get-cart-info-total-items-etc-from-cart-object/

    I hope this helps.

    Thank you ??

    Thread Starter FoxMulder84

    (@foxmulder84)

    Hello Rashed!
    First of all thank you for your prompt reply!
    I tried with the code you provided but it get the total quantity of the item in the cart.
    What I need is slighty different: I would need to get the quantity of item added by quantity input in product page.
    For example, if I add to cart 4 items, I need to get 4 (the number of the added items in that moment).

    Can I display that value?

    I hope it’s clearer now ??

    Thank you in advance!

    Hello @foxmulder84 ,

    Okay, if I understand correctly now, you are trying to access the item details that are shown on the product page after adding an item to the cart. Is that right?

    To understand how the item details are coming you need to check this file –

    Path: \wp-content\plugins\woocommerce\includes\wc-cart-functions.php
    Function: wc_format_list_of_items

    If you want to rather want to modify the message see the function – wc_add_to_cart_message especially line no. 118 $added_text variable to understand how the item details are added to the message.

    For any modification of the quantity of the item there is a filter available as well – woocommerce_add_to_cart_qty_html

    I hope this information will be useful for your case.

    Thank you ??

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Howdy.

    We haven’t heard back from you in a while, so I’m going to go ahead and mark this thread as resolved. If you have any other questions please start a new thread.

    Cheers!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get product item quantity added to cart’ is closed to new replies.