1

I am using commerce_donate module and I want to add a product (type=donation) into cart programmatically with the amount set as per the user has already selected on some other page. On some custom form submit, I want to add this donation type product into cart in order to process the usual commerce order / checkout.

How can I achieve this ?

Thanks.

2 Answers 2

1

No code needed, just watch the video about "Donations with Custom Line Item Types in Drupal Commerce". Some more details about it (= quote from that link):

This screencast shows how to use a custom line item type to allow your customer to specify a user-defined amount to donate. The line item has a Donation Amount field on it, and we then create a product display node that uses that custom line item. Finally, we use a custom rule to update the "price" of the donation product to the amount the customer has chosen.

3
  • @Travis please check my edited version of your answer, attempting to improve your link-only answer. Feel free to further enhance it, of if ou don't like my edit at all simply perform a rollback ... Commented Oct 12, 2016 at 15:20
  • That looks and sounds great @Pierre.Vriens, thanks for clarifying it! Commented Oct 12, 2016 at 15:20
  • @TravisJohnston : ok, merci for "approve", but know that most of my edit (clarification) was a copy/paste from that link you first posted. Commented Oct 12, 2016 at 15:24
0

In the form submit

function custom_form_submit($form,$form_state) {
  $line_item = commerce_product_line_item_new(commerce_product_load($pid));
  commerce_cart_product_add($account->uid, $line_item);
  drupal_goto('cart');
}
1
  • This is adding the product but with the price added in the backend. How do I modify/update the price using this code. ? Commented Oct 4, 2016 at 11:55

Your Answer

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

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