diff --git a/commerce_coupon.info b/commerce_coupon.info index 69fc229..0a3e43a 100644 --- a/commerce_coupon.info +++ b/commerce_coupon.info @@ -13,6 +13,7 @@ files[] = includes/views/handlers/commerce_coupon_handler_field_coupon_edit.inc files[] = includes/views/handlers/commerce_coupon_handler_field_coupon_link.inc files[] = includes/views/handlers/commerce_coupon_handler_argument_coupon_id.inc files[] = includes/views/handlers/commerce_coupon_handler_argument_coupon_code.inc +files[] = includes/views/handlers/commerce_coupon_handler_area_cart_form.inc files[] = commerce_coupon.info.inc files[] = commerce_coupon.test diff --git a/commerce_coupon.module b/commerce_coupon.module index ee01cab..8e302c7 100644 --- a/commerce_coupon.module +++ b/commerce_coupon.module @@ -788,7 +788,7 @@ function commerce_coupon_redeem_coupon_code($code, $order, &$error) { $coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon); - if (commerce_coupon_evaluate_conditions($coupon_wrapper, $order_wrapper)) { + if (empty($error) && commerce_coupon_evaluate_conditions($coupon_wrapper, $order_wrapper)) { // Add the coupon to the order. $order_wrapper->commerce_coupons[] = $coupon_wrapper->value(); diff --git a/commerce_coupon.test b/commerce_coupon.test index be4bbff..e37183e 100644 --- a/commerce_coupon.test +++ b/commerce_coupon.test @@ -235,6 +235,44 @@ class CommerceCouponTest extends CommerceCouponTestBase { } + public function testCommerceCouponUICartForm() { + // Login as administrator and add the field to the footer of the cart View. + $this->drupalLogin($this->store_admin); + $view = views_get_view('commerce_cart_form'); + $view->add_item('default', 'footer', 'commerce_order', 'coupon_cart_form'); + $view->save(); + + // Create a cart order and ensure we can see the code form on the cart + // management page. + $this->createDummyOrder($this->store_admin->uid, array($this->product->product_id => 1), $status = 'cart'); + $this->drupalGet('cart'); + $this->assertFieldByName('coupon_code', NULL, 'Coupon code field is present on cart management page.'); + $this->assertFieldByName('coupon_add', NULL, 'Coupon add submit button is present on cart management page.'); + + // Test coupon code validation. + $this->drupalPost(NULL, array(), t('Add coupon')); + $this->assertText(t('Please enter a coupon code.'), 'Validation message for missing code.'); + // Load a fresh form. + $this->drupalGet('cart'); + $values = array('coupon_code' => 'BLAH'); + $this->drupalPost(NULL, $values, t('Add coupon')); + $this->assertText(t('Please enter a valid coupon code.'), 'Validation message for code that doesn\'t exist.'); + + // Create a discount and add a coupon to it. Test it applies correctly. + $discount = $this->createDiscount('order_discount', 'fixed_amount', 300); + $coupon = commerce_coupon_create('discount_coupon'); + $coupon->code = 'HALF OFF'; + $coupon->commerce_discount_reference['und'][0]['target_id'] = $discount->discount_id; + commerce_coupon_save($coupon); + // Load a fresh form. + $this->drupalGet('cart'); + $values = array('coupon_code' => 'HALF OFF'); + $this->drupalPost(NULL, $values, t('Add coupon')); + $this->assertText(t('Coupon code applied.'), 'Coupon applied message for valid code.'); + + + } + public function testCommerceCouponTypeAccess() { // Login with store admin. $this->drupalLogin($this->store_admin); diff --git a/includes/views/commerce_coupon.views.inc b/includes/views/commerce_coupon.views.inc index 231e606..ce2d723 100644 --- a/includes/views/commerce_coupon.views.inc +++ b/includes/views/commerce_coupon.views.inc @@ -74,6 +74,15 @@ function commerce_coupon_views_data_alter(&$data) { $data['commerce_coupon']['coupon_id']['argument'] = array( 'handler' => 'commerce_coupon_handler_argument_coupon_id', ); + + // Expose the coupon form on the cart form. + $data['commerce_order']['coupon_cart_form'] = array( + 'title' => t('Coupon cart form'), + 'help' => t('Coupon cart form'), + 'area' => array( + 'handler' => 'commerce_coupon_handler_area_cart_form', + ), + ); } } diff --git a/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc b/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc new file mode 100644 index 0000000..1659703 --- /dev/null +++ b/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc @@ -0,0 +1,181 @@ + 'textfield', + '#title' => t('Form item weight'), + '#default_value' => $this->options['weight'], + '#required' => TRUE, + ); + } + + function options_validate(&$form, &$form_state) { + $weight = $form_state['values']['options']['weight']; + // Weight must be an integer: + if (!is_null($weight )&& (!is_numeric($weight)) || (int) $weight != $weight) { + form_set_error('options][weight', t('!name field must be an integer.', array('!name' => $form['weight']['#title']))); + } + } + + function render($values = FALSE) { + // Render a Views form item placeholder. + // This causes Views to wrap the View in a form. + return ''; + } + + /** + * This handler never outputs data when the view is empty. + */ + function views_form_empty($empty) { + return $empty; + } + + function views_form(&$form, &$form_state) { + + // Ensure this include file is loaded when the form is rebuilt from the cache. + $form_state['build_info']['files']['coupon_cart_form'] = drupal_get_path('module', 'commerce_coupon') . '/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc'; + + $form[$this->options['id']] = array( + '#prefix' => '