diff --git a/src/Campaigns/Controllers/CampaignRequestController.php b/src/Campaigns/Controllers/CampaignRequestController.php index 787a657f3b..3b7a7ce371 100644 --- a/src/Campaigns/Controllers/CampaignRequestController.php +++ b/src/Campaigns/Controllers/CampaignRequestController.php @@ -3,6 +3,7 @@ namespace Give\Campaigns\Controllers; use Exception; +use Give\Campaigns\CampaignDonationQuery; use Give\Campaigns\Models\Campaign; use Give\Campaigns\Repositories\CampaignRepository; use Give\Campaigns\ValueObjects\CampaignGoalType; @@ -31,7 +32,11 @@ public function getCampaign(WP_REST_Request $request) return new WP_Error('campaign_not_found', __('Campaign not found', 'give'), ['status' => 404]); } - return new WP_REST_Response($campaign->toArray()); + return new WP_REST_Response( + array_merge($campaign->toArray(), [ + 'goalProgress' => $campaign->goalProgress(), + ]) + ); } /** diff --git a/src/Campaigns/Models/Campaign.php b/src/Campaigns/Models/Campaign.php index 8c71988c28..c269c19843 100644 --- a/src/Campaigns/Models/Campaign.php +++ b/src/Campaigns/Models/Campaign.php @@ -5,6 +5,7 @@ use DateTime; use Exception; use Give\Campaigns\Actions\ConvertQueryDataToCampaign; +use Give\Campaigns\CampaignDonationQuery; use Give\Campaigns\Factories\CampaignFactory; use Give\Campaigns\Repositories\CampaignPageRepository; use Give\Campaigns\Repositories\CampaignRepository; @@ -193,6 +194,12 @@ public function merge(Campaign ...$campaignsToMerge): bool return give(CampaignRepository::class)->mergeCampaigns($this, ...$campaignsToMerge); } + public function goalProgress() + { + $query = new CampaignDonationQuery($this); + return $query->sumIntendedAmount(); + } + /** * @unreleased * diff --git a/src/Campaigns/Routes/RegisterCampaignRoutes.php b/src/Campaigns/Routes/RegisterCampaignRoutes.php index d649991071..d6b5e0ad25 100644 --- a/src/Campaigns/Routes/RegisterCampaignRoutes.php +++ b/src/Campaigns/Routes/RegisterCampaignRoutes.php @@ -219,6 +219,11 @@ public function getSchema(): array 'description' => esc_html__('Campaign goal', 'give'), 'errorMessage' => esc_html__('Must be a number', 'give'), ], + 'goalProgress' => [ + 'type' => 'number', + 'readonly' => true, + 'description' => esc_html__('Campaign goal progress', 'give'), + ], 'goalType' => [ 'enum' => [ 'amount', diff --git a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx index 997529e9ba..2f569e9378 100644 --- a/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx +++ b/src/Campaigns/resources/admin/components/CampaignDetailsPage/Components/CampaignStats.tsx @@ -8,6 +8,7 @@ import HeaderText from './HeaderText'; import HeaderSubText from './HeaderSubText'; import DefaultFormWidget from "./DefaultForm"; import {GiveCampaignDetails} from "@givewp/campaigns/admin/components/CampaignDetailsPage/types"; +import useCampaignEntityRecord from "@givewp/campaigns/admin/components/CampaignDetailsPage/useCampaignEntityRecord"; const campaignId = new URLSearchParams(window.location.search).get('id'); @@ -178,6 +179,9 @@ const RevenueWidget = () => { } const GoalProgressWidget = () => { + + const {campaign} = useCampaignEntityRecord(); + return (