programatically create new cart

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113399

I'm looking into creating a new cart with php but I see that there is a session ID necessary. Where does this session come from and where is it generated?

I basically want to create a new cart for a specific user so I can put products inside (also through php) so I'm curious what you guys recommend and if you have any information on the session ID

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 3 months ago #113506

Hi,

I don't really understand what you need, thanks to explain in more details.

If you want to create a specific cart for a customer, you can use the multicart option from our configuration, and then via the menu (in backend) Customer > Cart create a new cart for the desired user and add the desired products inside.
Then the customer can access the carts via the menu "User control panel" in frontend.

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113602

Sorry for the confusion:

I'm creating custom components for a client. When a certain parameter in that custom component changes, I want to programmatically create a new cart for a specific user.

Here's an example:
User1 orders some products, he pays and his cart is cleared. This part already happens in Hikashop. Then in my custom component I want to be able to add a certain product into that empty cart. How would I do that? Do i have to create a new cart or does the cart still exist but is simply empty? What I think is that the cart is just emptied after payment but it still exists for that user. Is that correct? If so I can just grab the cart corresponding to the user and put the product inside.

I'm trying to do all of this in PHP because it happens automatically.

Please Log in or Create an account to join the conversation.

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 3 months ago #113642

Hi,

The cart is deleted either when the order is confirmed or created based on the option "Clean cart when order is" of the configuration.
So you can't grab its content in the database after that.

You don't need to create an empty cart if you want to have a new cart with a product.
Simply add a product to the cart and the cart will be created automatically while the product is added to it.

I invite you to look at the addToCartFromFields function of the file administrator/components/com_hikashop/classes/cart.php
That function clears the current cart if there is one and then use the data given to add products to the cart using the updateEntry function of the same file.

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113730

Ok I will take a peek.

But I still think you might be misunderstanding me. In my scenario, for user_1 there is no cart. I want to create a new cart for that user (I have the userID) in php without the user having to do anything. Since the cart gets deleted after the order is confirmed, I have to create a new cart (there is a complicated reason why i do this). However the user is at this moment not on the site. This is all done separately in a php script. So what do I do with the session? Since there is no session for that specific user.

Last edit: 11 years 3 months ago by Ortix.

Please Log in or Create an account to join the conversation.

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 3 months ago #113732

I'm not misunderstanding you.
The code of the addToCartFromFields function clears the current cart if any and add products to a new cart with PHP code. From what you're saying, it's exactly what you want.

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113737

Ok will do!

When is that method called so I can dump the variables and see what's in them?

Please Log in or Create an account to join the conversation.

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 3 months ago #113743

When you have an entry page, upon submitting the entries, the current cart is deleted and a cart based on the products attached to the entry fields values is created:
www.hikashop.com/en/support/documentation/75-entry-listing.html

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113752

Awesome Thanks!

One last question, what about the session id? In the method you told me about the code is trying to get the user's session id, but the user is not invoking the method. How would I have to deal with this? Or is there something i'm overlooking?

Please Log in or Create an account to join the conversation.

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 3 months ago #113753

The session is handled automatically by joomla. I didn't see any code relative to the session id in the function I talked about.
The session id is stored along the cart when the user is not logged in so that the system can load it based on that on the following pages.
Since the cart is created automatically for you as I already explained, there is no need to do anything about the session id in your code.

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113755

Ah nevermind, I was looking at the wrong method. That was initCart(). My bad! Thanks for the help, I will continue to implement this. Thank you for your help and patience :)

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113794

Ok i implemented this but I realized there is a problem. There is no way of telling the system for which user this cart is. Now it just created a cart for me only, but my initial question was to create a cart for a specific user whose ID is known. Currently the method creates a cart for me only.

EDIT:

I analyzed the code a bit more and the user_id and its session is grabbed in initCart().

That method is called in resetCart() which is the first method called in addToCartFromFields(). That's why I was asking about the session and user_id.

Last edit: 11 years 3 months ago by Ortix.

Please Log in or Create an account to join the conversation.

  • Posts: 82758
  • Thank you received: 13346
  • MODERATOR
11 years 3 months ago #113825

Hi,

I indeed didn't understood that you wanted to create a cart for another user than the one currently connected.
In that case, after creating the cart and adding the products to it, you can simply run an update query on it to change the user_id to the user_id of the user and potentially the session_id to the session_id of the user for which you want to assign the cart.

If you want to know the session_id based on the current user_id, you first need to load the user_cms_id from the hikashop_user table using the user_id you know. Then, you need to load the session_id from the table session using the user_cms_id you just got on the userid column.

The following user(s) said Thank You: Ortix

Please Log in or Create an account to join the conversation.

  • Posts: 73
  • Thank you received: 1
11 years 3 months ago #113847

awesome thanks for the help!

I noticed that addToCartFromFields doesn't return the cart or its ID. Is it stored in the class somewhere?

Please Log in or Create an account to join the conversation.

  • Posts: 13201
  • Thank you received: 2322
11 years 3 months ago #113905

Hi,

The cart id is stored in the session, so you can find it with:
$app->getUserState( HIKASHOP_COMPONENT.'.cart_id', 0, 'int' );

Please Log in or Create an account to join the conversation.

Time to create page: 0.089 seconds
Powered by Kunena Forum