Developer API functions
Simple WishlistsUpdated
The plugin has a small set of public PHP functions for themes and other plugins. They act on the current visitor (a logged-in customer or a guest) unless you pass a user ID, and return a WP_Error on failure. Use these functions rather than writing to the wishlist tables, so counts and caches stay correct.
Add a product
<?php
// PHP: returns the new item ID, or WP_Error.
$item_id = swlp_add_to_wishlist( $product_id, array(
'user_id' => 0, // Add for this user instead of the current visitor.
'list_id' => 0, // Target list. Defaults to the owner's default list.
'variation_id' => 0, // Variation ID.
'attributes' => array(), // Chosen "any" attributes, e.g. array( 'attribute_pa_colour' => 'blue' ).
'quantity' => 1, // Desired quantity.
) );
$product_id can also be a variation ID. All the arguments are optional.
Remove a product
<?php
// PHP: removes the product from every list of the visitor (or user). Returns the number of items removed.
$removed = swlp_remove_from_wishlist( $product_id, $variation_id = 0, $user_id = 0 );
A $variation_id of 0 removes the product itself.
Check and count
<?php
// PHP
if ( swlp_is_in_wishlist( $product_id, $variation_id = 0, $user_id = 0 ) ) {
// The visitor (or user) has saved this product.
}
$count = swlp_get_wishlist_count( $user_id = 0 ); // Number of saved items.
Get the current list
<?php
// PHP: the visitor's default wishlist as an array, or null.
// Pass true to create it (and the guest cookie) if it does not exist yet.
$list = swlp_get_current_list( false );
The list array includes keys such as id, user_id, title, privacy (private, shared or public), is_default, type, token and item_count.
Get URLs
swlp_get_wishlist_url(): the wishlist page, or the My Account Wishlist tab when no page is set. Filter:swlp_wishlist_url.swlp_get_share_url( $list ): the share link of a list, for example/wishlist/view/<token>/. Filter:swlp_share_url. Theviewpart can be changed with theswlp_share_slugfilter.
Check the plugin is active
Wrap calls in function_exists( 'swlp_add_to_wishlist' ) so your code does not fail if the plugin is deactivated.


