• Resolved jnasoy

    (@jnasoy)


    Hello,

    Here’s the scenario.

    I’ve added custom field to my rating form and I want to dynamically create review entry every time a form is submitted. The dynamic review entry is specific to a certain category/term and its rating value is base on the submitted rating of the custom field.

    I have the following code

    
    add_action('site-reviews/review/created', function ($review, $command) {
      $overallR_val = $review->rating;
      $qualityR_val = $review->custom->quality_rating;
      $serviceR_val = $review->custom->service_rating;
      $sustainabilityR_val = $review->custom->sustainability_rating;
      $assigned_post_id = $command->assigned_posts[0];
      $author_user = $review->author;
      $author_email = $review->email;
      $author_id = $review->author_id;
      $date = $review->date;
      $ip_address = $review->ip_address;
    
      if (function_exists('glsr_create_review')) {
        $review = glsr_create_review([
            'rating' => $qualityR_val,
            'name' => $author_user,
            'email' => $author_email,
            'date' => $date,
            'assigned_users' => $author_id,
            'assigned_posts' => $assigned_post_id,
            'assigned_terms' => 139,
            'ip_address' => $ip_address,
            'quality_rating' => $qualityR_val,
            'service_rating' => $serviceR_val,
            'sustainability_rating' => $sustainabilityR_val
    
        ]);
    
    }
    // log data
      glsr_log($overallR_val);
      glsr_log($qualityR_val);
      glsr_log($serviceR_val);
      glsr_log($sustainabilityR_val);
    }, 10, 2);
    

    but it seems like glsr_create_review is also triggering the site-reviews/review/created causing infinite loop that creates multiple review entry.

    What will be the best approach for the said requirements?

    Thank you in advance for the help

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    Why do you need to create a second review from every submitted review?

    If you can explain the reasoning behind it, I may be able to provide a better solution.

    Thread Starter jnasoy

    (@jnasoy)

    Hello,

    The second review for every submitted review is assigned to a specific category or term and its rating value is based on the custom field of the main form (which doesn’t have terms assigned to it).

    I have 3 main categories for the ratings, quality, service, and sustainability.

    I could just add 3 rating forms that assigns to the different categories/terms but that will have 3 different submit button for each category form which does not meet my requirement. The requirement is to have single submit button for the multiple category ratings.

    My solution is have a form that does not assigned to any categories but add custom field for quality, services, and sustainability ratings.

    In order for me to easily display the rating of each categories, I need to dynamically create rating entry for the different categories every time a new rating (with the custom field) is submitted.

    Maybe there’s a better approach to this and I’m just over-complicate it ??

    I hope I explain it clearly ??

    Thank you

    Plugin Author Gemini Labs

    (@geminilabs)

    I still don’t understand why this is required.

    However, a simple solution is to add a custom value to the created review when using glsr_create_review, then check if the review contains that custom value in the beginning of using site-reviews/review/created and if so, abort.

    Thread Starter jnasoy

    (@jnasoy)

    Hello,

    Thank you for the response. I implemented a check in the assigned terms value and it seems to work.

    Another question, Does the glsr_create_review only accepts terms id in assinged_terms value?

    I’ve tried using the slug but it doesn’t work. No problem if I use the term/category ID.

     $review = glsr_create_review([
            'rating' => $qualityR_val,
            'name' => $author_user,
            'email' => $author_email,
            'date' => $date,
            'assigned_users' => $author_id,
            'assigned_posts' => $assigned_post_id,
            'assigned_terms' => 139,
            'ip_address' => $ip_address,
            'quality_rating' => $qualityR_val,
            'service_rating' => $serviceR_val,
            'sustainability_rating' => $sustainabilityR_val
          ]);

    Thanks

    • This reply was modified 3 years, 9 months ago by jnasoy.
    • This reply was modified 3 years, 9 months ago by jnasoy.
    Plugin Author Gemini Labs

    (@geminilabs)

    Yes, the glsr_create_review function only accepts a numeric ID (or a numeric ID array) for the “assigned_terms”, “assigned_posts”, and “assigned_terms” options.

    You can use get_term or get_term_by to get the term before creating the review.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Create Review functions creates multiple reviews’ is closed to new replies.