diff --git a/lib/Models/Segment.php b/lib/Models/Segment.php index 158217e916..d3758d9f35 100644 --- a/lib/Models/Segment.php +++ b/lib/Models/Segment.php @@ -155,6 +155,24 @@ class Segment extends Model { return $wc_segment; } + static function shouldShowWooCommerceSegment() { + $is_woocommerce_active = class_exists('WooCommerce'); + $woocommerce_users_count = Subscriber::where('is_woocommerce_user', 1)->count(); + + if (!$is_woocommerce_active && $woocommerce_users_count === 0) { + return false; + } + return true; + } + + static function getSegmentTypes() { + $types = [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS]; + if (Segment::shouldShowWooCommerceSegment()) { + $types[] = Segment::TYPE_WC_USERS; + } + return $types; + } + static function search($orm, $search = '') { return $orm->whereLike('name', '%' . $search . '%'); } @@ -185,7 +203,7 @@ class Segment extends Model { static function getSegmentsWithSubscriberCount($type = self::TYPE_DEFAULT) { $query = self::selectMany([self::$_table . '.id', self::$_table . '.name']) - ->whereIn('type', [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS]) + ->whereIn('type', Segment::getSegmentTypes()) ->selectExpr( self::$_table . '.*, ' . 'COUNT(IF(' . @@ -243,7 +261,7 @@ class Segment extends Model { static function listingQuery(array $data = []) { $query = self::select('*'); - $query->whereIn('type', [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS]); + $query->whereIn('type', Segment::getSegmentTypes()); if (isset($data['group'])) { $query->filter('groupBy', $data['group']); } diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index 447c89da2c..a65d218ac0 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -168,7 +168,7 @@ class Subscriber extends Model { $segments = Segment::orderByAsc('name') ->whereNull('deleted_at') - ->whereIn('type', [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS, Segment::TYPE_WC_USERS]) + ->whereIn('type', Segment::getSegmentTypes()) ->findMany(); $segment_list = []; $segment_list[] = [