Adding Ajax autocomplete to product_add on order form

Posts: 13
Joined: 03/11/2008

Hi,

This isn't directly an Ubercart issue, but it affects the "admin/store/orders/99/edit" page for me.

Basically, I am using hook_form_alter() to add an Ajax autocomplete to the product selector (used for adding products to an order when the order is being created by an administrator). I am not using the uBrowser option for this - just the default (list of products with a search filter textfield underneath), although I want to get rid of the select box with the (in my case anyway) HUGE list of products...

However, whilst I get the little gray Ajax "ball" in the textfield, Drupal doesn't seem to do the rest i.e. set automplete=OFF, and attach the Ajax script. No errors are thrown so I'm guessing it might be that since the DIV is built by Javascript, the elements in it are missing whatever initialisation scripts get called.

function form_alter() {
  if ($form_id == 'uc_order_product_select_form') {
    $form['product_search']['#autocomplete_path'] = 'admin/store/products/autocomplete';
  }
}

Can anyone suggest how I can make the Ajax work for this pane/DIV ??

Regards,

- Paul

Posts: 4747
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Yeah, I think the trouble here is that Drupal would normally load the autocomplete JS on pageload, but that div doesn't do a pageload. You might try using that same form alter function to add this:

<?php
  drupal_add_js
('misc/autocomplete.js');
?>

in an if statement for the order edit form so it gets loaded on the pageload.

Great idea, btw. Eye-wink I'd love it if you posted this as a contrib when it's done!

Posts: 13
Joined: 03/11/2008

Thanks for the tip Ryan!

What it needed in the end was this:

  $form['autocompletehack'] = array(
    '#type' => 'markup',
    '#value' => '<script type="text/javascript" src="/misc/autocomplete.js"></script>'
  );

Works a treat Smiling

I'm going to have a fair few bits and pieces to contribute to Ubercart once I've completed this project - this Ajax search, a sophisticated stock control module allowing tracking of a product's quantity and cost price on a per-supplier-invoice basis (e.g. you get a new delivery in and the cost price has changed slightly - can't be handled at present), an option to remove "Add to basket buttons when a product is out of stock, and no doubt more by the time I get to the end! This is a great project and I'll gladly do my bit!

Regards,

- Paul