| Server IP : 13.126.101.145 / Your IP : 216.73.217.37 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/wishlist-itrosys/ |
Upload File : |
<?php
/*
Plugin Name: WooCommerce Wishlist
Description: A custom plugin for adding WooCommerce products to a wishlist.
Version: 1.0
Author: Itrosys
Author URI: https://example.com/
License: GPL2
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Main Plugin Class
class WooCommerce_Wishlist {
public function __construct() {
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('woocommerce_single_product_summary', array($this, 'add_wishlist_button_single'), 35); // Single product page
add_action('woocommerce_after_shop_loop_item', array($this, 'add_wishlist_button_loop'), 20); // Product loop
add_action('wp_loaded', array($this, 'handle_wishlist_actions'));
add_action('init', array($this, 'create_wishlist_page'));
// Add custom endpoint for My Account page
add_action('init', array($this, 'add_custom_endpoint'));
add_action('woocommerce_account_wishlist_endpoint', array($this, 'wishlist_endpoint_content'));
add_filter('woocommerce_account_menu_items', array($this, 'add_wishlist_endpoint_menu_item'));
add_filter('query_vars', array($this, 'add_query_vars'));
}
public function add_wishlist_button_single() {
$product_id = get_the_ID();
$wishlist = $this->get_wishlist();
// if (is_user_logged_in()) {
// if (in_array($product_id, $wishlist)) {
// echo '<button class="button" title="Already in wishlist" disabled><i class="fa fa-heart-o"></i></button>';
// } else {
// echo '<button class="button add_to_wishlist" style="position:unset !important;background-color:#000 !important; border-radius:unset !important; padding:10px !important;border-radius:5px !important;
// " data-product-id="' . $product_id . '" data-wishlist-url="' . $this->get_wishlist_url() . '"><i class="fa fa-heart-o" style="position:unset !important; color:#ffbd2b !important;"></i> </button>';
// }
// } else {
// // If the user is not logged in, always show "Add to Wishlist" button
// echo '<button class="button add_to_wishlist_for_loggedout" data-product-id="' . $product_id . '" data-wishlist-url="' . $this->get_wishlist_url() . '"><i class="fa fa-heart-o"></i> </button>';
// }
}
public function add_wishlist_button_loop() {
global $product;
$product_id = $product->get_id();
$wishlist = $this->get_wishlist();
if (is_user_logged_in()) {
if (in_array($product_id, $wishlist)) {
echo '<button class="button already_in_wishlist-disabled" title ="Already in Wishlist" disabled><i class="fa fa-heart-o"></i></button>';
} else {
echo '<button class="button add_to_wishlist" data-product-id="' . $product_id . '" data-wishlist-url="' . $this->get_wishlist_url() . '"><i class="fa fa-heart-o"></i> </button>';
}
} else {
echo '<button class="button add_to_wishlist" data-product-id="' . $product_id . '" data-wishlist-url="' . $this->get_wishlist_url() . '"><i class="fa fa-heart-o"></i> </button>';
}
/// Get the product URL
$product_url = get_permalink($product_id);
$current_url = $_SERVER['REQUEST_URI']; // Get the current page URL path
if (strpos($current_url, '/rent') !== false) {
echo '<button class="button rent_now" onclick="window.location.href=\'' . esc_url($product_url) . '\';">Rent Now</button>';
} elseif (strpos($current_url, '/buy-used/') !== false) {
// echo '<button class="button rent_now" onclick="window.location.href=\'' . esc_url($product_url) . '\';">Buy Now</button>';
}
}
public function handle_wishlist_actions() {
if (isset($_GET['add_to_wishlist']) && is_user_logged_in()) {
$product_id = intval($_GET['add_to_wishlist']);
$wishlist = $this->get_wishlist();
if (!in_array($product_id, $wishlist)) {
$wishlist[] = $product_id; // Add the new product to the array
$this->save_wishlist($wishlist); // Save the updated wishlist
}
wp_redirect($this->get_wishlist_url());
exit;
}
if (isset($_GET['remove_from_wishlist']) && is_user_logged_in()) {
$product_id = intval($_GET['remove_from_wishlist']);
$wishlist = $this->get_wishlist();
if (($key = array_search($product_id, $wishlist)) !== false) {
unset($wishlist[$key]); // Remove the product from the array
$this->save_wishlist($wishlist); // Save the updated wishlist
}
wp_redirect($this->get_wishlist_url());
exit;
}
}
public function get_wishlist() {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
// Retrieve the wishlistId value from user_meta
$wishlist = get_user_meta($user_id, 'wishlistId', true);
// Convert the comma-separated list into an array if it's not empty
return !empty($wishlist) ? explode(',', $wishlist) : array();
}
return array(); // No wishlist for non-logged-in users
}
public function save_wishlist($wishlist) {
if (is_user_logged_in()) {
$user_id = get_current_user_id();
// Convert the wishlist array into a comma-separated string
$wishlist_string = implode(',', $wishlist);
// Save the comma-separated list of product IDs in the 'wishlistId' meta key
update_user_meta($user_id, 'wishlistId', $wishlist_string);
}
}
public function display_wishlist() {
$wishlist = $this->get_wishlist();
if (empty($wishlist)) {
return '<p>Your wishlist is empty. <a href="' . home_url('/rent') . '">Browse products to add to your wishlist</a>.</p>';
}
$output = '<form method="post" action="' . $this->get_wishlist_url() . '">';
$output .= '<table class="woocommerce-wishlist-table">';
$output .= '<thead>';
$output .= '<tr>';
$output .= '<th class="wishlist-table">Sr. No</th>';
$output .= '<th class="wishlist-table">Image</th>';
$output .= '<th class="wishlist-table">Product Name</th>';
$output .= '<th class="wishlist-table">Price</th>';
$output .= '<th class="wishlist-table">Remove</th>';
$output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';
$serial_number = 1;
foreach ($wishlist as $product_id) {
$product = wc_get_product($product_id);
$row_class = ($serial_number % 2 == 0) ? 'even-row' : 'odd-row';
$output .= '<tr class="' . $row_class . '">';
$output .= '<td>' . $serial_number . '</td>';
$output .= '<td style="width:25%">' . $product->get_image() . '</td>';
$output .= '<td><a href="' . get_permalink($product_id) . '">' . $product->get_name() . '</a></td>';
$output .= '<td>' . $product->get_price_html() . '</td>';
$output .= '<td><a href="' . $this->get_wishlist_url() . '?remove_from_wishlist=' . $product_id . '" class="remove"><i class="fa fa-trash"></i><span class="tooltip">Remove Equipment from Wishlist</span></a></td>';
$output .= '</tr>';
$serial_number++;
}
$output .= '</tbody>';
$output .= '</table>';
$output .= '</form>';
return $output;
}
public function enqueue_scripts() {
// Enqueue Font Awesome for heart icon
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css', array(), '6.0.0');
// Enqueue the plugin's script
wp_enqueue_script('woocommerce-wishlist', plugin_dir_url(__FILE__) . 'js/wishlist.js', array('jquery'), '1.0', true);
// Add custom CSS for wishlist table
wp_enqueue_style('woocommerce-wishlist-style', plugin_dir_url(__FILE__) . 'css/wishlist.css');
}
private function get_wishlist_url() {
return wc_get_account_endpoint_url('wishlist');
}
public function create_wishlist_page() {
if (get_page_by_path('wishlist') == NULL) {
$wishlist_page = array(
'post_title' => 'Wishlist',
'post_name' => 'wishlist',
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'page',
);
wp_insert_post($wishlist_page);
}
}
public function add_custom_endpoint() {
add_rewrite_endpoint('wishlist', EP_PAGES);
}
public function add_wishlist_endpoint_menu_item($items) {
// Rearrange items and insert 'Wishlist' at the 5th position
$new_items = array();
// Insert existing items up to 4th position
$i = 0;
foreach ($items as $key => $value) {
if ($i == 4) {
$new_items['wishlist'] = __('Wishlist', 'woocommerce');
}
$new_items[$key] = $value;
$i++;
}
// If there are fewer than 5 items, ensure 'Wishlist' is added
if (count($items) < 4) {
$new_items['wishlist'] = __('Wishlist', 'woocommerce');
}
return $new_items;
}
public function wishlist_endpoint_content() {
echo $this->display_wishlist();
}
public function add_query_vars($vars) {
$vars[] = 'wishlist';
return $vars;
}
}
// Initialize the plugin
new WooCommerce_Wishlist();