<?php
/*
Plugin Name: Wishlist
Description: Adds a Love icon to products and enables a wishlist functionality.
Version: 1.0
Author: Bishal Thapa
*/
// Shortcode [user_wishlist]
// Shortcode for count[wishlist_count]
// Add love icon to product
function add_love_icon_to_product() {
global $product;
$product_id = $product->get_id();
// Check if the product is in the wishlist
$user_id = get_current_user_id();
$wishlist = get_user_meta($user_id, 'user_wishlist', true);
if (in_array($product_id, (array) $wishlist)) {
$like_text = '✅ Liked';
$wishlist_count = !empty($wishlist) ? count($wishlist) : 0;
echo '<a href="#" class="add-to-wishlist liked" data-product_id="' . esc_attr($product_id) . '"><span class="wishlist-count">' . $wishlist_count . '</span> ' . $like_text . '</a>';
echo ' <a href="/wishlist">👁️ View Wishlist</a>';
} else {
$like_text = '💝 Like or Save';
echo '<a href="#" class="add-to-wishlist" data-product_id="' . esc_attr($product_id) . '">' . $like_text . '</a>';
}
}
add_action('woocommerce_after_shop_loop_item', 'add_love_icon_to_product', 20);
add_action('woocommerce_single_product_summary', 'add_love_icon_to_product', 5);
function show_wishlist_or_login_form() {
if (!is_user_logged_in()) {
// If the user is not logged in, display the WooCommerce login form along with the registration button
ob_start();
woocommerce_login_form(array('redirect' => '/wishlist'));
$login_form = ob_get_clean();
$registration_button = '<button style="background-color: blue; color: white; padding: 10px; border: none; border-radius: 5px; margin-top: -4px; margin-right: 10px;" onclick="window.location.href=\'https://www.hlomart.com/registration\';"><i class="fa fa-user-plus" style="margin-right: 5px;"></i>Registration</button>';
return '<p>You need to be logged in to view your wishlist.</p>' . $login_form . $registration_button;
}
$user_id = get_current_user_id();
$wishlist = get_user_meta($user_id, 'user_wishlist', true);
$output = '';
if (!empty($wishlist)) {
// Reverse the order of the wishlist array to show the latest items at the top
$wishlist = array_reverse($wishlist);
// Wishlist count
$wishlist_count = count($wishlist);
$output .= '<p>Total Items in Your Wishlist: ' . $wishlist_count . '</p>';
$output .= '<ul>';
foreach ($wishlist as $product_id) {
$product = wc_get_product($product_id);
$output .= '<li>';
// Add the "Remove from Wishlist" button
$output .= '<div class="remove-from-wishlist"><a href="#" data-product_id="' . esc_attr($product_id) . '" class="remove-from-wishlist-button">👎 Remove</a></div>';
// Display the small product image on the left side, make it clickable
$output .= '<div class="product-image"><a href="' . esc_url(get_permalink($product_id)) . '">' . $product->get_image('thumbnail') . '</a></div>';
// Display the product details
$output .= '<div class="product-details">';
// Display the product name as a clickable link with a custom CSS class for red color
$output .= '<div class="product-name red-product-title"><a href="' . esc_url(get_permalink($product_id)) . '">' . esc_html($product->get_name()) . '</a></div>';
// Get the product categories
$product_categories = wp_get_post_terms($product_id, 'product_cat');
if (!empty($product_categories)) {
$output .= '<div class="product-categories">';
$category_links = array();
foreach ($product_categories as $category) {
$category_link = get_term_link($category->term_id, 'product_cat');
if (!is_wp_error($category_link)) {
$category_links[] = '<a href="' . esc_url($category_link) . '" class="category">' . $category->name . '</a>';
}
}
// Display "Categories:" before the category links
$output .= 'Categories: ' . implode(', ', $category_links);
$output .= '</div>';
}
// Add the "Add to Cart" button
$output .= '<div class="add-to-cart">' . do_shortcode('[add_to_cart id="' . $product_id . '"]') . '</div>';
$output .= '</div>'; // Close product-details
$output .= '</li>';
}
$output .= '</ul>';
} else {
$output .= 'Your wishlist is empty.';
}
return $output;
}
add_shortcode('user_wishlist', 'show_wishlist_or_login_form');
// Add shortcode [wishlist_count]
function show_wishlist_count() {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$wishlist = get_user_meta($user_id, 'user_wishlist', true);
$wishlist_count = !empty($wishlist) ? count($wishlist) : 0;
return '<span class="wishlist-count">' . $wishlist_count . '</span>';
} else {
return ''; // If the user is not logged in, return an empty string
}
}
add_shortcode('wishlist_count', 'show_wishlist_count');
// Add wishlist scripts
function wishlist_scripts() {
?>
<style>
.add-to-wishlist {
color: blue; /* Change to blue */
font-size: 15px; /* Adjust size as needed */
cursor: pointer;
text-decoration: none;
font-weight: bold;
}
/* Custom CSS to make product titles red on the Wishlist page */
.red-product-title a {
color: #3F3F3F; /* Change to your desired color */
font-weight: bold;
}
.add-to-wishlist.liked {
color: red; /* Change to red when liked */
}
.product-image {
float: left;
margin-right: 10px; /* Add some margin to separate the image and button */
margin-left: -40px;
}
.product-image img {
max-width: 50px; /* Adjust the maximum width as needed */
max-height: 50px; /* Adjust the maximum height as needed */
}
.product-details {
overflow: hidden; /* Ensure the product details stay within the container */
margin-bottom: 10px; /* Add margin to separate products vertically */
border-bottom: 1px solid #ffffff; /* Add a white color divider */
padding-bottom: 10px; /* Add padding for spacing */
}
.add-to-cart {
float: right;
}
.product-name {
margin-left: 10px; /* Add some margin to separate the image and name */
}
/* Style for the wishlist count */
.wishlist-count {
background-color: red;
color: white;
font-size: 12px;
padding: 3px 5px;
border-radius: 50%;
margin-right: 5px;
display: inline; /* Added to show count only on liked products */
}
/* Style for the "Remove from Wishlist" button */
.remove-from-wishlist-button {
color: red;
text-decoration: underline;
cursor: pointer;
}
</style>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('body').on('click', '.add-to-wishlist', function(e) {
e.preventDefault();
var $link = $(this);
var product_id = $link.data('product_id');
var action = ($link.hasClass('liked')) ? 'remove_from_wishlist' : 'add_to_wishlist';
<?php if (!is_user_logged_in()) { ?>
// Redirect non-logged-in users to the wishlist page
window.location.href = '/wishlist';
return;
<?php } ?>
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: action,
product_id: product_id
},
success: function(response){
if (action === 'add_to_wishlist') {
$link.text('✅ Liked').addClass('liked');
$link.find('.wishlist-count').text(parseInt($link.find('.wishlist-count').text()) + 1);
$link.after(' <a href="/wishlist">👁️ View Wishlist</a>');
} else {
$link.text('💝 Like or Save').removeClass('liked');
$link.find('.wishlist-count').text(parseInt($link.find('.wishlist-count').text()) - 1);
$link.next('a').remove(); // Remove the "👁️ View" link when unliking
// Check if the product was not in the wishlist, then hide the count
if (parseInt($link.find('.wishlist-count').text()) <= 0) {
$link.find('.wishlist-count').css('display', 'none');
}
}
}
});
});
// Remove from Wishlist button click handler
$('body').on('click', '.remove-from-wishlist-button', function(e) {
e.preventDefault();
var $link = $(this);
var product_id = $link.data('product_id');
$.ajax({
type: 'POST',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: {
action: 'remove_from_wishlist',
product_id: product_id
},
success: function(response) {
// Reload the current page to update the wishlist display
location.reload();
}
});
});
});
</script>
<?php
}
add_action('wp_footer', 'wishlist_scripts');
// AJAX handler for adding/removing products from wishlist
add_action('wp_ajax_add_to_wishlist', 'add_to_wishlist_callback');
add_action('wp_ajax_nopriv_add_to_wishlist', 'add_to_wishlist_callback');
function add_to_wishlist_callback() {
if (!is_user_logged_in()) {
die('User is not logged in.');
}
$user_id = get_current_user_id();
$product_id = $_POST['product_id'];
$wishlist = get_user_meta($user_id, 'user_wishlist', true);
// Add product to wishlist
if (!in_array($product_id, (array) $wishlist)) {
$wishlist[] = $product_id;
update_user_meta($user_id, 'user_wishlist', $wishlist);
}
die();
}
add_action('wp_ajax_remove_from_wishlist', 'remove_from_wishlist_callback');
add_action('wp_ajax_nopriv_remove_from_wishlist', 'remove_from_wishlist_callback');
function remove_from_wishlist_callback() {
if (!is_user_logged_in()) {
die('User is not logged in.');
}
$user_id = get_current_user_id();
$product_id = $_POST['product_id'];
$wishlist = get_user_meta($user_id, 'user_wishlist', true);
// Remove product from wishlist
$index = array_search($product_id, $wishlist);
if ($index !== false) {
unset($wishlist[$index]);
update_user_meta($user_id, 'user_wishlist', $wishlist);
}
die();
}
?>