• Resolved ibro87

    (@ibro87)


    Hi there,

    I’m using WP-Optimize cache plugin and I need to clear the cache automatically when MyCred points balance change (e.g. user earns MyCred points by spinning lucky wheel). Is there any hook or filter that I can use in the functions.php file to achieve this? Thanks for your help!

Viewing 15 replies - 1 through 15 (of 21 total)
  • Hello @ibro87,

    Thanks for contacting us. Hope you are doing well.

    Yes, you can use ‘mycred_add_finished’ filter to achieve it. Please refer to its documentation. Here is the link below.

    Documentation Link

    Let me know if you have any questions further.

    Thanks & Regards,
    WP Experts Support Team

    Thread Starter ibro87

    (@ibro87)

    Hello @daniyalraza01,

    Thanks for the quick reply. There is an example how to send emails when user gains / loses points, which is great. However, this is not what I need. Could you please help me create the code that would purge the cache instead when user gains / losses the points?

    FYI, here’s what I am using when WooCommerce order is made and this works so I’d like to achieve the same when MyCred points are gained or lost. Thanks again for your help!

    add_action(‘woocommerce_payment_complete’, ‘wpo_flush_wc_purchase’, 10, 1);
    function wpo_flush_wc_purchase ( $order_id ) {
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
    foreach ( $items as $item ) {
    wpo_cache_flush ( $item[‘product_id’] );
    }
    }

    Hello @ibro87,

    Hope you are doing well.

    You can achieve it through ‘mycred_add_finished’ . You can easily figure the required and optional parameters mentioned in the documentation. Just Look for the function that will clear the cache in the caching plugin accordingly that you are using. You have to make the changes as per your requirements.

    We can just guide you the path here to achieve this customization. Let me know if you have any questions.

    Thanks

    Thread Starter ibro87

    (@ibro87)

    Hi Daniyal Raza,

    The function below clears the cache in the caching plugin when a post is updated.

    public function purge_post_on_update($post_id) {

    $post_type = get_post_type($post_id);

    $post_type_object = get_post_type_object($post_type);

    if ((defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) || ‘revision’ === $post_type || null === $post_type_object || !$post_type_object->public) {

    return;

    }

    Could you now please tell where to add MyCred filter in order to clear the cache when user gain or lose points? Thanks for your help!

    Hello @ibro87,

    You can check in ‘mycred_add_finished’ filter if the user balance changes call this cache clear function to clear the cache.

    Please confirm with using this.

    Thanks

    Thread Starter ibro87

    (@ibro87)

    Sorry, I’m not a WordPress developer and do not understand what you’re saying.

    Could you please provide an example code or detailed MyCred documentation that I could share with WP-Optimize support? if so, they might be able to better assist. Thank you.

    You can try using the below code snippet:

    add_filter( 'mycred_add_finished', 'check_for_balance_change', 1000, 3 );
    
    function check_for_balance_change( $reply, $request, $mycred ) {
    
         if ( $reply === false ) return $reply;
    
     extract( $request );
    
      $user_id = $request['user_id'];
      $type = $request['type'];
      $amount = $request['amount'];
    
      $balance = $mycred->get_users_balance( $user_id, $type );
    
      if($amount > 0) return $reply;
    
      if ( $balance <= 0 || ( $balance - $amount ) < 0 ) return false;
    
      if(!empty($amount)) {
          purge_post_on_update(get_the_ID());
      }
    
      return $reply; 
          
    }

    Let me know if that works for you.

    Thanks

    Thread Starter ibro87

    (@ibro87)

    Hi Daniyal,

    Thank you so much for providing me this snippet code. Unfortunately, this did not work but I feel like we’re very close to achieve what I need.

    Perhaps this helps?
    https://getwpo.com/faqs/when-does-wpo-clear-the-cache/

    Can you please explain more on which cache do you want to clear? Do you want to clear site wide cache?

    Thread Starter ibro87

    (@ibro87)

    Yes, I want to clear site wide cache when user gain or lose MyCred points. Let me know if I can be of any further assistance. Thank you!

    Daniyal Raza

    (@daniyalraza01)

    Here is the code snippet below for cache:

    add_filter( 'mycred_add_finished', 'check_for_balance_change', 1000, 3 );
    
    function check_for_balance_change( $reply, $request, $mycred ) {
    
    if ( $reply === true ) { clear_cached_data(); } return $reply;
    
    }

    You can use this example.

    Thanks

    Thread Starter ibro87

    (@ibro87)

    Hi Daniyal Raza,

    Unfortunately, this code does not work for WP-Optimize plugin. As I previously mentioned, I am not WP developer and I am not able to write custom codes. Are you able to provide me with working code specifically made for WP-Optimize plugin?

    Daniyal Raza

    (@daniyalraza01)

    Unfortunately, we cannot provide code for WP-Optimize plugin. myCred functionality does not include clearing site wide cache. You will have to figure it out by contacting their support team.

    Hope you understand.

    Thanks

    Thread Starter ibro87

    (@ibro87)

    Hi,

    I already did but they are not even familiar with MyCred plugin and they could not help. I find this thread unhelpful so I’m going to close it now.

    Sorry if you find this thread unhelpful. Actually, the query was not related to our plugin because myCred doesn’t have any cache feature right now.

Viewing 15 replies - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.