diff --git uc_aac.js uc_aac.js index bbe599e..63ce873 100644 --- uc_aac.js +++ uc_aac.js @@ -34,5 +34,5 @@ jQuery.fn.ucAacAttach = function() { } Drupal.behaviors.ucAac = function() { - $('.add-to-cart').ucAacAttach(); + $('.uc-aac-cart').ucAacAttach(); }; diff --git uc_aac.module uc_aac.module index 0760955..2fa0b3d 100644 --- uc_aac.module +++ uc_aac.module @@ -35,9 +35,11 @@ function uc_aac_menu() { * Implementation of hook_form_alter(). */ function uc_aac_form_alter(&$form, $form_state, $form_id) { - if (strstr($form_id, 'uc_product_add_to_cart_form_')) { + if (strstr($form_id, 'uc_product_add_to_cart_form')) { static $uc_aac_js = false; + // Add a class to the cart form. This will allow us to easily identify it for binding ajax stuff. + $form['#attributes']['class'] .= ' uc-aac-cart'; if (!$uc_aac_js) { $uc_aac_js = true; @@ -52,6 +54,11 @@ function uc_aac_form_alter(&$form, $form_state, $form_id) { $nid = $form['nid']['#value']; $product =& $form['#parameters'][2]; + // Add the products nid to the form, to avoid parsing formid's for an NID that may not exist. + $form['aac_nid'] = array( + '#type' => 'hidden', + '#value' => $nid, + ); // Use qty from post or use default qty if (isset($_POST['qty'])) { $form['qty']['#default_value'] = check_plain($_POST['qty']); @@ -150,7 +157,11 @@ function uc_aac_form_alter(&$form, $form_state, $form_id) { * Calculate product adjustments based on attribute option selections. */ function _uc_aac_calculate() { - $nid = array_pop(explode('_', check_plain($_POST['form_id']))); + // If the nid is not set, we can't do anything. + if (!is_numeric($_POST['aac_nid'])) { + return; + } + $nid = $_POST['aac_nid']; $output['nid'] = $nid; // Load the node and store the submitted data for later. @@ -225,7 +236,14 @@ function _uc_aac_calculate() { // Unset the form id to ensure Drupal doesn't attempt to process it. unset($_POST['form_id']); // Render the updated form - $output['form'] = drupal_get_form('uc_product_add_to_cart_form_'. $nid, $product); + // If the original form used the nid, then add it. + if (substr($_POST['form_id'], 'uc_product_add_to_cart_form_')) { + $output['form'] = drupal_get_form('uc_product_add_to_cart_form_'. $nid, $product); + } + // Otherwise use the non-standard form. + else { + $output['form'] = drupal_get_form('uc_product_add_to_cart_form', $product); + } // TODO: Fix bug where multiple add_to_cart forms on a single page have // elements with identical css id's after regenerating the forms