0

I have a shop with one product, I want have a feature that user only determine how much he/she want to pay and then pay it.after complement payment manager of site according of amount money he/she paid, create a custom product and sent it.
My idea is create custom porduct with 1$ cost and then create a form and get the amount value from user,then I want create an order programmically and in order update the price with the value that user entered. How can Implement this?

I want something like this enter link description here

  • Can't you just create a generic "product" with price of $0.01 per gramme, ($10 for kg) and sell it per weight. Of course hiding actual text about weight, but there are already modules that allows you to sell that way and all you would need to do would be to adjust some texts. – Mołot Dec 4 '13 at 13:36
  • @Mołot But current site work correctly,I afraid if done working like this another product take effect and lost correct working – Yuseferi Dec 4 '13 at 13:48
  • Well, that's legitimate reason all right. But for that we have devel servers :) Of course I wish someone will be able to post the exact answer you want. Mine was just a guess, not worth being an answer. – Mołot Dec 4 '13 at 13:51
  • @Mołot thank you very much, I have a idea, what is your opinion about it, I create a product with 1$ Price (name it as custom) and when user determine how much he/she want to pay, create a order and change price in order ? – Yuseferi Dec 4 '13 at 13:57
  • 1
    Looks doable and solid :) – Mołot Dec 4 '13 at 14:00
0

finally this is my solution for do this job

before I create a product, save it product id, in my custom function I change it's cost and add to order

global $user;

  // Sanitize just in case
  //862 create by myself as custom porduct
  $product_id = 862;
  $price=  floatval($form_state['values']['field_suggest_your_price']['und'][0]['value'])*100;
  // Load product
  $product = commerce_product_load((int)$product_id);
  $product->commerce_price_usd["und"][0]["amount"]=$price;
  commerce_product_save($product);       
  if((empty($product->product_id)) || (!$product->status)){
    drupal_set_message(t('Invalid product id'));
    drupal_goto(); // frontpage
    return FALSE;
  }
//$product->commerce_price["und"][0]["amount"]=$price;
//$product->commerce_price_usd["und"][0]["amount"]=$price;
//dsm($product);

  // Attempt to load existing order for user from session
  $order = commerce_cart_order_load();
  if(!$order){
    $order = commerce_cart_order_new($user->uid);
  }

  // Save new order to get order id
  if(empty($order->order_id)){
    $order->revision = FALSE;
    commerce_order_save($order);
  }
  $order_id = $order->order_id;

  // Wrapper for easy field access and adding line items, order passed by reference
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  // Create new line item based on selected product
  $line_item = commerce_product_line_item_new($product, 1, $order->order_id);
  // Save new line item to get line item id
  commerce_line_item_save($line_item);
  // Add line item to order wrapper
  $order_wrapper->commerce_line_items[] = $line_item;

  // Save order with new order number and line item added
  $order->revision = FALSE;
  $order_wrapper->save();

  if(!empty($order->order_id)){
    drupal_set_message(t('Thank you, your %product is ready for checkout.',
                         array('%product' => $product->title)));
    drupal_goto('checkout/'.$order->order_id);
  }else{
    drupal_set_message(t('Sorry, there was a problem building your order. Please try again later.'), 'error');
    drupal_goto(); // frontpage
  }
| improve this answer | |

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.