I am using drupal commerce for my shopping cart project and when a user register to drupal site a mail is fired to the user mail id. But In my project when a user buy a product (and if he is not registered ) he is redirect to the registration page and after register he will automatically log in to the site and redirect to the cart page . I have performed all the scenario except when user is registering a mail is sent to its mailbox which I do not need if the user buy a product before his registration
Here is my code
function user_verify_user_insert(&$edit, &$account, $category) {
// Make sure that this rule only applies to users not
// being created by admins. Also, if admin approval is
// required, further verification would be useless.
global $user;
$order = commerce_cart_order_load($user->uid);
$quantity_total = 0;
if ($order) {
$wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {
$quantity = $line_item_wrapper->quantity->value();
$quantity_total = $quantity_total + $quantity;
}
}
if (
!user_access('administer users', $user)
&&
variable_get_value('user_register') == 1
) {
$udata = new stdClass();
$udata->uid = $account->uid;
drupal_write_record('user_verify', $udata);
if (
(int)variable_get_value('user_verify_delay') == 0
) {
_user_verify_send_code($udata);
}
if (variable_get_value('user_verify_lock') && _user_verify_load($account) && $quantity_total==0 ) {
// modify the user's DB entry
$account->status = 0;
db_update('users')
->fields(array('status' => 0))
->condition('uid', $account->uid, '=')
->execute()
;
}
if ($quantity_total > 0) {
$account->status = 1;
db_update('users')
->fields(array('status' => 1))
->condition('uid', $account->uid, '=')
->execute()
;
_user_verify_cleanup($account);
$user = $account;
$form_state['uid'] = $account->uid;
user_login_submit(array(), $form_state);
}
}
}
function user_verify_form_alter(&$form, &$form_state, $form_id)
{
if($form_id==user_register_form)
{
$form['#submit'][] = 'user_verify_register_submit';
unset($form['simplenews']['newsletters']);
//$form['simplenews']['newsletters']['#attributes'] = array('checked'=> 'checked', 'style'=>'display:none');
}
}
function user_verify_register_submit($form, &$form_state)
{
$account = $form['#user'];
if ((int)$account->status == 1) {
$order_id = commerce_cart_order_id($account->uid);
if ((int)$order_id > 0) {
$form_state["redirect"] = url("checkout/" . $order_id);
}
}
}