1

I have a page (cart page in ecommerce) with some data id like to access via PHP so I can mail it.

When I print all session variables using print_r( $_SESSION ); I get the following code.

I want to echo the value of "total".

I am using $_SESSION['cart']['total']; but it doesn't work.

All other outputs like "currency" and "language" are displayed as they should be.

What am I doing wrong here?

array(22) {
  ["sessiontoken"]=>
  &string(32) "2239f469b65f039885a0c747f6595609"
  ["cart"]=>
  &object(shoppingCart)#1 (5) {
    ["contents"]=>
    array(2) {
      [216]=>
      array(1) {
        ["qty"]=>
        int(1)
      }
      [71]=>
      array(1) {
        ["qty"]=>
        int(1)
      }
    }
    ["total"]=>
    float(115.9)
    ["weight"]=>
    float(0)
    ["cartID"]=>
    &string(5) "91586"
    ["content_type"]=>
    string(8) "physical"
  }
  ["language"]=>
  &string(7) "english"
  ["languages_id"]=>
  &string(1) "1"
  ["currency"]=>
  &string(3) "USD"
  ["navigation"]=>
  &object(navigationHistory)#2 (2) {
    ["path"]=>
    array(5) {
      [0]=>
      array(4) {
        ["page"]=>
        string(9) "index.php"
        ["mode"]=>
        string(6) "NONSSL"
        ["get"]=>
        array(1) {
          ["cPath"]=>
          string(2) "50"
        }
        ["post"]=>
        array(0) {
        }
      }
      [1]=>
      array(4) {
        ["page"]=>
        string(7) "404.php"
        ["mode"]=>
        string(6) "NONSSL"
        ["get"]=>
        array(0) {
        }
        ["post"]=>
        array(0) {
        }
      }
      [2]=>
      array(4) {
        ["page"]=>
        string(21) "checkout_shipping.php"
        ["mode"]=>
        string(3) "SSL"
        ["get"]=>
        array(0) {
        }
        ["post"]=>
        array(15) {
          ["formid"]=>
          string(32) "2239f469b65f039885a0c747f6595609"
          ["action"]=>
          string(7) "process"
          ["payment"]=>
          string(5) "gspay"
          ["shipping"]=>
          string(9) "flat_flat"
          ["hd_shipping"]=>
          string(5) "12.00"
          ["hd_std_shipping"]=>
          string(5) "29.00"
          ["cust_fname"]=>
          string(1) "k"
          ["cust_lname"]=>
          string(1) "k"
          ["cust_email"]=>
          string(10) "[email protected]"
          ["cust_street"]=>
          string(3) "kkk"
          ["cust_city"]=>
          string(3) "kkk"
          ["cust_state"]=>
          string(3) "kkk"
          ["cust_zip"]=>
          string(3) "222"
          ["country"]=>
          string(2) "72"
          ["btnSubmit"]=>
          string(20) "Continue to checkout"
        }
      }
      [3]=>
      array(4) {
        ["page"]=>
        string(20) "checkout_payment.php"
        ["mode"]=>
        string(3) "SSL"
        ["get"]=>
        array(0) {
        }
        ["post"]=>
        array(0) {
        }
      }
      [4]=>
      array(4) {
        ["page"]=>
        string(25) "checkout_confirmation.php"
        ["mode"]=>
        string(3) "SSL"
        ["get"]=>
        array(0) {
        }
        ["post"]=>
        array(2) {
          ["formid"]=>
          string(32) "2239f469b65f039885a0c747f6595609"
          ["payment"]=>
          string(5) "gspay"
        }
      }
    }
    ["snapshot"]=>
    array(0) {
    }
  }
  ["user_type"]=>
  &string(5) "guest"
  ["sendto"]=>
  &NULL
  ["cartID"]=>
  &string(5) "91586"
  ["payment"]=>
  &string(5) "gspay"
  ["comments"]=>
  &NULL
  ["shipping"]=>
  &array(3) {
    ["id"]=>
    string(9) "flat_flat"
    ["title"]=>
    string(53) "Global Priority Shipping (It takes 5-7 business days)"
    ["cost"]=>
    string(5) "12.00"
  }
  ["cust_fname"]=>
  &string(1) "k"
  ["cust_lname"]=>
  &string(1) "k"
  ["cust_email"]=>
  &string(10) "[email protected]"
  ["cust_street"]=>
  &string(3) "kkk"
  ["cust_city"]=>
  &string(3) "kkk"
  ["cust_state"]=>
  &string(3) "kkk"
  ["cust_zip"]=>
  &string(3) "222"
  ["cust_country"]=>
  &string(2) "72"
  ["cust_country_title"]=>
  &string(7) "Finland"
  ["billto"]=>
  &NULL
}
1
  • 2
    ['cart']->total, since cart is an object? Commented Oct 24, 2014 at 19:42

4 Answers 4

1

Looks like cart is an object.

So, you should be able to access it via:

$_SESSION['cart']->total;

Sign up to request clarification or add additional context in comments.

Comments

1

$_SESSION['cart'] is an object, then total is it's property: $total = $_SESSION['cart']->total;

2 Comments

use "code block" to write code in answer. Then your answer will be highlighted. good luck.
Thanks for the advice. I am a new user here (registered), I will try to keep on with rules.
0

cart is an object so it should be something like

 $_SESSION['cart']->total; 

or if total is private or the corresponding class doesn't have a magic getter __get(); that could do it (or check the Class API for the public method name that returns $this->total;):

 $_SESSION['cart']->getTotal();

1 Comment

@user1721135 I believe my answer is the most generic as sometimes the variable of an object could be private and no __get() is defined in the class. If you agree please accept it as answer :) thanks!
0

From your output,

["cart"]=>
  &object(shoppingCart)#1 (5) {
    ...
    }
    ["total"]=>    float(115.9) 

The value $_SESSION['cart'] is a PHP Object. You should access the total using:

$_SESSION['cart']->total;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.