Okay I did some more investigation and found out why this isn't working as expected. There's a bit of code in the Classifieds plugin that makes this work for the original CRED form to Add Ad Packages, but not new CRED forms. So I've made some changes to the code I recommended earlier, and this is the update I would like you to use:
add_action('cred_save_data', 'ts_add_new_package_order_function', 10, 2);
function ts_add_new_package_order_function($post_id, $form_data) {
$package_ads = array(123,456);
if( array_key_exists($form_data['id'], $package_ads) ) {
//create an automatic title and slug using a given phrase and the ID of the post we are creating
$title = 'Package Order #' . $post_id;
wp_update_post(array('ID' => $post_id, 'post_title' => $title));
}
}
add_action( 'cred_commerce_after_order_completed', 'complete_order_add_ad_credits', 10, 1 );
function complete_order_add_ad_credits( $data ) {
if ((is_array($data)) && (!(empty($data)))) {
//Get processed product ID from the order $data array
if (isset($data['extra_data'][0]['cred_product_id'])) {
$processed_product_id = $data['extra_data'][0]['cred_product_id'];
$processed_package_post_id = $data['extra_data'][0]['cred_post_id'];
//Retrieve customer user ID
if (isset($data['user_id'])) {
$customer_user_id = $data['user_id'];
//Get the latest maximum ad credits assigned by administrator for an ad package woocommerce product
$max_ad_credits = get_post_meta($processed_product_id, 'wpcf-number-of-ads', TRUE);
//Associate this maximum ad credits to the completed order
$current_ad_credits_after_completed_order = $max_ad_credits;
//Store the ad credits in the purchased package.
$success_updating_credits = update_post_meta($processed_package_post_id, 'wpcf-package-number-of-ads-allowed', $current_ad_credits_after_completed_order);
//Mark status as ad package customer
$success_updating_credits = update_user_meta($customer_user_id, 'wpcf-ad-package-customer', 'yes');
//Retrieve any existing customer active packages if any
$success_retrieving_existing_packages_for_this_user_array = get_user_meta($customer_user_id, 'wpcf-customer-active-packages', FALSE);
if (empty($success_retrieving_existing_packages_for_this_user_array)) {
$success_retrieving_existing_packages_for_this_user_array = array();
}
$success_retrieving_existing_packages_for_this_user_array[] = (string)$processed_package_post_id;
//Delete all to add all
$success_delete_all_packages_to_user = delete_user_meta($customer_user_id, 'wpcf-customer-active-packages');
$active_packages_sort_order = array();
foreach ($success_retrieving_existing_packages_for_this_user_array as $key => $package_id) {
$success_add_package_mid_to_user = add_user_meta($customer_user_id, 'wpcf-customer-active-packages', $package_id, FALSE);
$active_packages_sort_order[$key] = $success_add_package_mid_to_user;
}
$success_updating_active_packages_sort_order = update_user_meta($customer_user_id, '_wpcf-customer-active-packages-sort-order', $active_packages_sort_order);
//Associate customer user_id to package
$success_inserting_custom_id_to_package = update_post_meta($processed_package_post_id, '_classifieds_ad_package_customer_id', $customer_user_id);
//Store original ad quantity after purchase and associate this with the package created
classifieds_store_original_ad_quantity_to_package($processed_package_post_id, $max_ad_credits);
//Retrieve user existing total available ad credits
$success_retrieving_existing_user_total_credits = get_user_meta($customer_user_id, 'wpcf-user-total-available-ad-credits', TRUE);
if (empty($success_retrieving_existing_user_total_credits)) {
$success_retrieving_existing_user_total_credits = 0;
}
$updated_total_user_credits_after_purchase = $success_retrieving_existing_user_total_credits + $max_ad_credits;
//Update user total available number of ad credits
$success_updating_total_ad_credits = update_user_meta($customer_user_id, 'wpcf-user-total-available-ad-credits', $updated_total_user_credits_after_purchase);
//duplicate post to WPML translation
classifieds_duplicate_on_publish($processed_package_post_id);
}
}
}
}
function classifieds_store_original_ad_quantity_to_package($post_id, $max_ad_credits) {
$success_updating_original_ad_qty = update_post_meta($post_id, 'wpcf-original-number-of-ads', $max_ad_credits);
}
function classifieds_duplicate_on_publish($post_id) {
global $Class_CRED_WPML_Integration;
if (is_object($Class_CRED_WPML_Integration)) {
//duplicate post to WPML translation
$Class_CRED_WPML_Integration->wpml_duplicate_on_publish($post_id);
$listing_location_updated_terms = wp_get_object_terms($post_id, 'location');
$Class_CRED_WPML_Integration->wpml_copy_term_values_to_translations($post_id, $listing_location_updated_terms, 'location', 'listing');
}
}
We'll use the previous format for the packages array, so you need to add a comma-separated list of your new CRED forms here again. However, the number of ads will not be hard-coded any longer. It will come directly from the product. Once the order is complete, the number of ads will appear in the package and the user's profile will be updated. Please try this out.