Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce get carts of all user

I want all users cart which is present in user session. Woocommerce is using user meta to store the persistence cart and the key is _woocommerce_persistent_car. I am able to get the user carts using this meta. But I don't have each user's cart id. Now The question is, if I have this carts and its data. I want to calculate the cart total including tax and other prices. I am getting this result for particular cart

array (size=2)
  '28d2995df7158f576605fc9df89a9b4b' => 
    array (size=8)
      'product_id' => int 344820
      'variation_id' => int 344868
      'variation' => 
        array (size=1)
          'Type' => string '539' (length=3)
      'quantity' => int 1
      'line_total' => float 12.99
      'line_tax' => int 0
      'line_subtotal' => float 12.99
      'line_subtotal_tax' => int 0
  '60f85b840756b7f2e2da48ad8429ca7a' => 
    array (size=8)
      'product_id' => int 344820
      'variation_id' => int 344877
      'variation' => 
        array (size=1)
          'Type' => string '539' (length=3)
      'quantity' => int 3
      'line_total' => float 38.97
      'line_tax' => int 0
      'line_subtotal' => float 38.97
      'line_subtotal_tax' => int 0

From seeing this result, I have all the details about cart but when I am at calculating the cart total, I am confused how to calculate it.

Can anyone help me with calculation of cart total?

like image 729
Mehul Kaklotar Avatar asked Jan 26 '26 09:01

Mehul Kaklotar


1 Answers

If you have that following data, you can use this code to get the total along with tax.

$total = 0;
foreach( $your_array as $data){
    if( $data["line_subtotal"])
        $total = $total + $data["line_subtotal"];

    if( $data["line_subtotal_tax"])
        $total = $total + $data["line_subtotal_tax"];
}

if you dont wanna add tax, then you can skip the tax addition.

like image 142
Pushpak Patel Avatar answered Jan 27 '26 21:01

Pushpak Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!