Hi Ryan, Thanks for your

hyperbole's picture
Offline
Joined: 09/18/2007
Juice: 19
Hi Ryan, Thanks for your

Hi Ryan,

Thanks for your suggestion. I'm sorry it's taken me so long to get back to you on this. Life just keeps getting in the way.

I implemented your suggestion, and it works pretty well. Here is the code I created:

           list($ret, $item) = GalleryCoreApi::
                    loadEntitiesById($entity_id, 'GalleryItem');
           if  ($ret)  { return array($ret, null); }

           $node             =  new StdClass();
           $node->type       = 'product';
           $node->status     =  1;
           $node->title      = $item->getTitle();
           $node->image      = $image_url;
           $node->sell_price = $product_price;
           $node->body       = 'Body: for $product_name';
           node_save($node);

           $node_id = $node->nid;

        if  ($node_id > 0) {
           list ($ret, $thumbnail_list) = GalleryCoreApi::
                        fetchThumbnailsByItemIds(array($entity_id));
           $thumbnail_item = $thumbnail_list[$entity_id];
           $thumbnail_url  = $urlGenerator->
              generateUrl(array_merge(
                             array('view'   => 'core.DownloadItem',
                                   'itemId' => $thumbnail_item->getId()
                                  ),
                             array()
                                     ),
                          array('forceSessionId' => false,
                                'forceFullUrl'   => true,
                                'htmlEntities'   => false
                         )     );

           $data = array( 'module'        => 'commerceclasses',
                          'entity-id'     => $entity_id,
                          'model'         => $purchase_id,
                          'name'          => $names,
                          'prices'        => $prices,
                          'thumbnail-url' => $thumbnail_url
                        );
           $num_options = count(explode(';', $names));
           for ($indx=1; $indx<=$num_options; $indx++) {
              $data['attributes'][$indx] = $indx;
           }

           uc_cart_add_item($node_id, 1, $data);
        }

I know that the above code is adding a node to the Drupal database, because I can see the records in the node table and I see the data in uc_cart_products. Also when I go to the main page of the site, I can see the new nodes listed there (something I want to get rid of later.)

I then added a function to handle the cart_item hook that gets called in uc_cart_get_contents:

function commerceclasses_cart_item($op, $item) {
    switch ($op) {
      case 'load':
         $item->options = get_commerceclasses_cart_options($item);
         $total_cost = $total_price = $total_weight = 0;
         foreach ($item->options as $option) {
             $total_cost   += $option['cost'];
             $total_price  += $option['price'];
             $total_weight += $option['weight'];
         }

         $item->cost   += $total_cost;
         $item->price  += $total_price;
         $item->weight += $total_weight;

         if  (! empty($item->data['model'])) {
             $item->model = $item->data['model'];
         }
        break;
    }
}

function get_commerceclasses_cart_options($item) {
    $options = array();
    $names   = explode(';', $item->data['names']);
    $prices  = explode(';', $item->data['prices']);
    $aid = 1;
    foreach ($names as $indx => $name) {
       $options[$aid] = array('attributes' => 'Option',
                              'nid'        => $item->nid,
                              'oid'        => $aid,
                              'aid'        => $aid,
                              'name'       => $name,
                              'cost'       =>  0,
                              'price'      => $prices[$indx],
                              'weight'     =>  0,
                             );
       $aid++;
    }

    return $options;
}

The function commerceclasses_cart_item doesn't ever get called. Is there something I'm supposed to do to register it so that it will be called? Could you please suggest what I need to do here?

Thanks,
Carl.

Programatically adding an item to the cart By: hyperbole@drupal.org (7 replies) Thu, 10/04/2007 - 16:40