Automattic\WooCommerce\Blocks\Utils
CartCheckoutUtils::get_cart_product_ids_for_user
Get product IDs from a user's persistent cart.
This method retrieves product IDs stored in the user's persistent cart meta. It can be used for abandoned cart emails, cart-based product collections, and other scenarios where cart products need to be retrieved for a user.
Method of the class: CartCheckoutUtils{}
No Hooks.
Returns
Array
Usage
$result = CartCheckoutUtils::get_cart_product_ids_for_user( ?int $user_id, ?string $user_email );
- ?int $user_id(required)
- .
- ?string $user_email(required)
- .
CartCheckoutUtils::get_cart_product_ids_for_user() CartCheckoutUtils::get cart product ids for user code WC 10.7.0
public static function get_cart_product_ids_for_user( ?int $user_id, ?string $user_email ) {
if ( empty( $user_id ) && ! empty( $user_email ) ) {
$user = get_user_by( 'email', $user_email );
if ( $user ) {
$user_id = $user->ID;
}
}
if ( empty( $user_id ) ) {
return array();
}
$cart_meta = get_user_meta( $user_id, '_woocommerce_persistent_cart_' . get_current_blog_id(), true );
if ( empty( $cart_meta ) || ! is_array( $cart_meta ) || empty( $cart_meta['cart'] ) ) {
return array();
}
return array_values(
array_unique(
array_filter(
array_map(
function ( $cart_item ) {
return isset( $cart_item['product_id'] ) ? intval( $cart_item['product_id'] ) : 0;
},
$cart_meta['cart']
)
)
)
);
}