Skip to content

Commit

Permalink
feature: remove duplicate meta and set correct value for _give_form_s…
Browse files Browse the repository at this point in the history
…ales
  • Loading branch information
Ante Laca committed Dec 7, 2023
1 parent cd3c767 commit a8109d3
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/DonationForms/Migrations/RemoveDuplicateMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function run()

foreach ($posts as $post) {
$formEarnings = give_get_meta($post->ID, '_give_form_earnings');
$formSales = give_get_meta($post->ID, '_give_form_sales');

// Update meta with duplicate keys only
if (count($formEarnings) > 1) {
Expand All @@ -59,6 +60,15 @@ public function run()
$earnings = $this->getEarningsFromDonations($post->ID);
give_update_meta($post->ID, '_give_form_earnings', $earnings);
}

if (count($formSales) > 1) {
// Delete all meta
give_delete_meta($post->ID, '_give_form_sales');

// Get sales count from donations
$sales = $this->getSalesCountFromDonations($post->ID);
give_update_meta($post->ID, '_give_form_sales', $sales);
}
}
}

Expand All @@ -67,9 +77,9 @@ public function run()
*
* @param $formId
*
* @return int
* @return float | int
*/
private function getEarningsFromDonations($formId): int
private function getEarningsFromDonations($formId)
{
$donations = Db::table('posts')
->attachMeta(
Expand All @@ -87,4 +97,26 @@ private function getEarningsFromDonations($formId): int
return array_sum($amounts);
}

/**
* Get sales count from donations by Form ID
*
* @param $formId
*
* @return int
*/
private function getSalesCountFromDonations($formId): int
{
return Db::table('posts')
->attachMeta(
'give_donationmeta',
'ID',
'donation_id',
[DonationMetaKeys::FORM_ID(), 'formId']
)
->where('post_type', 'give_payment')
->where('post_status', 'publish')
->where('give_donationmeta_attach_meta_formId.meta_value', $formId)
->count('ID');
}

}

0 comments on commit a8109d3

Please sign in to comment.