6

I'm creating a section of my site where users must purchase individual nodes of a certain content type. In an effort to enhance usability, I allow the user to create the node without paying, but then save it as "Unpublished" so it's not yet visible on the site. Then, once the user pays, we'll publish their node. So whenever a user creates a node of a particular type, it should add a Product (which represents the right to post that particular node) to the user's Cart.

I know I can implement hook_nodeapi() to hook into the node creation event, but how do I add the product to their cart?

  • Maybe you want to look the description of node checkout module here: drupal.stackexchange.com/questions/1306/… – David L Jun 27 '11 at 19:00
  • no, you don't want NodeCheckout - that links a Content Type to a product, presenting the user (customer) with the node form of the linked Content Type after purchase. This user wants to add their product to the cart programmaticaly - a completely different request than your answer offers. – Blake Senftner Sep 7 '11 at 15:08
9

Is the node that users create is a valid product type node? In any case you can try this - http://api.lullabot.com/uc_cart_add_item

uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE, $check_redirect = TRUE, $rebuild = TRUE)

Here $nid = nid of the product, $cid = cart id (also user id) - look at http://api.lullabot.com/uc_cart_get_id

Hope this helps.

  • Just to clarify, I think Chaulky wants to have a single node as a product, and every user needs to buy that one node. That single node is going to be completely unrelated to the node the user has created, and the node the user has created is never purchased in that way. Am I on the right lines? – Chris Cohen Mar 21 '11 at 13:15
  • @Chris Cohen in this case yes, actually. But I believe Dipen's answer uses $nid to be the node id of the actual product, not the node I've just created. I actually would love to be able to make the node they created show up as it's own product, but it's not required and would bloat the store considerably, so I decided not to. – Chaulky Mar 25 '11 at 13:10
  • +1 (2 if i could) for including links to documentation, it was very helpful! – Chaulky Mar 25 '11 at 13:10
  • The links in this answer go to a 403 page. – user1015214 Nov 7 '16 at 16:08
2

You can use the following code, where $nid is your product node id:

Drupal 7

  $nid = 4844;
  $form_state = array(
    'values' => array(
      'nid' => $nid,
      'qty' => 1,
    ),
  );
  $node = node_load($nid);
  drupal_form_submit("uc_product_add_to_cart_form", $form_state, $node);

Drupal 6

  $nid = 4844;
  $form_state = array(
    'values' => array(
      'nid' => $nid,
      'qty' => 1,
    ),
  );
  $node = node_load($nid);
  drupal_execute("uc_product_add_to_cart_form", $form_state, $node);
0

For anyone else who comes to this... bsenftner's comment is incorrect. Nodecheckout lets customers create a node before paying.

"So whenever a user creates a node of a particular type, it should add a Product (which represents the right to post that particular node) to the user's Cart."

... is exactly what Nodecheckout does.

I have a working classified advert system whereby the user creates their advert. On saving, Nodecheckout adds a (product) node called "Classified Advert - 6 Months" to their cart. Paying for this product publishes their node.

Even anonymous users can create an advert and then have their account created as part of the checkout process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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