commerce icon indicating copy to clipboard operation
commerce copied to clipboard

Issue #2901504 by sumanthkumarc: Provide limit by no of orders per user condition for promotions in commerce core

Open sumanthkumarc opened this issue 8 years ago • 4 comments

sumanthkumarc avatar Aug 11 '17 11:08 sumanthkumarc

@sumanthkumarc How is this different than limited per user?

mglaman avatar Aug 11 '17 13:08 mglaman

@mglaman Thanks for the review :) When you say limited per user? you mean the option "Usage limits" available in the right hand panes on promotions form?? If so , i saw that no where the mail is passed to check availability per user, also i guess its confusing for user to understand if this is total limit irrespective of users or per user limit. See this available method from promotion class.

public function available(OrderInterface $order) {
    if (!$this->isEnabled()) {
      return FALSE;
    }
    if (!in_array($order->bundle(), $this->getOrderTypeIds())) {
      return FALSE;
    }
    if (!in_array($order->getStoreId(), $this->getStoreIds())) {
      return FALSE;
    }
    $time = \Drupal::time()->getRequestTime();
    if ($this->getStartDate()->format('U') > $time) {
      return FALSE;
    }
    $end_date = $this->getEndDate();
    if ($end_date && $end_date->format('U') <= $time) {
      return FALSE;
    }
    if ($usage_limit = $this->getUsageLimit()) {
      /** @var \Drupal\commerce_promotion\PromotionUsageInterface $usage */
      $usage = \Drupal::service('commerce_promotion.usage');
      if ($usage_limit <= $usage->getUsage($this)) {
        return FALSE;
      }
    }

    return TRUE;
  }

let me know if i'm missing anything here :)

sumanthkumarc avatar Aug 14 '17 07:08 sumanthkumarc

@mglaman Rate/usage limiting is not the same thing. That is "a user can only receive this promotion once". This is "the user can only receive this promotion if this is his first order (or one of the first N)".

bojanz avatar Aug 15 '17 11:08 bojanz

@bojanz @mglaman Even then, below is the getUsage method, which takes $mail for rate limiting, and i wasn't able to find any instance where, getUsage() method is passed with mail, everywhere either promotion object or coupon object is being passed.

/**
   * {@inheritdoc}
   */
  public function getUsage(PromotionInterface $promotion, CouponInterface $coupon = NULL, $mail = NULL) {
    $coupons = $coupon ? [$coupon] : [];
    $usages = $this->getUsageMultiple([$promotion], $coupons, $mail);
    return $usages[$promotion->id()];
  }

UPDATE: According to bojan, this is the intended behaviour and per user rate limiting is new feature.

sumanthkumarc avatar Aug 16 '17 04:08 sumanthkumarc