Your cart is currently empty!
JudgeMe product review sync to WooCommerce
Synchronizes product reviews from JudgeMe into WooCommerce Product Reviews (native) to power schema and the use of standard product archives stars for performance.
// Settings
define( 'CCOM_JUDGEME_PERPAGE', 50 );
// Scheduler
add_action( 'admin_notices', function() {
// Require Action Scheduler
if( ! function_exists( 'as_has_scheduled_action' ) ) {
return;
}
// Schedule: JudgeMe Scheduler
$hook = 'ccom_judgeme_scheduler';
if( ! as_has_scheduled_action( $hook ) ) {
as_schedule_recurring_action(
current_time( 'timestamp', 1 ),
DAY_IN_SECONDS,
$hook
);
}
} );
add_action( 'ccom_judgeme_scheduler', function() {
// Get Reviews Total
$args = [
'body' => [
'api_token' => get_option( 'judgeme_shop_token' ),
'shop_domain' => get_option( 'judgeme_domain' ),
],
];
$url = 'https://judge.me/api/v1/reviews/count';
$response = wp_remote_get( $url, $args );
$response_body = json_decode( wp_remote_retrieve_body( $response ) );
if( empty( $response_body->count ) ) {
return;
}
// Loop Product Batches
for(
$i = 0;
$i < intval( $response_body->count ) + CCOM_JUDGEME_PERPAGE;
$i += CCOM_JUDGEME_PERPAGE ) {
// Schedule: JudgeMe Sync Interval
$hook = 'ccom_judgeme_sync';
$args = [ 'offset' => $i ];
if( ! as_has_scheduled_action( $hook, $args ) ) {
as_schedule_single_action(
current_time( 'timestamp', 1 ) + $i,
$hook, $args
);
}
} // End Product Batch Loop
} );
add_action( 'ccom_judgeme_sync', function( $offset ) {
// Get Reviews Per Page
$args = [
'body' => [
'api_token' => get_option( 'judgeme_shop_token' ),
'page' => 1 + floor( $offset / CCOM_JUDGEME_PERPAGE ),
'per_page' => CCOM_JUDGEME_PERPAGE,
'shop_domain' => get_option( 'judgeme_domain' ),
],
];
$url = 'https://judge.me/api/v1/reviews';
$response = wp_remote_get( $url, $args );
$response_body = json_decode(
wp_remote_retrieve_body( $response )
);
if( empty( $response_body->reviews ) ) {
return;
}
// Loop Reviews
global $wpdb;
foreach( $response_body->reviews as $review ) {
// Product Reviews Only
if( empty( $review->product_external_id ) ) {
continue;
}
// Check Preexistence
$sql = $wpdb->prepare(
"
SELECT `comment_id`
FROM {$wpdb->prefix}commentmeta
WHERE `meta_key` = 'judgeme_id'
AND `meta_value` = {$review->id}
LIMIT 1
"
);
$comment_id = $wpdb->get_var( $sql );
// Preexists, Update Approval Status
if( $comment_id ) {
wp_set_comment_status(
$comment_id, $review->hidden == '1' ? 'hold' : 'approve'
);
continue;
}
// Get User Account
$user = get_user_by( 'email', $review->reviewer->email );
$user_id = $user ? $user->ID : '';
// Insert Woo Review
$args = [
'comment_agent' => '',
'comment_approved' => $review->hidden == '1' ? 0 : 1,
'comment_author' => $review->reviewer->name,
'comment_author_email' => $review->reviewer->email,
'comment_author_IP' => $review->ip_address,
'comment_author_url' => '',
'comment_content' => $review->body,
'comment_date' => $review->created_at,
'comment_parent' => 0,
'comment_post_ID' => $review->product_external_id,
'comment_type' => 'review',
'user_id' => $user_id,
];
$comment_id = wp_insert_comment( $args );
// Insert Woo Review Metadata
if( $comment_id ) {
update_comment_meta( $comment_id, 'rating', $review->rating );
update_comment_meta( $comment_id, 'judgeme_title', $review->title );
update_comment_meta( $comment_id, 'judgeme_id', $review->id );
}
} // End Loop Reviews
} );
Instructions for JudgeMe product review sync to WooCommerce
- Log into a staging or locally hosted clone of your site.
- Install and activate Code Snippets plugin.
- WP Admin > Snippets > Add New
- Copy and paste the code from the section above.
- Check to ensure formatting came over properly.
- Customize the code as desired.
- Add a meaningful title.
- Select whether to run on front-end or back-end (or both).
- Click “Save and Activate”.
- Test your site to ensure it works.
- Disable if any problems, or recover.
- Repeat for live environment.
Need help modifying JudgeMe product review sync to WooCommerce?
Contact me. I can help with fitting projects or refer to my partner.
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.
Disclaimer of warranty