From 5a3b6a198c901c9c26eea5ba9599d3542c4197a1 Mon Sep 17 00:00:00 2001 From: sumanthkumarc Date: Fri, 11 Aug 2017 16:45:07 +0530 Subject: [PATCH] Issue #2901504 by sumanthkumarc: Provide limit by no of orders per user condition for promotions in commerce core --- .../Commerce/Condition/OrdersPerUser.php | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 modules/order/src/Plugin/Commerce/Condition/OrdersPerUser.php diff --git a/modules/order/src/Plugin/Commerce/Condition/OrdersPerUser.php b/modules/order/src/Plugin/Commerce/Condition/OrdersPerUser.php new file mode 100644 index 0000000000..b9c2c6ae6f --- /dev/null +++ b/modules/order/src/Plugin/Commerce/Condition/OrdersPerUser.php @@ -0,0 +1,144 @@ +orderStorage = $entity_type_manager->getStorage('commerce_order'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'operator' => '>', + 'orders' => 1, + ] + parent::defaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + + $form['operator'] = [ + '#type' => 'select', + '#title' => t('Operator'), + '#options' => $this->getComparisonOperators(), + '#default_value' => $this->configuration['operator'], + '#required' => TRUE, + ]; + $form['orders'] = [ + '#type' => 'number', + '#title' => t('Orders'), + '#default_value' => $this->configuration['orders'], + '#min' => 1, + '#required' => TRUE, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::submitConfigurationForm($form, $form_state); + + $values = $form_state->getValue($form['#parents']); + $this->configuration['operator'] = $values['operator']; + $this->configuration['orders'] = $values['orders']; + } + + /** + * {@inheritdoc} + */ + public function evaluate(EntityInterface $entity) { + $this->assertEntity($entity); + /* @var \Drupal\commerce_order\Entity\OrderInterface $order */ + $order = $entity; + $user_id = $order->getCustomerId(); + + $orders = $this->orderStorage->getQuery() + ->condition('cart', FALSE) + ->condition('uid', $user_id) + ->count() + ->execute(); + + switch ($this->configuration['operator']) { + case '>=': + return $orders >= $this->configuration['orders']; + + case '>': + return $orders > $this->configuration['orders']; + + case '<=': + return $orders <= $this->configuration['orders']; + + case '<': + return $orders < $this->configuration['orders']; + + case '==': + return $orders == $this->configuration['orders']; + + default: + throw new \InvalidArgumentException("Invalid operator {$this->configuration['operator']}"); + } + } + +}