From d946f672c1e6d22bf70b08ae5bcaf92d70a56f7d Mon Sep 17 00:00:00 2001 From: Jakob Perry Date: Thu, 14 Jan 2016 00:55:16 -0800 Subject: [PATCH] Add currency detection to commerce discounts --- commerce_discount.info | 1 + commerce_discount.install | 23 +++++++++ commerce_discount.module | 2 +- .../commerce_discount_offer.inline_entity_form.inc | 7 +++ includes/views/commerce_discount.views.inc | 5 ++ .../commerce_discount_handler_field_type.inc | 59 ++++++++++++++++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 includes/views/handlers/commerce_discount_handler_field_type.inc diff --git a/commerce_discount.info b/commerce_discount.info index 26ec861..e9019e6 100644 --- a/commerce_discount.info +++ b/commerce_discount.info @@ -30,6 +30,7 @@ files[] = includes/commerce_discount.controller.inc files[] = includes/views/commerce_discount.views.inc files[] = includes/views/handlers/commerce_discount_handler_field_operations_dropbutton.inc files[] = includes/views/handlers/commerce_discount_handler_field_commerce_discount_analytics.inc +files[] = includes/views/handlers/commerce_discount_handler_field_type.inc ; Simple tests files[] = tests/commerce_discount.test diff --git a/commerce_discount.install b/commerce_discount.install index 78455e4..b6fff48 100644 --- a/commerce_discount.install +++ b/commerce_discount.install @@ -102,6 +102,13 @@ function commerce_discount_schema() { 'description' => 'The discount offer type (bundle).', 'default' => '', ), + 'currency' => array( + 'type' => 'varchar', + 'length' => 12, + 'not null' => TRUE, + 'description' => 'Currency code for this type.', + 'default' => '', + ), ), 'primary key' => array('discount_offer_id'), ); @@ -1115,3 +1122,19 @@ function commerce_discount_update_7110() { return t('Discount date and usage sub-modules are now in core.'); } + +/** + * Add a currency column to discount offers. + */ +function commerce_discount_update_7111() { + $schema = commerce_discount_schema(); + // Add the uuid column to the display table. + $table = 'commerce_discount_offer'; + $field = 'currency'; + // Due to a previous failure, the column may already exist: + if (!db_field_exists($table, $field)) { + $spec = $schema[$table]['fields'][$field]; + db_add_field($table, $field, $spec); + $msg[] = t('Added commerce_discount_offer.currency column.'); + } +} \ No newline at end of file diff --git a/commerce_discount.module b/commerce_discount.module index 0f11a1a..2b30c30 100644 --- a/commerce_discount.module +++ b/commerce_discount.module @@ -426,7 +426,7 @@ function commerce_discount_commerce_discount_type_info() { function commerce_discount_commerce_discount_offer_type_info() { $types = array(); $types['fixed_amount'] = array( - 'label' => t('@currency off', array('@currency' => commerce_currency_get_symbol(variable_get('commerce_default_currency', 'USD')))), + 'label' => t('@currency off', array('@currency' => commerce_currency_get_symbol(commerce_default_currency()))), 'action' => 'commerce_discount_fixed_amount', 'entity types' => array('commerce_order', 'commerce_line_item'), ); diff --git a/includes/commerce_discount_offer.inline_entity_form.inc b/includes/commerce_discount_offer.inline_entity_form.inc index c1bc9b9..f991c2f 100644 --- a/includes/commerce_discount_offer.inline_entity_form.inc +++ b/includes/commerce_discount_offer.inline_entity_form.inc @@ -36,6 +36,13 @@ class CommerceDiscountOfferInlineEntityFormController extends EntityInlineEntity $offer->type = $form_state['commerce_dicount_offer_selection_' . $entity_form['#ief_id']]; } + // Always attempt to determine current default currency + $currency = commerce_default_currency($form_state['commerce_discount']); + // If the offer doesn't yet have a currency, set it. + if (empty($offer->currency)) { + $offer->currency = $currency; + } + // Get discount type. $discount_type = commerce_discount_type($form_state['commerce_discount']->type); diff --git a/includes/views/commerce_discount.views.inc b/includes/views/commerce_discount.views.inc index 0d3273d..565c807 100644 --- a/includes/views/commerce_discount.views.inc +++ b/includes/views/commerce_discount.views.inc @@ -83,6 +83,11 @@ function commerce_discount_views_data_alter(&$data) { ); } + // Custom Discount Offer Type handler + if (isset($data['commerce_discount_offer'])) { + $data['commerce_discount_offer']['type']['field']['handler'] = 'commerce_discount_handler_field_type'; + } + // Order table: discount usage relationship. if (isset($data['commerce_order'])) { $data['commerce_order']['discount_usage'] = array( diff --git a/includes/views/handlers/commerce_discount_handler_field_type.inc b/includes/views/handlers/commerce_discount_handler_field_type.inc new file mode 100644 index 0000000..92cb6b3 --- /dev/null +++ b/includes/views/handlers/commerce_discount_handler_field_type.inc @@ -0,0 +1,59 @@ +value_options)) { + return; + } + + if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) { + if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) { + $this->value_options = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']); + } + else { + $this->value_options = call_user_func($this->definition['options callback']); + } + } + else { + $this->value_options = array(); + } + } + + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + } + + function pre_render(&$values) { + $this->get_value_options(); + } + + function render($values) { + $value = $values->{$this->field_alias}; + if (!empty($this->options['machine_name']) || !isset($this->value_options[$value])) { + $result = check_plain($value); + } + // We must attempt to fetch the currency here in the view for fixed amount + // because the label is cached in the entity info to the site default currency. + else if ($value == 'fixed_amount') { + $result = t('@currency off', array('@currency' => commerce_currency_get_symbol(commerce_default_currency()))); + } + else { + $result = $this->value_options[$value]; + } + + return $result; + } +} \ No newline at end of file -- 2.5.4 (Apple Git-61)