简体   繁体   中英

Wordpress how to use session ? I want to use Shopping Cart

I want to develop online order & shopping cart. My solution is use Session to save user's carts. But Wordpress not provide Session. Have any solution to develop that function?

WordPress doesn't use sessions so you can either start one yourself, or consider creating a database table for storing shopping cart contents and instead use the database.

If you want to start a session, your plugin should call add_action using init as the hook and in your function call, use the code:

if ( session_id() == '' || (function_exists('session_status') && PHP_SESSION_NONE == session_status()) ) {
    // no session has been started yet
    session_start();
}

That code will start a session early on in the WordPress request cycle so you can then use it in your plugin to store shopping cart data in the session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM