Problem/Motivation

I found that my users get frustrated because they receive the error:

You need to enter a value for other tip.

When they clearly have hit a radio button for a tip. It's even still selected for them.

Steps to reproduce

Proposed resolution

Why not use the submitPaneForm functionality to add the tip to the order?

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

  • 3564305-make-it-so Comparecompare

Comments

generalredneck created an issue.

generalredneck’s picture

So The error message here was part of #3564307: Cannot add tip. That said, this is still a usability option IMO incase they forget to hit the "Add" button but have an item selected. I think this will make it so that store operators dont' miss out on much wanted tip transactions due to a usability issue and the user not noticing or not wanting to go back and correct it.

generalredneck’s picture

As an FYI this will require some duplication of AddTip since that function and the submitInlineForm() function seem to handle how to grab from $form_state a bit differently.

generalredneck’s picture

FYI I created a custom module to overload the contrib module's InlineForm because I wanted to do some other work around building the form as well... This is what I ended up with in my submit handler:

  public function submitInlineForm(array &$inline_form, FormStateInterface $form_state) {
    $values = $form_state->getValue($inline_form['#parents']);
    $inline_configuration = $form_state->get('inline_configuration');
    /** @var \Drupal\commerce_order\Entity\Order $order */
    $order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($inline_configuration['order_id']);
    $total_price = $order->getTotalPrice();
    $tip = $values['tip_info']['tip'];
    if ($values['tip_info']['tip_options'] && $values['tip_info']['tip'] == 'other') {
      $tip = $values['tip_info']['other_tip'];
    }
    elseif ($values['tip_info']['tip_options'] && $values['tip_info']['tip'] !== 'none') {
      $tip = round((float) $total_price->getNumber() * (float) $values['tip_info']['tip'], 2);
    }
    if ($values['tip_info']['tip'] !== 'none' && !empty($tip)) {
      $order->addAdjustment(new Adjustment([
        'type' => 'custom',
        'label' => 'Tip',
        'amount' => new Price($tip, $total_price->getCurrencyCode()),
        'locked' => TRUE,
        'source_id' => 'custom',
        'percentage' => NULL,
        'included' => FALSE,
      ]))->save();
    }
  }

The differing code is in the first lines... addTip has

    $triggering_element = $form_state->getTriggeringElement();
    $parents = array_slice($triggering_element['#parents'], 0, -1);
    $values = $form_state->getValue($parents);

Where mine has

    $values = $form_state->getValue($inline_form['#parents']);

and then goes on to reference the values in the $values variable using the full array key syntax like $values['tip_info']['tip'] instead of $values['tip']

Hope this helps.