I am trying to create "quick buy" functionality. I have a product listing and a customer is able to click on "quick buy" for that product and then I want to be able to load up a view mode for that product display in an overlay which has a few product display fields including the add to cart field.
I can get the loading of the product display via jQuery load (or ajax) no problem, the issue is that the add to cart button is not working via ajax and the customer is instead redirected to the callback (which is now the action in the add to cart form).
Javascript code to load product display node:
$('.quick-buy:not(.ajax-processed)').addClass('ajax-processed').click(function(e) {
e.preventDefault();
$.ajax({
url: '/product/get/ajax/'+$(this).attr('rel')
}).done(function(result){
$('#quick-buy').html(result);
Drupal.attachBehaviors();
});
});
Module code:
function tweaks_menu() {
$item['product/get/ajax/%'] = array(
'page callback' => 'tweaks_get_product_ajax',
'page arguments' => array(3),
'access arguments' => array('access content'),
);
return $items;
}
function tweaks_get_product_ajax($nid) {
$node = node_load($nid, NULL, false);
$node_view = node_view($node, 'product_quick_buy');
$html = drupal_render($node_view);
print $html;
}
I have also tried:
function tweaks_get_product_ajax($nid) {
$node = node_load($nid, NULL, false);
$node_view = node_view($node, 'product_quick_buy');
$html = drupal_render($node_view);
$settings = FALSE;
$javascript = drupal_add_js(NULL, NULL);
if (isset($javascript['settings'])) {
$settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
$settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $javascript['settings']['data']));
$settings .= ');
}
print $html . $settings;
}
...to no avail.