7 replies [Last post]
hyperbole@drupal.org's picture
Offline
Joined: 10/04/2007
Juice: 3
Was this information Helpful?

Since mid July, I have been writing a module for use with Gallery2. I've been writing the module for a client who wants to use Gallery2 for displaying images that would be bought by an end user. My client is embedding Gallery2 into Drupal and wants to use Ubercart as the cart module in Drupal for handling the sales transactions.

In the module I've written there is an 'Add to cart' button the end user can press to add a specific image with attributes to the cart.

Before I had written the module, I asked here which function I should call to add an item to the cart and Ryan answered that I should call 'uc_cart_add_item'. Now that I have the module almost complete, I realize I have a problem with that function.

The function 'uc_cart_add_item' expects the caller to pass a node-id. In Gallery2, there are no node-ids only entity-ids. I was considering possibly creating a temporary node-id every time an end user bought a product, but I'm not really familiar with Drupal and am not sure if that is the right approach or how I would go about creating the temporary node-id.

I assume the developers here are more familiar with the way Drupal works than I am so I thought I would come and ask for some help solving this problem.

Is there a way for me to pass information about an image to the cart without having a node-id? Or, is there a way for me to create a node-id each time I want to add an item to the cart? If I create a new node when I add an item to the cart, will that node become visible as a page somewhere on the commerce site?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
Re: Programatically adding an item to the cart

What I would do is create a node product that represents any image. Just call it "Picture" or something. Then, give it an attribute called "File" but don't give that any options. Then, when you go to add it to the cart w/ the function, use the data array to specify that attribute w/ the filename of the image you want. You'll have to play with it a little more to get the right image to show up in the cart, but this should be a good starting place.

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.

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15459
Re: Hi Ryan, Thanks for your

You probably just need to change the name of the function. Any implementation of hook_cart_item() will get called at the appropriate time, but the prefix of the function has to be the name of the module. So your function would work if the module name is commerceclasses.module.

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

Thanks Ryan,
It turns out that I had implemented the functions for the hooks in the Gallery module I've been writing. Apparently, Drupal doesn't look in the embedded Gallery module for hooks.

When I took them out of the Gallery module and put them into their own *.module file, added a corresponding *.info file, and moved them under the Drupal modules directory, the functions started being called when the cart function was invoked.

Now I'm trying to figure out how to get a thumbnail to appear in the cart for the programatically added product. Do you have any suggestions?

cartguy's picture
Offline
Joined: 10/25/2007
Juice: 8
Much needed

Hey... this sound really useful. We too would like to present pictures to the user to buy. Gallery2 is really nice in presenting art to the media, while ubercart has all the shopping cart features.

The code looks impressive... but I have no idea what does it say Smiling .
I am (almost) at the level of downloading modules, activating it, and pressing checkboxes... Smiling .

So please "hurry" and share your work with the rest of the universe...

Thanks.

tbendick's picture
Offline
Joined: 01/07/2008
Juice: 4
Re: Much needed

Any Updates.. I would love to test this out.

Tommy

milplus's picture
Offline
Joined: 11/27/2010
Juice: 182
gettng mand to embedd gallery2 into ubercart

dear guys I've seen in the thread you've probably been successful to embed Gallery2 with g2 image chooser into ubercart.

I've drupal 6 and ubercart 2.3.1 so not the latest version

I need to add gallery2 album photos to the ubercart product photos linking them to the photos in the albums.

Any idea. i'm not that far becuase with g2 image chooser installed ok I just need to switch from drupal node/page envoronment to pruduct node one. i guess

if you can advise

It's very urgent to have evidence of someone has realized what I need

cheers