For anyone who might have the same requirement this is how I resolved it with a code snippet. This code snippet counts any claim toward packages limit and it creates a default package if customer doesn’t have a package assigned.
use HivePress\Models\Listing_Claim;
use HivePress\Models\User_Listing_Package;
use HivePress\Models\Listing_Package;
add_action('hivepress/v1/models/listing_claim/update_status', function($claim_id) {
error_log('Updating package for claim started');
// Validate the claim ID
if (!is_numeric($claim_id) || $claim_id <= 0) {
return;
}
// Fetch the claim object
$claim = Listing_Claim::query()->get_by_id($claim_id);
if (!$claim) {
return;
}
// Check if the claim status is 'publish'
if ($claim->get_status() !== 'publish') {
return;
}
// Get the user ID from the claim
$user_id = $claim->get_user__id();
// Fetch user packages, ordered by submit limit (descending)
$user_packages = User_Listing_Package::query()
->filter(['user' => $user_id])
->order(['submit_limit' => 'desc'])
->get();
// Initialize user package
$user_package = $user_packages ? reset($user_packages) : null;
// If no user package found, assign a default free package
if (!$user_package) {
$default_free_package_id = YOUR_DEFAULT_PACKAGE_ID_HERE;
$package = Listing_Package::query()->get_by_id($default_free_package_id);
if (!$package || $package->get_status() !== 'publish') {
return;
}
// Create a new user package
$user_package = (new User_Listing_Package())->fill(
array_merge(
$package->serialize(),
[
'user' => $user_id,
'package' => $package->get_id(),
'default' => true,
]
)
);
// Save the new user package
if (!$user_package->save()) {
return;
}
}
// Reduce the submit limit if greater than 0
if ($user_package->get_submit_limit() > 0) {
$user_package->set_submit_limit($user_package->get_submit_limit() - 1)->save();
error_log('Updating package for claim successful');
}
});
Also if anyone has any feedback on this code, please let me know