• Hi.
    I’m writing a new customized e-commerce plugin. I want to publish it on WordPress plugins directory too.
    I’ve created a new custom post type for “Products” as long as a new CPT for “Orders”. I have a “Shop Setting” page that contains some options too.
    I’v also created shortcodes to use in “Cart” and “Checkout” pages.

    Now I’m in the middle of codding for “Add to Cart” section.
    I have a question, could you please help me?
    For processing Cart contents, I have three solutions:
    1- Keep Cart contents in Cookie
    2- Keep Cart contents in Session
    3- Keep Cart contents in DB

    * Cookies are fast and they doesn’t have any load on server. But they are not secure and will be deleted after a period of time.
    * Sessions have not implemented in WordPress (so far I know)
    * DB is a good solution and permanent, but It needs so many DB transactions that causes junk data on DB and load on server.

    Do you have a solution or help for me?
    If your answer is to choose DB solution, what about to define Cart as a CPT and keeping it’s contents as meta?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Cookies are fast I guess, but they do have a load on the server. Cookies are passed to the server on every page request, and they are processed by the server when they arrive to make them available to the underlying software (Apache, PHP, etc).

    Sessions are available in WordPress, but you can also set up yoru own PHP sessions. But remember that PHP sessions use cookies to remember the session ID, so you’ve got a small extra load on the server resources, but still a similar 9but maybe slightly smaller) load on the server from the cookies. The biggest up-side to sessions is that they can be a little bit more secure, but not that much more as session ID’s can be shared around.

    Storing in the database is a little bit more resource-intensive, but not by that much in reality. Think about it… Your cart will save realyl two things – Product ID’s and quantities. Every product will still need a minimum of one DB call to get the products details. This will probably be more than one, as there’s normally going to be an image to go with it, and pricing can be from more than one table depending on how distributed it is. Add that to the multiple database calls that are made just to create the page in the first place, and there’s only a very small extra overhead by using the database.

    So what’s best? Personally, I’d store this in either sessions if you don’t mind people loosing their carts contents, or on the database if you want to have a persistant cart. The extra overhead of any processing for this really is negligable compared to what the rest of the system is using, so don’t be concerned about that. The worst that can happen is that your sotre beomces popular and you need to upgrade your server – and if that happens you’re very lucky!

    Thread Starter Sina Saeedi

    (@melodymag)

    Thanks “catacaustic”.

    If I want to store cart contents on DB, I have to generate a unique key (like MD5) and store cart contents in DB related to that key. And also keep that key on a session to recognize what user owns what cart. If the user is logged in, I store the user ID and the data will be absolutely permanent and if the user is not logged in, there will be a lot of junk data on DB after a period of time.

    Actually I need to store data either on session and DB, and for not logged in users there are a lot of junk that fills the DB.
    1- In your opinion is it a good solution?
    2- How can I handle that junk data?
    3- You said I can Use sessions in WP, Is there any function to use WordPress sessions?

    You don’t need to generate any hashes. It’s a lot easier than that. 🙂

    If the user is logged in, record the cart under their user ID. if they are not logged in, record it under their IP address. While this isn’t perfect it’s about the best way to do it.

    As far as having junk in the database, you always will. You can set up a CRON job or something to delete carts that are over X days/weeks/months old.

    Thread Starter Sina Saeedi

    (@melodymag)

    Thank you 🙂
    Is it a good solution to save Cart as a CPT and it’s contents as Post Meta?

    Or to store cart data in a separate table and it’s contents in another table?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Help on writing customized e-commerce plugin’ is closed to new replies.