I have a site that creates documents for a fee. I needed a way for users to purchase a product (scenario), and then I would have my website gather information about that scenario (checked out node) and then compile a pdf document for the user. I have created the following module that is HIGHLY specific to my needs but can be modified to anyones needs if they have a little php knowledge. NOTE, this is NOT TESTED yet and incomplete but someone was asking in another thread and I thought I would post this here as a starting point for them and possibly a new UC Node Checkout module or feature.
This is what I have so far and I will add to it as it comes along. Thus far it simply gathers the data *I* need and assigns it to an array. This is my first time using nodeapi() so that might have bugs. Hopefully will run!
<?php
// $Id$
/**
* @file
* Automatically adds associated product information to uc_node_checkout node fields.
* Specifically, price, user address, and product title.
*/
/**
* Implementation of hook_help()
*/
function ucnc_document_help($path, $arg) {
if ($path == 'admin/help#ucnc_document'){
$text = 'This module will automatically assign values to selected uc_node_checkout nodes';
return '<p>'.t($text).'</p>';
}
}
/**
* implemenation of hook_nodeapi
* When document node is created, we want to grab data from the product node while
* the product remains in the cart.
*
*/
function ucnc_document_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
global $user;
switch ($op) {
case 'insert':
if ($user->uid != 0){
if ($node->type == 'seminar_registration'){
$product_array = ucnc_document_get_product_data($node, $user);
//TODO apply $product_array to
}
}
}
}
/**
* Retrieves the price of the seminar and assigns value to field_seminar_price.
* @param object $node
* Is the seminar node.
* @param object $user
* Is the current user.
* @return
* An Array. Product and order data use for pdf generation.
*/
function ucnc_document_get_product_data($node, $user){
// load the associated product that corresponds to the seminar registration.
$cart_item = uc_node_checkout_load_cart_item($node->nid);
$prod_obj = uc_cart_get_item($cart_item->cart_item_id);
//Product object is loaded, lets get the data.
$price = number_format($prod_obj->price, 2, '.', '');
$product_title = $prod_obj->title;
//Get the order delivery address.
$address = uc_get_addresses($user->uid, $type = 'delivery');
$product_data = array (
'price' => $price,
'title' => $product_title,
'address' => $address,
);
}
Comments
Comment #1
tpainton CreditAttribution: tpainton commentedThe function ucnc_document_get_product_data should contain a return.. I left it out because I was still working onit.. sorry can't edit!
Comment #2
tpainton CreditAttribution: tpainton commentedand it didn't work :) I have to get this done, so, I continue to update. Here is my finished code.
debugging now. Might be tomorrow before I Can finish.. need to go scrape some paint. Literally.
Comment #3
tpainton CreditAttribution: tpainton commentedWell. This isn't as easy as I assumed. Node API case 'insert'. Doesn't work period. Using 'presave' DOES work, but only if I go back and edit the node, then save again. This must be something to do with the save procedure that UC Node Checkout uses when you submit the node by pressing 'add to cart'. Any ideas greatly appreciated.
Comment #4
tpainton CreditAttribution: tpainton commentedChanging to support request.. Thanks.
Comment #5
tpainton CreditAttribution: tpainton commentedhmmm, lost.
Comment #6
tpainton CreditAttribution: tpainton commentedI solved this by simply using hook_add_to_cart() and pulling variables from the $data param. So essentually, when a product is added to the cart, fields on the product are then assigned to the associated document. hook_add_to_cart also has a built in validation so I can send the user back to the document edit page to review things... Code if anyone wants it, but its very customized to by use.. might be a good guide.
Comment #7
bonked CreditAttribution: bonked commentedThe code would be helpful to post here, even though this is highly specific, seeing how you invoked the hook would help a lot of out.
Comment #8
tpainton CreditAttribution: tpainton commentedSince originally posting I have changed some things. I don't use hook_add_to_cart any longer but hook_payment_entered().
Also, something useful I found is that when uc node checkout, redirects you to the node creation form, the product nid is passed in the url. So I created this function to nab it and therefore associate it with the document.
This function changes $document->paid to 1 when the product is paid for.
Comment #9
aidanlis CreditAttribution: aidanlis commentedIf you'd like to sponsor the development of this feature, please reopen this issue.
Comment #10
aidanlis CreditAttribution: aidanlis commented