1212 */
1313class PromotionStorage extends CommerceContentEntityStorage implements PromotionStorageInterface {
1414
15- /**
16- * Helper method to return base query for valid promotions.
17- *
18- * @param \Drupal\commerce_order\Entity\OrderTypeInterface $order_type
19- * The order type.
20- * @param \Drupal\commerce_store\Entity\StoreInterface $store
21- * The store.
22- *
23- * @return \Drupal\Core\Entity\Query\QueryInterface
24- * The entity query.
25- */
26- protected function loadValidQuery (OrderTypeInterface $ order_type , StoreInterface $ store ) {
27- $ query = $ this ->getQuery ();
28-
29- $ or_condition = $ query ->orConditionGroup ()
30- ->condition ('end_date ' , gmdate ('Y-m-d ' ), '>= ' )
31- ->notExists ('end_date ' , gmdate ('Y-m-d ' ));
32- $ query
33- ->condition ('stores ' , [$ store ->id ()], 'IN ' )
34- ->condition ('order_types ' , [$ order_type ->id ()], 'IN ' )
35- ->condition ('start_date ' , gmdate ('Y-m-d ' ), '<= ' )
36- ->condition ('status ' , TRUE )
37- ->condition ($ or_condition );
38- return $ query ;
39- }
40-
4115 /**
4216 * {@inheritdoc}
4317 */
4418 public function loadValid (OrderTypeInterface $ order_type , StoreInterface $ store ) {
45- $ query = $ this ->loadValidQuery ($ order_type , $ store );
19+ $ query = $ this ->buildLoadQuery ($ order_type , $ store );
20+ // Only load promotions without coupons. Promotions with coupons are loaded
21+ // coupon-first in a different process.
22+ $ query ->notExists ('coupons ' );
4623 $ result = $ query ->execute ();
4724 if (empty ($ result )) {
4825 return [];
@@ -56,7 +33,7 @@ public function loadValid(OrderTypeInterface $order_type, StoreInterface $store)
5633 * {@inheritdoc}
5734 */
5835 public function loadByCoupon (OrderTypeInterface $ order_type , StoreInterface $ store , CouponInterface $ coupon ) {
59- $ query = $ this ->loadValidQuery ($ order_type , $ store );
36+ $ query = $ this ->buildLoadQuery ($ order_type , $ store );
6037 $ query ->condition ('coupons ' , $ coupon ->id ());
6138 $ result = $ query ->execute ();
6239 if (empty ($ result )) {
@@ -68,4 +45,30 @@ public function loadByCoupon(OrderTypeInterface $order_type, StoreInterface $sto
6845
6946 }
7047
48+ /**
49+ * Builds the base query for loading valid promotions.
50+ *
51+ * @param \Drupal\commerce_order\Entity\OrderTypeInterface $order_type
52+ * The order type.
53+ * @param \Drupal\commerce_store\Entity\StoreInterface $store
54+ * The store.
55+ *
56+ * @return \Drupal\Core\Entity\Query\QueryInterface
57+ * The entity query.
58+ */
59+ protected function buildLoadQuery (OrderTypeInterface $ order_type , StoreInterface $ store ) {
60+ $ query = $ this ->getQuery ();
61+
62+ $ or_condition = $ query ->orConditionGroup ()
63+ ->condition ('end_date ' , gmdate ('Y-m-d ' ), '>= ' )
64+ ->notExists ('end_date ' , gmdate ('Y-m-d ' ));
65+ $ query
66+ ->condition ('stores ' , [$ store ->id ()], 'IN ' )
67+ ->condition ('order_types ' , [$ order_type ->id ()], 'IN ' )
68+ ->condition ('start_date ' , gmdate ('Y-m-d ' ), '<= ' )
69+ ->condition ('status ' , TRUE )
70+ ->condition ($ or_condition );
71+ return $ query ;
72+ }
73+
7174}
0 commit comments