diff --git a/commerce_ajax_cart.admin.inc b/commerce_ajax_cart.admin.inc
index 2de5e7d..13f35de 100644
--- a/commerce_ajax_cart.admin.inc
+++ b/commerce_ajax_cart.admin.inc
@@ -52,42 +52,61 @@ function commerce_ajax_cart_settings_form() {
'#options' => $view_settings,
'#title' => t('Shopping cart to use'),
'#default_value' => variable_get('commerce_ajax_cart_view', COMMERCE_AJAX_CART_DEFAULT_VIEW),
+ '#description' => t('Select Shopping cart form, if you are not sure...'),
);
- $behaviours = commerce_ajax_cart_script_examples();
+ module_load_include('inc', 'block', 'block.admin');
+ $current_theme = variable_get('theme_default', 'garland');
+ $blocks = block_admin_display_prepare_blocks($current_theme);
+ $blocks_list = array();
+ foreach ($blocks as $block) {
+ if ($block['module'] == 'commerce_cart') {
+ $blocks_list[$block['bid']] = $block['info'];
+ }
+ }
- $form['commerce_ajax_cart_behaves'] = array(
- '#type' => 'container',
- '#tree' => TRUE,
- '#title' => t('Example behaviours'),
+ $form['commerce_ajax_cart_block'] = array(
+ '#type' => 'select',
+ '#options' => $blocks_list,
+ '#title' => t('Commerce cart block'),
+ '#default_value' => variable_get('commerce_ajax_cart_block'),
);
- $behaviours_default = variable_get('commerce_ajax_cart_behaves', array(
- 'show_cart' => 0,
- 'fly_to_cart' => 0,
- ));
+ $form['commerce_ajax_cart_use_dialog'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Use dialog'),
+ '#default_value' => variable_get('commerce_ajax_cart_use_dialog'),
+ '#description ' => t('This popup dialog inform about adding to cart. If disabled, adding to cart message will appear as status message.'),
+ );
+ $form['commerce_ajax_cart_content_name'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Content id or class'),
+ '#default_value' => variable_get('commerce_ajax_cart_content_name', '#block-system-main'),
+ '#description' => t('Main container, where status message will appear. Usually, by default is #block-system-main
'),
+ '#states' => array(
+ 'invisible' => array(
+ ':input[name="commerce_ajax_cart_use_dialog"]' => array('checked' => TRUE),
+ ),
+ ),
+ );
- foreach ($behaviours as $id => $b) {
- $form['behaves'][$id] = array(
- '#type' => 'checkbox',
- '#title' => check_plain($b['title']),
- '#return_value' => 1,
- '#default_value' => $behaviours_default[$id],
- );
- $form['behaves'][$id . '_example'] = array(
- '#type' => 'fieldset',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#title' => t('Show code'),
- );
- $form['behaves'][$id . '_example']['code'] = array(
- '#markup' => '
',
- );
- }
+ $form['commerce_ajax_cart_fly2cart'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Fly to cart'),
+ '#default_value' => variable_get('commerce_ajax_cart_fly2cart'),
+ );
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save settings'),
+ // TODO Bad. Better solution?
+ $form['commerce_ajax_cart_fly2cart_parent'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Image and add to cart container classes or IDs'),
+ '#default_value' => variable_get('commerce_ajax_cart_fly2cart_parent'),
+ '#description' => t('Class or ID names of closest parent element, separated by comma in order of distance increasing, that contain both image of product and "add to cart" form' . $b['content'] . '
For example, .node,.views-row
- .node will seeked first, then if not found, .views-row
Be sure, it shouldn\'t contain other products images and add to cart forms.'),
+ '#states' => array(
+ 'invisible' => array(
+ ':input[name="commerce_ajax_cart_fly2cart"]' => array('checked' => FALSE),
+ ),
+ ),
);
return system_settings_form($form);
diff --git a/commerce_ajax_cart.info b/commerce_ajax_cart.info
index d693e0a..0dbb126 100644
--- a/commerce_ajax_cart.info
+++ b/commerce_ajax_cart.info
@@ -10,4 +10,3 @@ version = "7.x-1.0-beta2"
core = "7.x"
project = "commerce_ajax_cart"
datestamp = "1389966808"
-
diff --git a/commerce_ajax_cart.module b/commerce_ajax_cart.module
index edf0d83..d412068 100644
--- a/commerce_ajax_cart.module
+++ b/commerce_ajax_cart.module
@@ -7,7 +7,7 @@
*/
define('COMMERCE_AJAX_CART_CACHE_PREFIX', 'commerce_ajax_cart_');
-define('COMMERCE_AJAX_CART_DEFAULT_VIEW', 'commerce_cart_block.default');
+define('COMMERCE_AJAX_CART_DEFAULT_VIEW', 'commerce_cart_form.default');
/**
* Implements hook_views_api().
@@ -64,16 +64,30 @@ function commerce_ajax_cart_get_defaults() {
}
/**
+ * Implements hook_theme().
+ */
+function commerce_ajax_cart_theme() {
+ return array(
+ 'commerce_ajax_cart' => array(
+ 'variables' => array('product_count' => NULL, 'total_quantity' => NULL, 'total_amount' => NULL, 'order' => NULL),
+ 'path' => drupal_get_path('module', 'commerce_ajax_cart') . '/theme',
+ 'template' => 'commerce-ajax-cart',
+ ),
+ 'commerce_ajax_cart_dialog' => array(
+ 'variables' => array('line_item' => NULL),
+ 'path' => drupal_get_path('module', 'commerce_ajax_cart') . '/theme',
+ 'template' => 'commerce-ajax-cart-dialog',
+ ),
+ );
+}
+
+/**
* Implements hook_preprocess_block().
*/
function commerce_ajax_cart_preprocess_block(&$variables) {
- if ('commerce_cart' == $variables['block']->module && $variables['block_id'] == 1) {
- $variables['content'] = l('' . t('Show cart') . '', 'cart', array(
- 'html' => TRUE,
- 'attributes' => array(
- 'class' => array('commerce-ajax-cart-loader'),
- ),
- ));
+ if ($variables['block']->bid == variable_get('commerce_ajax_cart_block')) {
+ $params = commerce_ajax_cart_block_params();
+ $variables['content'] = theme('commerce_ajax_cart', $params);
}
}
@@ -81,8 +95,8 @@ function commerce_ajax_cart_preprocess_block(&$variables) {
* Menu callback to print the contents block.
*/
function commerce_ajax_cart_show_contents_block() {
- $content = commerce_cart_block_view('cart');
- print $content['content'];
+ $params = commerce_ajax_cart_block_params();
+ print theme('commerce_ajax_cart', $params);
}
/**
@@ -108,31 +122,20 @@ function commerce_ajax_cart_preprocess_html() {
'weight' => 9,
));
$commerce_ajax_cart_view = commerce_ajax_cart_get_view_name();
- drupal_add_js(array(
- 'commerce_ajax_cart' => array(
- 'position' => $defaults,
- 'update_url' => url('cart/ajax/update'),
- 'form_id' => $commerce_ajax_cart_view['view'],
- 'update_url_block' => url('cart/ajax/block'),
- 'ajax_url' => url('cart/ajax'),
- 'text' => variable_get('commerce_ajax_cart_text', t('There is no product in your cart')),
- ),
- ), 'setting');
- $behaves = commerce_ajax_cart_script_examples();
- $behaviours_default = variable_get('commerce_ajax_cart_behaves', array());
- foreach ($behaviours_default as $key => $status) {
- if ($status == 1) {
- $cmd = "jQuery(window).bind('commerce_ajax_cart_update',function(e,formId) {";
- $cmd .= $behaves[$key]['content'];
- $cmd .= "});";
- drupal_add_js($cmd, array(
- 'type' => 'inline',
- 'scope' => 'footer',
- 'weight' => 10,
- ));
- $cmd = '';
- }
+ $setting['commerce_ajax_cart']= array(
+ 'position' => $defaults,
+ 'update_url' => url('cart/ajax/update'),
+ 'form_id' => $commerce_ajax_cart_view['view'],
+ 'update_url_block' => url('cart/ajax/block'),
+ 'ajax_url' => url('cart/ajax'),
+ 'text' => variable_get('commerce_ajax_cart_text', t('There is no product in your cart')),
+ );
+ if (variable_get('commerce_ajax_cart_fly2cart')) {
+ $parents = explode(',', variable_get('commerce_ajax_cart_fly2cart_parent'));
+ $setting['commerce_ajax_cart']['fly2cart_parent'] = $parents;
}
+ drupal_add_js($setting, 'setting');
+
}
/**
@@ -185,9 +188,49 @@ function commerce_ajax_cart_entity_info_alter(&$entity_info) {
}
/**
+ * Implements hook_form_alter().
+ *
+TODO Make views cart form ajaxable for all actions: edit quantity, remove, update.
+function commerce_ajax_cart_form_alter(&$form, &$form_state, $form_id) {
+ $commerce_ajax_cart_view = variable_get('commerce_ajax_cart_view', COMMERCE_AJAX_CART_DEFAULT_VIEW);
+ // TODO Make better selection view form from settings:
+ if ($form_id == 'views_' . $commerce_ajax_cart_view['view'] . '_' . $commerce_ajax_cart_view['display']) {
+ $form['#action'] = url('cart');
+ if (!empty($form['edit_delete'])) {
+ foreach(element_children($form['edit_delete']) as $key) {
+ if (!empty($form['edit_delete'][$key]['#line_item_id'])) {
+ $form['edit_delete'][$key]['#ajax'] = array(
+ 'callback' => 'commerce_ajax_cart_cart_callback',
+ );
+ }
+ }
+ }
+ }
+}
+
+function commerce_ajax_cart_cart_callback($form, &$form_state) {
+ global $user;
+ drupal_get_messages();
+ $_POST = array();
+ $order_id = commerce_cart_order_id($user->uid);
+ $cart = commerce_embed_view('commerce_cart_form', 'default', array($order_id));
+ return array(
+ '#type' => 'ajax',
+ '#commands' => array(
+ ajax_command_replace('.view-commerce-cart-form', $cart),
+ ),
+ );
+}
+*/
+
+/**
* Implements hook_form_FORM_ID_alter().
*/
function commerce_ajax_cart_form_commerce_cart_add_to_cart_form_alter(&$form, $form_state, $form_id) {
+ if (variable_get('commerce_ajax_cart_use_dialog')) {
+ $form['#attached']['library'][] = array('system', 'ui.dialog');
+ }
+
$form['submit']['#ajax'] = array(
'callback' => 'commerce_ajax_cart_callback',
);
@@ -202,13 +245,40 @@ function commerce_ajax_cart_callback($form, &$form_state) {
$form = drupal_rebuild_form($form['#form_id'], $form_state, $form);
$commands[] = array('command' => 'commerce_ajax_cart_update');
- $commands[] = ajax_command_prepend('#block-system-main', theme('status_messages'));
+
+ if (variable_get('commerce_ajax_cart_use_dialog')) {
+ // TODO Theme should have more control for title, content, close button
+ $dialog = theme('commerce_ajax_cart_dialog', array('line_item' => $form_state['line_item']));
+ $commands[] = ajax_command_remove('#commerce-ajax-cart-dialog');
+ $commands[] = ajax_command_append('body', '