public static function CouponRedemption::removeCoupon in Commerce Core 8.2
Submit callback for the "Remove coupon" button.
File
- modules/
promotion/ src/ Plugin/ Commerce/ InlineForm/ CouponRedemption.php, line 243
Class
- CouponRedemption
- Provides an inline form for redeeming a coupon.
Namespace
Drupal\commerce_promotion\Plugin\Commerce\InlineFormCode
public static function removeCoupon(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
$parents = array_slice($triggering_element['#parents'], 0, -1);
$inline_form = NestedArray::getValue($form, $parents);
$order_storage = \Drupal::entityTypeManager()
->getStorage('commerce_order');
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $order_storage
->load($inline_form['#configuration']['order_id']);
$coupon_ids = array_column($order
->get('coupons')
->getValue(), 'target_id');
$coupon_index = array_search($triggering_element['#coupon_id'], $coupon_ids);
$order
->get('coupons')
->removeItem($coupon_index);
$order
->save();
$form_state
->setRebuild();
}