WebToffee Import Export for WooCommerce
FacetWP doesn’t automatically index content added via the WebToffee (Product) Import Export for WooCommerce plugin.
Fortunately, there are two hooks available that can be used to allow FacetWP to automatically detect and index imported content.
Trigger a full re-index after a scheduled import
To trigger a full re-index after WebToffee has finished with a scheduled import, add the following code to your (child) theme’s functions.php.
Scheduled imports are imports running on a (cron) schedule, and are visible on the “Scheduled Actions” page.
Note that this hook only fires after a succesful scheduled import. When the import triggers any errors, or when the import does not change any post content, the hook will not fire, and the re-index will not run.
<?php
function fwp_reindex_posts_after_scheduled_import( $out ) {
if ( function_exists( 'FWP' ) ) {
FWP()->indexer->index();
}
}
add_action('wt_ier_scheduled_action_finished', 'fwp_reindex_posts_after_scheduled_import', 10, 1);
Trigger a full re-index after a manual import
The following code triggers a full re-index after a manual import. This hook will always fire after a manual import, also when the import does not change any post content.
<?php
function fwp_reindex_posts_after_manual_import() {
if ( function_exists( 'FWP' ) ) {
FWP()->indexer->index();
}
}
add_filter('wt_iew_importer_done_import', 'fwp_reindex_posts_after_manual_import', 10);
Send an e-mail after importing
Both hooks mentioned above can also be used to send an email after the import is finished.
The following code triggers a full re-index and sends an e-mail after a succesful scheduled import. You can use the same lines within the hook for a manual import.
<?php
function fwp_reindex_posts_send_email( $out ) {
// do a full re-index
if ( function_exists( 'FWP' ) ) {
FWP()->indexer->index();
}
// send a basic e-mail
$email = 'me@example.com, someone@example.com'; // change to your e-mail address(es)
wp_mail( $email, 'Product Auto Import Export', 'Product scheduled import completed.' ); // Specify the subject and message of the e-mail
}
add_action('wt_ier_scheduled_action_finished', 'fwp_reindex_posts_send_email', 10, 1);
The following also sends an e-mail after a scheduled import, but it includes a detailed log file of the import:
<?php
function fwp_reindex_posts_send_email_log( $out ) {
// do a full re-index
if ( function_exists( 'FWP' ) ) {
FWP()->indexer->index();
}
// send an e-mail with import log file
ini_set('max_execution_time', -1);
ini_set('memory_limit', -1);
$wt_log_path = WP_CONTENT_DIR . '/webtoffee_iew_log';
$files = glob("$wt_log_path/*.*");
$files = array_combine($files, array_map('filectime', $files));
arsort($files);
$destination = key($files);
$email = 'me@example.com'; // change to your e-mail address(es)
$object= 'Product scheduled import completed'; // Specify the subject of the e-mail
$message = 'Product scheduled import completed.'; // Specify the message of the e-mail
$mail_attachment = array($destination);
$headers = '';
wp_mail( $email, $object, $message,$headers,$mail_attachment );
}
add_action('wt_ier_scheduled_action_finished', 'fwp_reindex_posts_send_email_log', 10, 1);
Prevent duplicate results with imported posts
If you are regularly importing posts, be aware of the fact that imported posts often end up having the exact same post date.
This can lead to issues when queries are using the post date in their orderby
query argument. Adding to the problem is that WordPress by default will order queries by post date.
In this scenario with multiple posts sharing the same date, if no specific order is set for a query, or if it is intentionally set to date
, MySQL will not have a fallback and will often sort erratically. This can lead to duplicate results in your listing, most visibly when you are using a Pager/Load more facet or a Sort facet.
The problem can be easily fixed by setting something else than date
in the orderby
argument of your queries. Or by introducing a secondary, fallback sorting method.