Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2863864 - Disable promotions via cron if end date or usage limit hit #708

Open
wants to merge 21 commits into
base: 8.x-2.x
Choose a base branch
from

Conversation

swickham78
Copy link

No description provided.

Copy link
Contributor

@mglaman mglaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few changes and no tests. We definitely need to test this.

I don't think we need to use a queue worker. But I'd like to use more of the API for querying the promotions. Even if this means running an entity query to load invalid via end date. Then load valid and check their usages and add to list of promotions to exired/disable.

I'm not too worried on the performance since this should only be invoked during cron.

/**
* Get all expired promotions that are still active and disable them.
*/
function commerce_promotion_disabled_expired() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the storage provides a method to load expired, we can just keep this within the cron, no need for its own function. If people want to run this manually then they can.


if ($promotions !== FALSE) {
// Disable all expired promotions.
foreach ($promotions as $promotion) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bojanz I think this is fine versus a queue. There shouldn't be that much activity, generally.

$usage_query = $this->database->select('commerce_promotion_usage', 'cpu');
$usage_query->addExpression('COUNT(cpu.promotion_id)', 'count');
$usage_query->where('cpu.promotion_id = cpd.promotion_id');
$usage_query->groupBy('cpu.promotion_id');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have this query in the usage service :: getUsageMultiple. It feels like we should rework this method a bit so it can be used.

https://github.com/drupalcommerce/commerce/blob/8.x-2.x/modules/promotion/src/PromotionUsage.php#L65

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if I add another method to PromotionStorage that will load and check promotion uses by calling getUsageMultiple? That way I can run a quick query to only pass promotions to it which are active and have a usage set.

I think the only way I can mess around with getUsageMultiple to handle return usage on its own would be to be just make it load all promotions if none are passed which could be excessive even if we're not too concerned w/ performance.

Thoughts?


// We want to get results of queries that have passed their final date or
// have met their max usage.
$or_condition = $query->orConditionGroup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using a regular select, when we can run $this->getQuery().

I'd actually rather do a load of ->condition('end_date', gmdate('Y-m-d'), '<') and ->condition('status', TRUE)

We need < because it's valid if >=. This would cancel promotions on their last day.

$query->condition('cpd.status', 1);
}

$result = $query->execute()->fetchCol(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We return the invalid and set them as disabled. Then I'd say run the usage checks afterwards for anything not yet expired

@mglaman
Copy link
Contributor

mglaman commented Apr 7, 2017

@swickham78 at quick glance this looks good, better change. Bojan made some changes which are causing conflict.

@swickham78
Copy link
Author

@mglaman Conflicts have been addressed.

@mglaman
Copy link
Contributor

mglaman commented Apr 8, 2017

@swickham78 okay, cool. Errors are just PHPCS. I'll get around to a proper review

Copy link
Contributor

@mglaman mglaman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The storage methods are tested, but the cron op is still not tested.

@@ -104,4 +104,61 @@ public function loadAvailable(OrderTypeInterface $order_type, StoreInterface $st
return $promotions;
}

/**
* Return active promotions that have passed their end date.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document blocks should only be on the interface, implementers just do {@inheritdoc}

}

/**
* Returns active promotions which have a met their maximum usage.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document blocks should only be on the interface, implementers just do {@inheritdoc}

* @return \Drupal\commerce_promotion\Entity\PromotionInterface[]
* Promotions with maxed usage.
*/
public function loadMaxedUsage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bojanz name bikeshedding.

$this->assertEquals(SAVED_NEW, $expired_promotion->save());

$promotions = $this->promotionStorage->loadExpired();
$this->assertEquals(count($promotions), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do assertCount

@swickham78
Copy link
Author

@mglaman Completely forgot this was left unresolved. Just pushed a new commit with adjustments based on your last notes.

@swickham78
Copy link
Author

@mglaman Looks like this got lost in the shuffle. Nothing changed since the last push, just did a rebase and fixed some conflicts that came form it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants