sinanisler logo

WooCommerce Product User Wishlist Snippet

If you need a very very simple wishlist button for woocommerce here you go enjoy.

This code adds 2 shortcode one for product page and one for wishlist page itself.

The current user can add products to its own wishlist. Wishlist is just a simple array in the user profile custom field.

I didn’t add any CSS styling do that yourself 😉

For Product Page: [add_to_wishlist_button]

For Wishlist Page: [user_wishlist]


// For Product Page: [add_to_wishlist_button] 

// For Wishlist Page: [user_wishlist] 

// Enqueue the native JS script for AJAX from the child theme
function enqueue_wishlist_scripts() {
    // Enqueue custom.js from the child theme directory
    wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/custom.js', array(), null, true);
    wp_localize_script('custom-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('wishlist_nonce')));
}
add_action('wp_enqueue_scripts', 'enqueue_wishlist_scripts');

// Shortcode to add "Add to/Delete from Wishlist" button
function add_to_wishlist_button() {
    if (is_user_logged_in()) {
        global $product;
        $product_id = get_the_ID();
        $user_id = get_current_user_id();
        $wishlist = get_user_meta($user_id, 'wishlist_products', true);
        
        // Determine button text based on whether the product is already in the wishlist
        $button_text = in_array($product_id, (array)$wishlist) ? 'Delete from Wishlist' : 'Add to Wishlist';
        
        // Button HTML with AJAX trigger
        return '<button class="add-to-wishlist" data-product-id="' . esc_attr($product_id) . '">' . esc_html($button_text) . '</button>
        <div class="wishlist-message"></div>';
    } else {
        return '<p><a href="' . esc_url(wp_login_url(get_permalink())) . '" class="wishlist-shortcode-link">Log in</a> to wishlist.</p>';
    }
}
add_shortcode('add_to_wishlist_button', 'add_to_wishlist_button');

// AJAX handler to add or remove product from user wishlist
function handle_toggle_wishlist() {
    // Check if user is logged in and verify nonce
    if (is_user_logged_in() && isset($_POST['product_id']) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'wishlist_nonce')) {
        $user_id = get_current_user_id();
        $product_id = intval($_POST['product_id']);

        // Ensure the product ID is valid
        if (get_post_type($product_id) === 'product') {
            $wishlist = get_user_meta($user_id, 'wishlist_products', true);
            if (empty($wishlist)) {
                $wishlist = array();
            }

            if (!in_array($product_id, $wishlist)) {
                // Add product to wishlist
                $wishlist[] = $product_id;
                update_user_meta($user_id, 'wishlist_products', $wishlist);
                echo 'Product added to wishlist!';
            } else {
                // Remove product from wishlist
                $wishlist = array_diff($wishlist, array($product_id));
                update_user_meta($user_id, 'wishlist_products', $wishlist);
                echo 'Product removed from wishlist!';
            }
        } else {
            echo 'Invalid product.';
        }
    } else {
        echo 'Error: Invalid request or you need to log in to modify wishlist.';
    }
    wp_die();
}
add_action('wp_ajax_toggle_wishlist', 'handle_toggle_wishlist');

// Shortcode to display user's wishlist products
function display_user_wishlist() {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        $wishlist = get_user_meta($user_id, 'wishlist_products', true);

        if (!empty($wishlist)) {
            $output = '<ul class="user-wishlist">';
            foreach ($wishlist as $product_id) {
                $product = wc_get_product($product_id);
                if ($product) {
                    $output .= '<li><a href="' . esc_url(get_permalink($product_id)) . '">' . esc_html($product->get_name()) . '</a></li>';
                }
            }
            $output .= '</ul>';
        } else {
            $output = '<p>You have no wishlist products.</p>';
        }
        return $output;
    } else {
        return '<p>You need to <a href="' . esc_url(wp_login_url(get_permalink())) . '" class="wishlist-list-shortcode-link">log in</a> to see your wishlist.</p>';
    }
}
add_shortcode('user_wishlist', 'display_user_wishlist');






Leave the first comment