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 ?



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.

  • @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 ... – Pierre.Vriens Oct 12 '16 at 15:20
  • That looks and sounds great @Pierre.Vriens, thanks for clarifying it! – Travis Johnston Oct 12 '16 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. – Pierre.Vriens Oct 12 '16 at 15:24

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);
  • This is adding the product but with the price added in the backend. How do I modify/update the price using this code. ? – Mohit Wadhwa Oct 4 '16 at 11:55

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.