|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\commerce_promotion\Element; |
| 4 | + |
| 5 | +use Drupal\commerce\Element\CommerceElementBase; |
| 6 | +use Drupal\commerce_order\Entity\OrderInterface; |
| 7 | +use Drupal\commerce_promotion\Entity\CouponInterface; |
| 8 | +use Drupal\commerce_promotion\Entity\PromotionInterface; |
| 9 | +use Drupal\Core\Form\FormStateInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides a form element for embedding the coupon redemption form. |
| 13 | + * |
| 14 | + * Usage example: |
| 15 | + * @code |
| 16 | + * $form['coupons'] = [ |
| 17 | + * '#type' => 'commerce_coupon_redemption_form', |
| 18 | + * // The order to which the coupon will be applied to. |
| 19 | + * '#order' => $order, |
| 20 | + * ]; |
| 21 | + * @endcode |
| 22 | + * |
| 23 | + * @RenderElement("commerce_coupon_redemption_form") |
| 24 | + */ |
| 25 | +class CouponRedemptionForm extends CommerceElementBase { |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function getInfo() { |
| 31 | + $class = get_class($this); |
| 32 | + return [ |
| 33 | + // The order to which the coupon will be applied to. |
| 34 | + '#order' => NULL, |
| 35 | + '#process' => [ |
| 36 | + [$class, 'attachElementSubmit'], |
| 37 | + [$class, 'processForm'], |
| 38 | + ], |
| 39 | + '#element_validate' => [ |
| 40 | + [$class, 'validateElementSubmit'], |
| 41 | + [$class, 'validateForm'], |
| 42 | + ], |
| 43 | + '#commerce_element_submit' => [ |
| 44 | + [$class, 'submitForm'], |
| 45 | + ], |
| 46 | + '#theme_wrappers' => ['container'], |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Builds the coupon redemption form. |
| 52 | + * |
| 53 | + * @param array $element |
| 54 | + * The form element being processed. |
| 55 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 56 | + * The current state of the form. |
| 57 | + * @param array $complete_form |
| 58 | + * The complete form structure. |
| 59 | + * |
| 60 | + * @throws \InvalidArgumentException |
| 61 | + * Thrown when the #order property is empty or invalid entity. |
| 62 | + * |
| 63 | + * @return array |
| 64 | + * The processed form element. |
| 65 | + */ |
| 66 | + public static function processForm(array $element, FormStateInterface $form_state, array &$complete_form) { |
| 67 | + if (empty($element['#order'])) { |
| 68 | + throw new \InvalidArgumentException('The commerce_coupon_redemption_form element requires the #order property.'); |
| 69 | + } |
| 70 | + if (!$element['#order'] instanceof OrderInterface) { |
| 71 | + throw new \InvalidArgumentException('The commerce_coupon_redemption_form #order property must be an order entity.'); |
| 72 | + } |
| 73 | + |
| 74 | + $id_prefix = implode('-', $element['#parents']); |
| 75 | + // @todo We cannot use unique IDs, or multiple elements on a page currently. |
| 76 | + // @see https://www.drupal.org/node/2675688 |
| 77 | + // $wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper'); |
| 78 | + $wrapper_id = $id_prefix . '-ajax-wrapper'; |
| 79 | + |
| 80 | + $form_state->set('order', $element['#order']); |
| 81 | + $element = [ |
| 82 | + '#tree' => TRUE, |
| 83 | + '#prefix' => '<div id="' . $wrapper_id . '">', |
| 84 | + '#suffix' => '</div>', |
| 85 | + // Pass the id along to other methods. |
| 86 | + '#wrapper_id' => $wrapper_id, |
| 87 | + ] + $element; |
| 88 | + $element['code'] = [ |
| 89 | + '#type' => 'textfield', |
| 90 | + '#title' => t('Coupon code'), |
| 91 | + '#description' => t('Enter your coupon code here.'), |
| 92 | + ]; |
| 93 | + $element['apply'] = [ |
| 94 | + '#type' => 'submit', |
| 95 | + '#value' => t('Apply coupon'), |
| 96 | + '#name' => 'apply_coupon', |
| 97 | + '#limit_validation_errors' => [ |
| 98 | + array_merge($element['#parents'], ['code']), |
| 99 | + ], |
| 100 | + ]; |
| 101 | + |
| 102 | + return $element; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Validates the coupon redemption form. |
| 107 | + * |
| 108 | + * @param array $element |
| 109 | + * The form element. |
| 110 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 111 | + * The current state of the form. |
| 112 | + */ |
| 113 | + public static function validateForm(array &$element, FormStateInterface $form_state) { |
| 114 | + $coupon_parents = array_merge($element['#parents'], ['code']); |
| 115 | + $coupon_code = $form_state->getValue($coupon_parents); |
| 116 | + if (empty($coupon_code)) { |
| 117 | + return; |
| 118 | + } |
| 119 | + $code_path = implode('][', $coupon_parents); |
| 120 | + /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ |
| 121 | + $order = $form_state->get('order'); |
| 122 | + $entity_type_manager = \Drupal::entityTypeManager(); |
| 123 | + |
| 124 | + /** @var \Drupal\commerce_promotion\CouponStorageInterface $coupon_storage */ |
| 125 | + $coupon_storage = $entity_type_manager->getStorage('commerce_promotion_coupon'); |
| 126 | + $coupon = $coupon_storage->loadByCode($coupon_code); |
| 127 | + if (empty($coupon)) { |
| 128 | + $form_state->setErrorByName($code_path, t('Coupon is invalid')); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + foreach ($order->get('coupons') as $item) { |
| 133 | + if ($item->target_id == $coupon->id()) { |
| 134 | + $form_state->setErrorByName($code_path, t('Coupon has already been redeemed')); |
| 135 | + return; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + $order_type_storage = $entity_type_manager->getStorage('commerce_order_type'); |
| 140 | + /** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */ |
| 141 | + $promotion_storage = $entity_type_manager->getStorage('commerce_promotion'); |
| 142 | + |
| 143 | + /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */ |
| 144 | + $order_type = $order_type_storage->load($order->bundle()); |
| 145 | + $promotion = $promotion_storage->loadByCoupon($order_type, $order->getStore(), $coupon); |
| 146 | + if (empty($promotion)) { |
| 147 | + $form_state->setErrorByName($code_path, t('Coupon is invalid')); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + if (!self::couponApplies($order, $promotion, $coupon)) { |
| 152 | + $form_state->setErrorByName($code_path, t('Coupon is invalid')); |
| 153 | + return; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Submits the coupon redemption form. |
| 159 | + * |
| 160 | + * @param array $element |
| 161 | + * The form element. |
| 162 | + * @param \Drupal\Core\Form\FormStateInterface $form_state |
| 163 | + * The current state of the form. |
| 164 | + */ |
| 165 | + public static function submitForm(array &$element, FormStateInterface $form_state) { |
| 166 | + $coupon_parents = array_merge($element['#parents'], ['code']); |
| 167 | + $coupon_code = $form_state->getValue($coupon_parents); |
| 168 | + /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ |
| 169 | + $order = $form_state->get('order'); |
| 170 | + $entity_type_manager = \Drupal::entityTypeManager(); |
| 171 | + $order_type_storage = $entity_type_manager->getStorage('commerce_order_type'); |
| 172 | + /** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */ |
| 173 | + $promotion_storage = $entity_type_manager->getStorage('commerce_promotion'); |
| 174 | + /** @var \Drupal\commerce_promotion\CouponStorageInterface $coupon_storage */ |
| 175 | + $coupon_storage = $entity_type_manager->getStorage('commerce_promotion_coupon'); |
| 176 | + |
| 177 | + $coupon = $coupon_storage->loadByCode($coupon_code); |
| 178 | + /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */ |
| 179 | + $order_type = $order_type_storage->load($order->bundle()); |
| 180 | + $promotion = $promotion_storage->loadByCoupon($order_type, $order->getStore(), $coupon); |
| 181 | + |
| 182 | + if (self::couponApplies($order, $promotion, $coupon)) { |
| 183 | + $order->get('coupons')->appendItem($coupon); |
| 184 | + $order->save(); |
| 185 | + drupal_set_message(t('Coupon applied')); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Checks if a coupon applies. |
| 191 | + * |
| 192 | + * @param \Drupal\commerce_order\Entity\OrderInterface $order |
| 193 | + * The order. |
| 194 | + * @param \Drupal\commerce_promotion\Entity\PromotionInterface $promotion |
| 195 | + * The promotion. |
| 196 | + * @param \Drupal\commerce_promotion\Entity\CouponInterface $coupon |
| 197 | + * The coupon. |
| 198 | + * |
| 199 | + * @return bool |
| 200 | + * Returns TRUE if the coupon applies, FALSE otherwise. |
| 201 | + */ |
| 202 | + protected static function couponApplies(OrderInterface $order, PromotionInterface $promotion, CouponInterface $coupon) { |
| 203 | + if ($promotion->applies($order)) { |
| 204 | + return TRUE; |
| 205 | + } |
| 206 | + else { |
| 207 | + foreach ($order->getItems() as $orderItem) { |
| 208 | + if ($promotion->applies($orderItem)) { |
| 209 | + return TRUE; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + return FALSE; |
| 215 | + } |
| 216 | + |
| 217 | +} |
0 commit comments