Last active
October 3, 2024 13:40
-
-
Save Crocoblock/bcf96e00d54d84f38865d175d01fbe67 to your computer and use it in GitHub Desktop.
JetEngine Get purchased Woocommerce products macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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