Ubercart: Anonymous checkout, create new user, need order attached to new uid
In Ubercart > Checkout settings, when you enable "anonymous checkout enabled" it means "checkout without login".
By default, Ubercart always creates a new user during the checkout process, and enabling anonymous checkout just prevents people from having to register and login before they can checkout. Ubercart still creates an account for them.In this case, Ubercart doen't attach the order to the newly created user id.
Hence, later when the user logs in and try to edit his created node, he gets "Access denied" error, as uid column in node table is still "0" which needs to be updated when the anonymous user checkouts.
For this i created my own custom module. Go to the sites/all/modules folder and create a new folder with the name of your module, I will name it as ‘anon_user_node’ in this article. After that create two files:
* modulename.info
* modulename.module
Copy and paste the following code in your .module file:
function anon_user_node_user($op, &$edit, &$user, $category = NULL) {
$rebuild = FALSE;
$uid = $user->uid;
if($user->uid != 0) {
$result = db_query("SELECT ucnp.nid FROM uc_orders uco, uc_order_products ucop, uc_node_checkout_order_products ucnp WHERE ucop.order_product_id = ucnp.order_product_id AND uco.order_id = ucop.order_id AND uco.uid = %d",$uid);
// Update the author of node checkout nodes referenced in the cart.
while($row = db_fetch_object($result) ) {
$node_obj = node_load($row->nid);
$node_obj->uid = $user->uid;
node_save($node_obj);
// Make sure we rebuild the shopping cart with the updated info.
$rebuild = TRUE;
}
}
// Rebuild the cart contents so the checkout nodes are updated.
if ($rebuild) {
uc_cart_get_contents($user->uid, 'rebuild');
}
}
In the above code, basically i have applied joins to uc_orders, uc_order_products and uc_node_checkout_order_products tables so as to fetch node id by passing the user id of currently logged in user.