suggested update for usability

Created on 2 April 2025, 4 days ago

there isn't the ability to set percentages (at least for drupal 10.4.4)

setting a number for the tip in increments of 1 cents isn't very useful so I suggest the tip inline form gets changed to the following (apologies I don't think I can commit code to this project?)

public function buildInlineForm(array $inline_form, FormStateInterface $form_state) {
$inline_form = parent::buildInlineForm($inline_form, $form_state);
/** @var \Drupal\commerce_order\Entity\Order $order */
$order = $this->entityTypeManager->getStorage('commerce_order')->load($this->configuration['order_id']);
if (!$order) {
throw new \RuntimeException('Invalid order_id given to the coupon_redemption inline form.');
}
assert($order instanceof OrderInterface);
$form_state->set('inline_configuration', $this->getConfiguration());
$inline_form = [
'#tree' => TRUE,
] + $inline_form;

if ($this->configuration['tip_description'] && $this->configuration['tip_description']['value'] && $this->configuration['tip_description']['format']) {
$inline_form['tip_description'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['tip-description-container'],
],
];
$inline_form['tip_description']['description'] = [
'#type' => 'processed_text',
'#text' => $this->configuration['tip_description']['value'],
'#format' => $this->configuration['tip_description']['format'],
];
}
$inline_form['tip_info'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['tip-info-container'],
],
];
$inline_form['tip_info']['tip'] = [
'#type' => 'select',
'#options' => [10=>'10%',15=> '15%', 18=> '18%', 20=>'20%'],
];
$inline_form['tip_info']['add_tip'] = [
'#type' => 'submit',
'#value' => t('Add'),
'#name' => 'add_tip',
'#limit_validation_errors' => [
$inline_form['#parents'],
],
'#submit' => [[get_called_class(), 'addTip']],
'#ajax' => [
'callback' => [get_called_class(), 'ajaxRefreshForm'],
'element' => $inline_form['#parents'],
],
];
$adjustments = $order->getAdjustments();
$hide_tip = FALSE;

foreach ($adjustments as $index => $adjustment) {
$type = $adjustment->getType();
$label = $adjustment->getLabel();
$sourceId = $adjustment->getSourceId();
if ($type == 'custom' && $sourceId == 'custom' && $label == 'Tip') {
$hide_tip = TRUE;
/** @var \Drupal\commerce_price\Price $amount */
$amount = $adjustment->getAmount();
$tip_value_formatter = $this->currencyFormatter->format($amount->getNumber(), $amount->getCurrencyCode());
$inline_form[$index] = [
'#type' => 'container',
'#attributes' => [
'class' => ['tip-index--container'],
],
];
$inline_form[$index]['tip_value'] = [
'#type' => 'label',
'#title' => $tip_value_formatter,
'#title_display' => 'hidden',
];
$inline_form[$index]['remove_tip'] = [
'#type' => 'submit',
'#value' => t('Remove'),
'#name' => 'remove_tip_' . $index,
'#ajax' => [
'callback' => [get_called_class(), 'ajaxRefreshForm'],
'element' => $inline_form['#parents'],
],
'#weight' => 50,
'#limit_validation_errors' => [
$inline_form['#parents'],
],
'#adjustment_index' => $index,
'#submit' => [[get_called_class(), 'removeTip']],
// Simplify ajaxRefresh() by having all triggering elements
// on the same level.
'#parents' => array_merge($inline_form['#parents'], ['remove_tip_' . $index]),
];
}
}
if ($hide_tip) {
// Don't allow additional tip to be added.
$inline_form['tip_info']['tip']['#access'] = FALSE;
$inline_form['tip_info']['add_tip']['#access'] = FALSE;
}

return $inline_form;
}

/**
* Submit callback for the "Add Tip" button.
*/
public static function addTip(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
$parents = array_slice($triggering_element['#parents'], 0, -1);
$values = $form_state->getValue($parents);
if (!empty($values['tip'])) {
$inline_configuration = $form_state->get('inline_configuration');
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($inline_configuration['order_id']);
$total_price = $order->getTotalPrice();
$tip=(int)$values['tip'];
$adj=$total_price->getNumber()*$tip/100;
$order->addAdjustment(new Adjustment([
'type' => 'custom',
'label' => 'Tip',
'amount' => new Price($adj, $total_price->getCurrencyCode()),
'locked' => TRUE,
'source_id' => 'custom',
'percentage' => NULL,
'included' => FALSE,
]))->save();
}
$form_state->setRebuild();
}

Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇨🇦Canada bakerrac12

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @bakerrac12
  • 🇻🇳Vietnam hungtrv99

    Thank you for your feedback!

    Currently, my idea is to allow users to configure the tip options, making it easier for them to modify. Your code seems to have the tip options hardcoded.

    Regarding the step value for the number input, I have already released a version that reduces it from 0.1 to 0.01.

    Thanks again for your interest in my small project!

Production build 0.71.5 2024