Skip Navigation

[Resolved] When users purchase adverts on this site, the ad package names are wrong

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 20 replies, has 3 voices.

Last updated by Christian Cox 4 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#572205
2017-09-21 16_28_20-Photos.png

When a user purchases an ad pack, the packs are created using the name of "
“CRED Auto Draft 171ec81a29ece805fe849894816140eb"

They also do not have any number of ads assigned to them.

#572242

Hi, can you tell me:
- Is this happening for only one CRED form, or on multiple forms?
- Has this always been happening, or did it just start recently?
- If you temporarily disable all non-Toolset plugins (except WordPress) and activate a default theme like Twenty Seventeen, does the problem persist?

#572561

Hi Christian,

- I can confirm that this is happening on ALL packages, except for the default toolset one which is still working correctly
- I'm not sure if this has always been happening. The site is under development and we are only just testing it
- changing plugins / templates does not affect the result.

#573021

Thanks. After a bit of investigation I see what is happening here. There is code built in to the Classifieds plugin that automatically creates the name of each package when the form is submitted. That code is specifically attached to the original CRED ad package form. If you would like to do something similar for your other package forms, you can add similar code to your theme:

add_action('cred_save_data', 'ts_add_new_package_rename_function', 10, 2);
function ts_add_new_package_rename_function($post_id, $form_data) {
  $package_forms = array( 123, 456 );
  if( in_array($form_data['id'], $package_forms) ) {
    //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));
  }
}

Modify the $package_forms array to include a comma-separated list of form IDs you would like to target, and the automatic title will be applied to the packages created by those forms.

#573321

Hi Christian,

I tried adding this to my functions.php file, but it caused my website to crash. Am i putting it in the wrong place?

// Process Additional Custom Packages
add_action('cred_save_data', 'ts_add_new_package_rename_function', 10, 2);

function ts_add_new_package_rename_function($post_id, $form_data) {

  $package_forms = array(7237, 7231, 7232, 7233, 7230, 7234, 7235, 7236 );

  if( in_array($form_data['id'], $package_forms) ) {

    //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));

  }

}
#573419

Looks okay to me, you should add this code at the bottom of functions.php, just before this (if it exists):

?>

If that does not work, please post the entire contents of your functions.php file here for me to review. Please surround your content with code tags as described below the input box.

#573427
Screenshot 2017-09-25 21.12.21.png

Hi Christian,

Ok, so that has partly fixed it. It now creates packages with the correct names, however it doesn't put any credits on the packages

In the photo, the top package is the one i just created as a test, the middle one you can ignore (it was one of the ones that was broken) the bottom package is the one that is the default package that came with toolset.

As you can see, there are no credits assigned.

#573445
Screen Shot 2017-09-25 at 4.48.57 PM.png

Okay we'll need to do some refactoring to add the custom field values.

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 => array(
      "original" => 10,
      "allowed" => 10
     ),
     456 => array(
      "original" => 20,
      "allowed" => 20
      ),
     789 => array(
      "original" => 30,
      "allowed" => 30
      )
  );
  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 the appropriate number of ads to the package
    update_post_meta($post_id, 'wpcf-original-number-of-ads', $package_ads[$form_data['id']]['original']);
    update_post_meta($post_id, 'wpcf-package-number-of-ads-allowed', $package_ads[$form_data['id']]['allowed']);
  }
}

The $package_ads array now includes each CRED form ID as an index, and the value of that index is a nested array that includes information about the original # ads and allowed # ads for each form. You can add more items to the package_ads array to correspond to each CRED form that adds a new package. Modify the CRED form ID and the two values for each form.

#573655

<deleted> - I fixed this bit myself - i had the wrong formatting in the functions.php which was causing my site to fail - i'm currently testing the fix.

#573664

We are getting there slowly. the packages now have the correct amount on them, but when the order completes, it doesn't assign the package to the user now, or update the users available credits

#573837

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.

#573855

OK, that seems to work, unless I (as the site admin) purchase a package for myself. I tested this process as the site admin, and it didn't assign the packages to me. When i tested as a site user it worked fine though.

It also doesn't seem to be decreasing the amount of credits available if I submit a post. After i submit i have the same amount of available credits.

#573897

OK, that seems to work, unless I (as the site admin) purchase a package for myself. I tested this process as the site admin, and it didn't assign the packages to me.
Okay that's odd, I've been using site admin for testing and it's working fine. Is the admin profile updated when you any of the Add Package forms, or is it just a specific one, or just the new ones?

It also doesn't seem to be decreasing the amount of credits available if I submit a post. After i submit i have the same amount of available credits.
This sounds like a separate issue involving the post submission process. Please create a new ticket so we can investigate this independently.

#573918

I tested this using both a default package, and a new package. Neither of them assigned the credits to the admin user. I then tested this using a standard user, and made 2 purchases, one for 3 credits, and one for 5 credits. It credited them both, but for some reason credited the "5" twice. You can see the package has been assigned to the user twice as well.

Just to be sure, i made a second purchase for "5" and it duplicated it again. - the "5" pack is the default one that came with toolset.

I'll log the other issue separately regarding post processing.

#573931

Okay it sounds like I need to make a clone of your site and determine what's going on. Please standby and I will update you once I have performed a few more tests.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.