Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/5ad970077e8a94f002cae4d1b32d5fa6 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/5ad970077e8a94f002cae4d1b32d5fa6 to your computer and use it in GitHub Desktop.
per-order upsell analytics from One-Click Upsells
add_action( 'admin_init', function () {
if ( empty( $_GET['wfocu_export_csv'] ) || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
global $wpdb;
$rows = $wpdb->get_results(
"
SELECT
s.order_id AS parent_order_id,
s.email,
s.gateway,
s.timestamp AS session_date,
e.object_id AS offer_id,
e.action_type_id,
e.value AS upsell_revenue,
e.timestamp AS event_date,
fm.meta_value AS funnel_id,
om.meta_value AS new_order_id
FROM {$wpdb->prefix}wfocu_session s
INNER JOIN {$wpdb->prefix}wfocu_event e
ON e.sess_id = s.id
AND e.object_type = 'offer'
LEFT JOIN {$wpdb->prefix}wfocu_event_meta fm
ON fm.event_id = e.id
AND fm.meta_key = '_funnel_id'
LEFT JOIN {$wpdb->prefix}wfocu_event_meta om
ON om.event_id = e.id
AND om.meta_key = '_new_order'
WHERE e.action_type_id IN (4, 6)
ORDER BY s.order_id, e.timestamp
",
ARRAY_A
);
nocache_headers();
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=wfocu-upsell-detail-' . date( 'Y-m-d' ) . '.csv' );
$out = fopen( 'php://output', 'w' );
fputcsv(
$out,
[
'Parent Order ID',
'Customer Email',
'Gateway',
'Session Date',
'Offer ID',
'Offer Name',
'Status',
'Upsell Revenue',
'Event Date',
'Funnel ID',
'Funnel Name',
'Upsell Order ID',
'Initial Cart Products',
]
);
foreach ( $rows as $row ) {
$offer_name = get_the_title( $row['offer_id'] ) ?: 'Offer #' . $row['offer_id'];
$funnel_name = $row['funnel_id'] ? ( get_the_title( $row['funnel_id'] ) ?: 'Funnel #' . $row['funnel_id'] ) : '';
$status = ( '4' === (string) $row['action_type_id'] ) ? 'Accepted' : 'Rejected';
$parent_order = wc_get_order( $row['parent_order_id'] );
$cart_items = [];
if ( $parent_order instanceof WC_Order ) {
foreach ( $parent_order->get_items() as $item ) {
if ( $item->get_meta( '_upstroke_purchase' ) ) {
continue;
}
$cart_items[] = $item->get_name() . ' x' . $item->get_quantity();
}
}
fputcsv(
$out,
[
$row['parent_order_id'],
$row['email'],
$row['gateway'],
$row['session_date'],
$row['offer_id'],
$offer_name,
$status,
$row['upsell_revenue'],
$row['event_date'],
$row['funnel_id'],
$funnel_name,
$row['new_order_id'] ?: '',
implode( ' | ', $cart_items ),
]
);
}
fclose( $out );
exit;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment