diff --git a/modules/cart/commerce_cart.module b/modules/cart/commerce_cart.module index 1f2dbff..0a02a7d 100644 --- a/modules/cart/commerce_cart.module +++ b/modules/cart/commerce_cart.module @@ -1719,6 +1719,9 @@ function commerce_cart_forms($form_id, $args) { * - $line_item->data['context']['show_single_product_attributes']: a boolean * indicating whether or not product attribute fields with single options * should be shown on the Add to Cart form. + * - $line_item->data['context']['button_text']: a text field setting from the + * formatter options field that can override the default Add to cart button + * text * - $line_item->quantity: the default value for the quantity widget if * included (determined by the $show_quantity parameter). * - $line_item->commerce_product: the value of this field will be used as the @@ -2226,7 +2229,7 @@ function commerce_cart_add_to_cart_form($form, &$form_state, $line_item, $show_q else { $form['submit'] = array( '#type' => 'submit', - '#value' => t('Add to cart'), + '#value' => !empty($line_item->data['context']['button_text']) ? $line_item->data['context']['button_text'] : t('Add to cart'), '#weight' => 50, ); } @@ -2442,6 +2445,7 @@ function commerce_cart_field_formatter_info() { 'description' => t('Display an Add to Cart form for the referenced product.'), 'field types' => array('commerce_product_reference', 'entityreference'), 'settings' => array( + 'button_text' => t('Add to cart'), 'show_quantity' => FALSE, 'default_quantity' => 1, 'combine' => TRUE, @@ -2462,6 +2466,14 @@ function commerce_cart_field_formatter_settings_form($field, $instance, $view_mo $element = array(); if ($display['type'] == 'commerce_cart_add_to_cart_form') { + $button_text = !empty($settings['button_text']) ? check_plain($settings['button_text']) : t('Add to cart'); + $element['button_text'] = array( + '#type' => 'textfield', + '#title' => t('Button text'), + '#default_value' => $button_text, + '#description' => t('Text to use in the Add to cart button. Default: %default', array('%default' => 'Add to cart')), + ); + $element['show_quantity'] = array( '#type' => 'checkbox', '#title' => t('Display a textfield quantity widget on the add to cart form.'), @@ -2531,7 +2543,13 @@ function commerce_cart_field_formatter_settings_summary($field, $instance, $view $summary = array(); if ($display['type'] == 'commerce_cart_add_to_cart_form') { + $button_text = !empty($settings['button_text']) ? check_plain($settings['button_text']) : t('Add to cart'); + $translated_instance = commerce_i18n_object('field_instance', $instance); + if (!empty($translated_instance['add_to_cart_form-button_text-' . $view_mode])) { + $button_text = $translated_instance['add_to_cart_form-button_text-' . $view_mode] . ' (' . t('translated') . ')'; + } $summary = array( + t('Button text: !text', array('!text' => $button_text)), t('Quantity widget: !status', array('!status' => !empty($settings['show_quantity']) ? t('Enabled') : t('Disabled'))), t('Default quantity: @quantity', array('@quantity' => $settings['default_quantity'])), t('Combine like items: !status', array('!status' => !empty($settings['combine']) ? t('Enabled') : t('Disabled'))), @@ -2573,6 +2591,7 @@ function commerce_cart_field_formatter_view($entity_type, $entity, $field, $inst if (!empty($products)) { $type = !empty($settings['line_item_type']) ? $settings['line_item_type'] : 'product'; $line_item = commerce_product_line_item_new(commerce_product_reference_default_product($products), $settings['default_quantity'], 0, array(), $type); + $line_item->data['context']['button_text'] = !empty($settings['button_text']) ? check_plain($settings['button_text']) : t('Add to cart');; $line_item->data['context']['product_ids'] = array_keys($products); $line_item->data['context']['add_to_cart_combine'] = !empty($settings['combine']); $line_item->data['context']['show_single_product_attributes'] = !empty($settings['show_single_product_attributes']); @@ -2652,6 +2671,21 @@ function commerce_cart_field_attach_view_alter(&$output, $context) { $arguments['line_item']->data['context']['product_ids'] = 'entity'; } + // Translate button text. + $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']); + $translated_instance = commerce_i18n_object('field_instance', $instance); + $view_mode = $element['#view_mode']; + // Special case for full view mode. + if ($view_mode == 'full') { + $view_modes = field_view_mode_settings($element['#entity_type'], $element['#bundle']); + if (!empty($view_modes['full']) && !$view_modes['full']['custom_settings']) { + $view_mode = 'default'; + } + } + if (!empty($translated_instance['add_to_cart_form-button_text-' . $view_mode])) { + $arguments['line_item']->data['context']['button_text'] = $translated_instance['add_to_cart_form-button_text-' . $view_mode]; + } + // Replace the array containing the arguments with the return value of // drupal_get_form(). It will be rendered when the rest of the object is // rendered for display. @@ -2719,6 +2753,19 @@ function commerce_cart_preprocess_views_view(&$vars) { * Implements hook_i18n_string_list_TEXTGROUP_alter(). */ function commerce_cart_i18n_string_list_field_alter(&$strings, $type = NULL, $object = NULL) { + // Add to cart button text translatable strings. + $field = field_info_field($object['field_name']); + if (in_array($field['type'], array('commerce_product_reference', 'entityreference')) && !empty($object['display'])) { + foreach ($object['display'] as $display_name => $display) { + if (!empty($display['settings']['button_text'])) { + $strings['field'][$object['field_name']][$object['bundle']]['add_to_cart_form-button_text-' . $display_name] = array( + 'string' => $display['settings']['button_text'], + 'title' => t('Add to cart button text') . ' (view mode: ' . $display_name . ')', + ); + } + } + } + // Attribute widget title translatable string. if (!isset($strings['field']) || !is_array($object) || !commerce_cart_field_instance_is_attribute($object)) { return; } diff --git a/modules/cart/includes/views/handlers/commerce_cart_handler_field_add_to_cart_form.inc b/modules/cart/includes/views/handlers/commerce_cart_handler_field_add_to_cart_form.inc index 1a8e31e..3f04182 100644 --- a/modules/cart/includes/views/handlers/commerce_cart_handler_field_add_to_cart_form.inc +++ b/modules/cart/includes/views/handlers/commerce_cart_handler_field_add_to_cart_form.inc @@ -8,6 +8,7 @@ class commerce_cart_handler_field_add_to_cart_form extends views_handler_field_e function option_definition() { $options = parent::option_definition(); + $options['button_text'] = array('button_text' => t('Add to cart')); $options['show_quantity'] = array('default' => FALSE); $options['default_quantity'] = array('default' => 1); $options['combine'] = array('default' => TRUE); @@ -23,6 +24,13 @@ class commerce_cart_handler_field_add_to_cart_form extends views_handler_field_e function options_form(&$form, &$form_state) { parent::options_form($form, $form_state); + $form['button_text'] = array( + '#type' => 'textfield', + '#title' => t('Button text'), + '#default_value' => $this->options['button_text'] ? check_plain($this->options['button_text']) : t('Add to cart'), + '#description' => t('Text to use in the Add to cart button. Default: %default', array('%default' => 'Add to cart')), + ); + $form['show_quantity'] = array( '#type' => 'checkbox', '#title' => t('Display a textfield quantity widget on the add to cart form.'), @@ -87,6 +95,7 @@ class commerce_cart_handler_field_add_to_cart_form extends views_handler_field_e // Build the line item we'll pass to the Add to Cart form. $line_item = commerce_product_line_item_new($product, $default_quantity, 0, array(), $this->options['line_item_type']); + $line_item->data['context']['button_text'] = check_plain($this->options['button_text']); $line_item->data['context']['product_ids'] = $product_ids; $line_item->data['context']['add_to_cart_combine'] = $this->options['combine'];