The current_usage field won't cut it, cause we'll need to track per-user usage as well in the future.

We need to remove that field and implement a custom service with custom storage, which keeps track of the promotion_id, order_id, additional_id (i.e coupon code), uid.
We also need to figure out whether we'll track usage for anonymous users or not (if yes, we need to find the most stable id: IP address like flood, or session id? Probably the session id).

Methods would be something like register() and check().

Comments

bojanz created an issue. See original summary.

mglaman’s picture

Assigned: Unassigned » mglaman

I'll be working on something like this, so claiming ticket for a while.

mglaman’s picture

Component: Developer experience » Promotions

If we track orders by session ID, I'd say we need to track by that here as well.

mglaman’s picture

I just realized this is inherently impossible because the offer plugin applies the adjustment,not the promotion. So the source ID is wrong.

  /**
   * {@inheritdoc}
   */
  public function applyAdjustment(EntityAdjustableInterface $entity, Price $amount) {
    $entity->addAdjustment(new Adjustment([
      'type' => 'promotion',
      // @todo Change to label from UI when added in #2770731.
      'label' => t('Discount'),
      'amount' => $amount->multiply('-1'),
      'source_id' => $this->getPluginId(),
    ]));
  }
bojanz’s picture

The adjustment should be pointing to the promotion, that's a bug.

mglaman’s picture

Status: Active » Needs work

Yep, new code is on https://github.com/drupalcommerce/commerce/pull/695. Fixes that, slowly getting my test to pass.

mglaman’s picture

Status: Needs work » Needs review

PR at https://github.com/drupalcommerce/commerce/pull/695 should have passing tests. All kernel tests passed locally for promotions. Letting Travis do the rest.

bojanz’s picture

Assigned: mglaman » bojanz

Wrapping this up.

  • bojanz committed 5ab9e14 on 8.x-2.x authored by mglaman
    Issue #2763705 by mglaman, bojanz: Implement a usage API
    
bojanz’s picture

Status: Needs review » Fixed

Final version committed.

We implemented usage tracking for anonymous customers and made both anon and authenticated customers tracked by email.
Will need a followup to update usage if the user changes their email.

Utilvideo’s picture

Documentation for API ?:)

bojanz’s picture

@Regnoy
What are you trying to do?

This is an internal API meant to be used by other parts of the Promotion module, it's not meant to be consumed by developers (other than perhaps getting current usage for display purpose). The code is the documentation.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.