0

I want to create a pre-made order in Drupal Commerce for an anonymous user, and redirect the user to the checkout.

My use-case is that I have two sites: One big Drupal Commerce site (up and running since 2011), and another site that sells a few very specific products. The second site is not a Drupal site.

Ideally I want users on the second site to be able to select a few products, click "checkout" and be redirected to the checkout page on the Drupal site, with an order that contains the products that he/she selected on site two.

Now, selecting products etc. won't be an issue (second site has access to all product data via Services API), but I have no idea how to create the order for the anonymous user in Drupal and make it belong to him/her.

I read this blog entry which could've been of use (send user to page that creates the order and then redirects user to checkout) but apparently that doesn't work for anonymous users.

Any help would be greatly appreciated!

1 Answer 1

0

I think you may be thinking about this incorrectly. If I were you, I'd write a custom module that created a new path designed to create a shopping cart for whoever lands on it with the requested products. You can include a token or something in the URL to ensure each one only gets processed once, and you can redirect to /cart or some other route after the order is created to try to reduce duplicate carts.

This means on the secondary site you're just storing in a cookie or the session the product IDs that you'll then use to construct the cart when you pass them to the Drupal site via query parameters or something like that. It isn't really possible to create a remote session for someone and then force assign the session ID to them when they land on your site afaik.

3
  • Actually, that's exactly how I've figured that I could do it (albeit with query params for the SKU's instead of a cookie). It's just that I read in the comments of your old blog entry that the solution didn't work for anonymous users - i.e. that the order that got created didn't end up belonging to the user, so the user would end up being redirected to an empty cart, and the order would just be simmering cluelessly in the system. Are you saying that it will work? Oct 19, 2018 at 7:48
  • Well, so long as you have redirected the customer to your site and you use the API properly, it should be fine. Specifically, that means using commerce_cart_product_add() with 0 as the uid for an anonymous user to build the cart. That will ensure the order ID gets noted in the appropriate session variable for you. Oct 22, 2018 at 14:54
  • The sample code from the blog entry did not work as expected even if I used 0 as uid. The order was never assigned to the user. However, commerce_cart_product_add() worked perfectly. Oct 24, 2018 at 7:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.