diff --git a/commerce_coupon.module b/commerce_coupon.module index 1c526f4..6e3f95e 100644 --- a/commerce_coupon.module +++ b/commerce_coupon.module @@ -821,40 +821,24 @@ function commerce_coupon_line_item_title($line_item) { } /** - * @TODO: Implement this method correct - * - * Returns the elements necessary to add a product line item through a line item - * manager widget. + * Add coupon line item form + * Returns the elements necessary to add a coupon line item + * through the line item manager widget. */ -function commerce_coupon_line_item_add_form($form_state) { - $order = $form_state['commerce_order']; +function commerce_coupon_line_item_add_form($element, &$form_state) { $form = array(); - - $form['amount'] = array( + + $form['coupon_code'] = array( '#type' => 'textfield', - '#title' => t('Amount'), - '#default_value' => $default_amount, - '#size' => 10, + '#title' => t('Coupon Code'), + '#description' => t('Enter the coupon code here.'), ); - - // Build a currency options list from all enabled currencies. - $options = array(); - foreach (commerce_currencies(TRUE) as $currency_code => $currency) { - $options[$currency_code] = check_plain($currency['code']); - } - - $form['currency_code'] = array( - '#type' => 'select', - '#title' => t('Currency'), - '#options' => $options, - '#default_value' => commerce_default_currency(), - ); - + return $form; } /** - * Adds the selected shippng information to a line item added via a line item + * Adds the selected coupon to a line item added via a line item * manager widget. * * @param $line_item @@ -870,26 +854,25 @@ function commerce_coupon_line_item_add_form($form_state) { * NULL if all is well or an error message if something goes wrong. */ function commerce_coupon_line_item_add_form_submit(&$line_item, $element, &$form_state, $form) { - // TODO: Implement this method correct - $order = $form_state['commerce_order']; - - // Populate the line item with the product data. - $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item); - //$line_item_wrapper->shipping_method = $element['actions']['shipping_method']['#value']; - $line_item_wrapper->commerce_unit_price->amount = $element['actions']['amount']['#value']; - $line_item_wrapper->commerce_unit_price->currency_code = $element['actions']['currency_code']['#value']; - $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add( - $line_item_wrapper->commerce_unit_price->value(), - 'base_price', - $line_item_wrapper->commerce_unit_price->value(), - TRUE, - FALSE - ); + + // Load the selected coupon + if ($coupon = commerce_coupon_load_by_code($element['actions']['coupon_code']['#value'])) { + if( ! commerce_coupon_code_is_valid($coupon->commerce_coupon_code[LANGUAGE_NONE][0]['value'], $order) ){ + return t('The selected coupon code is not valid for this order'); + } + // Apply coupon to order + commerce_coupon_redeem_coupon($coupon, $order); + // Replace line_item with new line_item populated with coupon details for this order + $line_item = commerce_coupon_line_item_new($coupon, 1, $order->order_id); + } + else { + return t('You have entered an invalid coupon code.'); + } } /** - * Creates a new product line item populated with the proper product values. + * Creates a new coupon line item populated with the proper coupon values. * * @param $commerce_coupon * The fully loaded coupon to add. diff --git a/includes/commerce_coupon.checkout_pane.inc b/includes/commerce_coupon.checkout_pane.inc index cf566cc..580730f 100644 --- a/includes/commerce_coupon.checkout_pane.inc +++ b/includes/commerce_coupon.checkout_pane.inc @@ -17,7 +17,7 @@ function commerce_coupon_pane_checkout_form($form, &$form_state, $checkout_pane, $pane_form['coupon_code'] = array( '#type' => 'textfield', '#title' => t('Coupon Code'), - '#description' => t('Enter here your coupon code.'), + '#description' => t('Enter your coupon code here.'), ); // Extract the View and display keys from the cart contents pane setting.