Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Last active October 3, 2024 13:40
Show Gist options
  • Save Crocoblock/bcf96e00d54d84f38865d175d01fbe67 to your computer and use it in GitHub Desktop.
Save Crocoblock/bcf96e00d54d84f38865d175d01fbe67 to your computer and use it in GitHub Desktop.
JetEngine Get purchased Woocommerce products macro
<?php
add_action( 'jet-engine/register-macros', function() {
/**
* Return purchased products.
*/
class Purchased_Products_Macro extends \Jet_Engine_Base_Macros {
public function macros_tag() {
return 'purchased_products';
}
public function macros_name() {
return esc_html__( 'Purchased products', 'jet-engine' );
}
public function macros_args() {
return array();
}
public function macros_callback( $args = array() ) {
$user_id = get_current_user_id();
if ( ! $user_id || ! function_exists( 'WC' ) ) {
return;
}
$args = array(
'customer_id' => $user_id,
'limit' => -1,
'status' => array(
'completed',
),
);
$orders = wc_get_orders( $args );
$products = array();
foreach ( $orders as $order ) {
$items = $order->get_items();
foreach ( $items as $item ) {
$products[] = $item->get_data()['product_id'];
}
}
return implode( ',', $products );
}
}
new Purchased_Products_Macro();
} );
@ashiqhosenbd
Copy link

I tried a lot using this code but it's not working with the query builder.

@TimoKuijper
Copy link

TimoKuijper commented Oct 3, 2024

You have to click on 'advanced settings' to reach the fallback settings that citizen-01 is mentioning. It took me some time to figure this out. This might be helpfull for other people reading this.

Checkout screenshot:
https://prnt.sc/MnNKpLE5BoG7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment