0

I'm trying to use Drupal Commerce for a subscription site that will have only one product to purchase - a subscription to a service.

I'd prefer if a user could either do product subscription as a part of user registration, or immediately be redirected. Currently I'm trying the redirection with a rule to a custom order page (which I'm creating with rules), however rules doesn't seem to know what the order id is of the created entity.

So I'm either looking for a way to fix that, or another solution that I may have missed. Basically I want registration to be the easiest thing possible.

2 Answers 2

0

Figured this out - apparently you have to add the action, "save entity" after you create it, and also force it to save immediately.

0

You can do it easily by pro-grammatically to implement this functionality. As i already used this earlier.

You just need to use the hook_form_alter() drupal function to alter the registration form

function regform_form_alter(&$form, $form_state, $form_id) {

if ($form_id == 'Your_FORM_ID') {
$form['#submit'][]='registration_formalter_submit';

}

}
function registration_formalter_submit($form, &$form_state){
 global $user;
 $PID = 2; // say any
  $uid = ($user->uid) ? $user->uid : 0;
    $order = commerce_order_new($uid, 'checkout_checkout');

// Save the order to get its ID.
    commerce_order_save($order);

// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
// here you need to pass product ID
    $product = commerce_product_load($PID);

    $line_item = commerce_product_line_item_new($product, 1, $order->order_id);
// Save the line item to get its ID.
    commerce_line_item_save($line_item);
// Add the line item to the order using fago's rockin' wrapper.
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $order_wrapper->commerce_line_items[] = $line_item;

// Save the order again to update its line item reference field.
    commerce_order_save($order);
    drupal_goto('checkout');

}

Also, add a rule to clear cart before adding a product to a cart.

Hope it works for you!

2
  • Does this work pretty seamlessly? Do you need to have "Add to Cart" enabled? I don't really want to have a cart if at all possible - there's only one product. I'll take a look at tossing this code in and seeing if it does what I need.
    – Jack Ryan
    Commented Aug 4, 2014 at 19:24
  • Yes. It is the appropriate way to purchase a product on registration progrmmatically. Also, you can try a drupal module drupal.org/project/commerce_registration. Commented Aug 6, 2014 at 4:12

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.