I need help refactoring or redoing the code to display the number of wishlisted per product.
Currently, my implementation is not functioning as expected, and I believe your expertise in the theme’s intricacies will be invaluable in resolving this issue.
I have attached the current code for your reference.
function woodmart_total_wishlist_count_shortcode() {
global $product;
if (!$product) {
return '';
}
$product_id = $product->get_id();
global $wpdb;
$wishlist_count = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(*) FROM {$wpdb->usermeta}
WHERE meta_key = 'woodmart_wishlist'
AND meta_value LIKE %s
", '%"'. $product_id . '"%'));
if (!$wishlist_count) {
$wishlist_count = 0;
}
return '<div class="wishlist-count">Wishlisted: ' . $wishlist_count . ' times</div>';
}
add_shortcode('wishlist_count', 'woodmart_total_wishlist_count_shortcode');