Last active
September 25, 2025 20:10
-
-
Save sabrina-zeidan/afe47d02fdfb7a6df248e3992c7b7044 to your computer and use it in GitHub Desktop.
TI Wishlist plugin add random products to users wishlists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //https://example.com/?add_to_wishlist_user=1&add_to_wishlist_product=5503 | |
| //https://example.com/?add_to_wishlist_user=1&add_random=5 | |
| function add_product_to_wishlist( $user_id, $product_id ) { | |
| global $wpdb; | |
| // 1. Get or create default wishlist for user | |
| $wishlist = $wpdb->get_var( $wpdb->prepare( | |
| "SELECT ID FROM {$wpdb->prefix}tinvwl_lists | |
| WHERE author = %d AND type = 'default' LIMIT 1", | |
| $user_id | |
| )); | |
| if ( ! $wishlist ) { | |
| $wpdb->insert( | |
| "{$wpdb->prefix}tinvwl_lists", | |
| array( | |
| 'author' => $user_id, | |
| 'title' => 'Wishlist', | |
| 'type' => 'default', | |
| 'status' => 'private', | |
| 'date' => current_time( 'mysql' ), | |
| ), | |
| array( '%d', '%s', '%s', '%s', '%s' ) | |
| ); | |
| $wishlist = $wpdb->insert_id; | |
| } | |
| // 2. Prevent duplicates | |
| $exists = $wpdb->get_var( $wpdb->prepare( | |
| "SELECT ID FROM {$wpdb->prefix}tinvwl_items | |
| WHERE wishlist_id = %d AND product_id = %d LIMIT 1", | |
| $wishlist, $product_id | |
| )); | |
| if ( ! $exists ) { | |
| $product = wc_get_product( $product_id ); | |
| $price = $product ? $product->get_price() : 0; | |
| $in_stock = $product && $product->is_in_stock() ? 1 : 0; | |
| // Insert item | |
| $wpdb->insert( | |
| "{$wpdb->prefix}tinvwl_items", | |
| array( | |
| 'wishlist_id' => $wishlist, | |
| 'product_id' => $product_id, | |
| 'variation_id'=> 0, | |
| 'formdata' => '{"tinvwl-hidden-fields":"[\"quantity\"]"}', | |
| 'author' => $user_id, | |
| 'date' => current_time( 'mysql' ), | |
| 'quantity' => 1, | |
| 'price' => $price, | |
| 'in_stock' => $in_stock, | |
| ), | |
| array( '%d','%d','%d','%s','%d','%s','%d','%f','%d' ) | |
| ); | |
| // Optional analytics | |
| $wpdb->query( $wpdb->prepare( | |
| "INSERT INTO {$wpdb->prefix}tinvwl_analytics | |
| (wishlist_id, product_id, variation_id, ID, cart) | |
| VALUES (%d, %d, 0, %s, 1) | |
| ON DUPLICATE KEY UPDATE cart = cart+1", | |
| $wishlist, $product_id, md5( uniqid( rand(), true ) ) | |
| )); | |
| } | |
| return $wishlist; | |
| } | |
| // 🔥 Hook into init and listen for query string | |
| add_action( 'init', function() { | |
| if ( isset($_GET['add_to_wishlist_user']) ) { | |
| $user_id = absint( $_GET['add_to_wishlist_user'] ); | |
| // Case 1: specific IDs provided | |
| if ( isset($_GET['add_to_wishlist_products']) ) { | |
| $ids = explode( ',', sanitize_text_field( $_GET['add_to_wishlist_products'] ) ); | |
| foreach ( $ids as $pid ) { | |
| $pid = absint( $pid ); | |
| if ( $pid ) { | |
| add_product_to_wishlist( $user_id, $pid ); | |
| } | |
| } | |
| } | |
| // Case 2: random products requested | |
| if ( isset($_GET['add_random']) ) { | |
| $count = absint( $_GET['add_random'] ); | |
| if ( $count > 0 ) { | |
| // Grab random products from WooCommerce catalog | |
| $random_products = wc_get_products( array( | |
| 'limit' => $count, | |
| 'orderby' => 'rand', | |
| 'status' => 'publish', | |
| ) ); | |
| foreach ( $random_products as $product ) { | |
| add_product_to_wishlist( $user_id, $product->get_id() ); | |
| } | |
| } | |
| } | |
| // Optional: redirect after done | |
| wp_redirect( site_url( '/wishlist/' ) ); | |
| exit; | |
| } | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment