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

tpainton’s picture

The function ucnc_document_get_product_data should contain a return.. I left it out because I was still working onit.. sorry can't edit!

tpainton’s picture

and it didn't work :) I have to get this done, so, I continue to update. Here is my finished code.

<?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_data = ucnc_document_get_product_data($node, $user);
                    $node->field_seminar_name[0]['value'] = check_plain($product_data['title']);
                    $node->field_seminar_price[0]['value'] = $product_data['price'];
                    
                }
            }
    }



}

/**
 * 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,
        );
    return $product_data;

}

debugging now. Might be tomorrow before I Can finish.. need to go scrape some paint. Literally.

tpainton’s picture

Well. 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.

tpainton’s picture

Category: feature » support

Changing to support request.. Thanks.

tpainton’s picture

hmmm, lost.

tpainton’s picture

I 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.

bonked’s picture

The 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.

tpainton’s picture

Since 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.

<?php
/**
 * Given the $node, we determine what the product is that is associated
 *
 * @param $node The seminar document.
 *
 * @return
 * The product node ready to be evaluated.
 * 
 */
function ucnc_document_get_product($node){
    // If document is being created then $node->product is empty, we need to fill it.
    // We do this by grabing the associated product from the url as provided by UC Node Checkout.
    if (empty($node->product)){
        $pnid = $_GET['product_nid'];
        return $pnid;
    }
    else{
        return $node->product;
    }
    
}

?>

This function changes $document->paid to 1 when the product is paid for.

<?php
/**
 * Implentation of hook_uc_payment_entered
 * Will update the seminar registration 'paid' field to TRUE (1)
 * TODO the data variable is passed, we could just use that instead of $order.
 */
function ucnc_document_uc_payment_entered($order, $method, $amount, $account, $data, $comment){
    //Obtain the corresponding seminar document from the order.
    $array = $order->products;
    foreach ($array as $object){
        $prodarray = get_object_vars($object);
        $seminar_doc_nid = $prodarray['data']['node_checkout_nid'];
        $sem_node = node_load($seminar_doc_nid);
        $sem_node->paid = 1;
        node_save($sem_node);
    }
}
?>
aidanlis’s picture

Status: Active » Postponed

If you'd like to sponsor the development of this feature, please reopen this issue.

aidanlis’s picture

Assigned: tpainton » Unassigned