Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created November 28, 2023 13:19
Show Gist options
  • Select an option

  • Save Lonsdale201/c708544e74429ff2d6abb43d3fd8f84e to your computer and use it in GitHub Desktop.

Select an option

Save Lonsdale201/c708544e74429ff2d6abb43d3fd8f84e to your computer and use it in GitHub Desktop.
JetEngine - Custom Macro - User Purchased Any Product
// place the code in the child theme functions.php or a custom code snippets plugin.
// This macro will return the users who are purchased any product on your website (and can select the order status)
// how to use
// Create a new User Query in the Query builder.
// Go to the Include/exclude section
// Click the Dynamic tag icon, and select the WC User Purchased Any Product macro. In the select field choose the order status, or use the "All" option
// This macro will return the selected user id
// My all Gist: https://gist.github.com/Lonsdale201
add_action( 'jet-engine/register-macros', function(){
class WC_User_Purchased_Any_Product extends \Jet_Engine_Base_Macros {
public function macros_tag() {
return 'wc_user_purchased_any_product';
}
public function macros_name() {
return 'WC User Purchased Any Product';
}
public function macros_args() {
return array(
'order_status' => array(
'label' => 'Order Status',
'type' => 'select',
'options' => array_merge( array( 'all' => 'All' ), wc_get_order_statuses() ),
'default' => 'all'
),
);
}
public function macros_callback( $args = array() ) {
$order_status = !empty( $args['order_status'] ) && 'all' !== $args['order_status'] ? $args['order_status'] : array_keys( wc_get_order_statuses() );
if ( !class_exists( 'WooCommerce' ) ) {
return 'WooCommerce is not active';
}
$query_args = array( 'numberposts' => -1, 'post_status' => $order_status );
$user_ids = array();
$orders = wc_get_orders( $query_args );
foreach ( $orders as $order ) {
$user_id = $order->get_user_id();
if ( $user_id && !in_array( $user_id, $user_ids ) ) {
$user_ids[] = $user_id;
}
}
if ( empty( $user_ids ) ) {
return 'No users have purchased products';
}
return implode( ',', $user_ids );
}
}
new WC_User_Purchased_Any_Product();
} );
@faisalae
Copy link

faisalae commented Dec 4, 2023

Thanks for the code, Is it possible to make this query for specific product_id?

@faisalae
Copy link

faisalae commented Dec 4, 2023

I have multiple buttons with the product names, I want a query to show a button only current user who purchase for a specific product purchase, not any.

@Lonsdale201
Copy link
Author

I have multiple buttons with the product names, I want a query to show a button only current user who purchase for a specific product purchase, not any.

Ill check this tomorrow :)

@faisalae
Copy link

faisalae commented Dec 4, 2023

Sure, Thank you

@faisalae
Copy link

faisalae commented Dec 5, 2023

Did you get a chance to check?

@Lonsdale201
Copy link
Author

Did you get a chance to check?

Hi, I haven't had time to write yet. But I don't fully understand it at the moment. So you want to create a product query? Because this is the code, user query.

So what you need is to query products, and for that user only show what they have purchased, but in a way that say only for the specified product. Wouldn't dynamic visibility be better here?

It's fine to make a new macro that returns products, but of all of them, only the one that was purchased, but to me at first glance what you want there is dynamic visibility simpler, but correct me if I'm wrong.

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