Last active
December 14, 2015 02:29
-
-
Save hiranthi/5014142 to your computer and use it in GitHub Desktop.
Adding products to the wishlist through AJAX so customers don't leave the current page (for Shopp - Wishlist Pro from Shopplugin.nl).
This file contains 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
jQuery(document).ready(function($) { | |
// the add-to-wishlist links should be inside an id="edit-wishlist-pro", ie a div or a span | |
$('#edit-wishlist-pro a').click(function(e) { | |
e.preventDefault(); | |
// the surrounding grandparent should have a rel attached that holds the ID of the product (ie rel="12") | |
var product = $(this).parent().parent().attr('rel'); | |
// the parent (<p>) doesn't have a 'remove' class, so we're adding to the wishlist | |
if ( ! $(this).parent().hasClass('remove') ) | |
{ | |
/* Send the data using post and put the results back in as the #add-to-wishlist results */ | |
$.post( | |
ShoppNL.ajaxurl, | |
{ | |
'action' : 'shoppnl_handle_ajax', | |
'handle' : 'add_to_wishlist', | |
'data' : 'product='+product, | |
'security' : ShoppNL.nonce | |
}, | |
function( response ) { | |
$('#edit-wishlist-pro').contents( response ); | |
} | |
); | |
} | |
// The link does have a class 'remove', so we want to remove the product from the wishlist | |
else | |
{ | |
/* Send the data using post and put the results back in as the #add-to-wishlist results */ | |
$.post( | |
ShoppNL.ajaxurl, | |
{ | |
'action' : 'shoppnl_handle_ajax', | |
'handle' : 'remove_from_wishlist', | |
'data' : 'product='+product, | |
'security' : ShoppNL.nonce | |
}, | |
function( response ) { | |
$('#edit-wishlist-pro').contents( response ); | |
} | |
); | |
} | |
return false; | |
}); | |
}); |
This file contains 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 | |
function shoppnl_wishlistpro_handle_ajax() | |
{ | |
// make sure the referer is legit | |
check_ajax_referer( 'shoppnl-handle-ajax', 'security' ); | |
$handle = strtolower(strip_tags($_REQUEST['handle'])); | |
parse_str($_REQUEST['data'], $data); | |
/* switch between the different AJAX handles */ | |
switch($handle) | |
{ | |
// Add to the wishlist | |
case 'add_to_wishlist': | |
$user_ID = get_customer_id(shopp('customer','email','mode=value&return=true')); | |
if (SHOPP_DEBUG) new ShoppError('AJAX: $user_ID = '.$user_ID,false,SHOPP_DEBUG_ERR); | |
$wishlist = new WishlistObject($user_ID); | |
$wishlist->parent = $user_ID; | |
if (SHOPP_DEBUG) new ShoppError('Adding product #'.$product.' to the wishlist',false,SHOPP_DEBUG_ERR); | |
$wishlist->value[$product] = $product; | |
// save the wishlist | |
if ( $wishlist->save() ) | |
{ | |
echo __('<span class="add success">This product has been added to your wishlist.</span>'); | |
} | |
else | |
{ | |
echo __('<span class="add error">Something went wrong, the product could not be added to your website.</span>'); | |
} | |
break; | |
// Remove from the wishlist | |
case 'remove_from_wishlist': | |
// get the wishlist Object | |
$wishlist = new WishlistObject( $user_ID ); | |
$wishlist->parent = $user_ID; | |
// unset the product | |
unset( $wishlist->value[ $product ] ); | |
// save the wishlist | |
$wishlist->save(); | |
if ( ! $wishlist->value[ $product ] ) | |
{ | |
echo __('<span class="remove success">The product was removed from your wishlist.</span>'); | |
} | |
else | |
{ | |
echo __('<span class="remove error">Something went wrong, the product could not be removed from your wishlist.</span>'); | |
} | |
break; | |
} # end switch | |
// we have to die to make sure there's no -1 at the end of the output | |
die; | |
} // end onx_wishlistpro_handle_ajax | |
add_action('wp_ajax_shoppnl_handle_ajax', 'shoppnl_wishlistpro_handle_ajax'); // loggedin | |
add_action('wp_ajax_nopriv_shoppnl_handle_ajax', 'shoppnl_wishlistpro_handle_ajax'); // not loggedin |
This file contains 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 | |
include('ajax.php'); | |
// add the JS for AJAX support | |
function shopp_wishlistpro_custom_ajax() | |
{ | |
// don't forget to adjust the URL to the correct URL | |
wp_register_script('shopp-wishlist-pro-custom', '//domain.com/ajax.js', array('jquery'), '1.0', true); | |
// localize script | |
wp_localize_script( 'shopp-wishlist-pro-custom', 'ShoppNL', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce('shoppemailmrktng-handle-ajax') ) ); | |
// enqueue script | |
wp_enqueue_script('shopp-wishlist-pro-custom'); | |
} // end shopp_wishlistpro_custom_ajax | |
add_action('wp_enqueue_scripts', 'shopp_wishlistpro_custom_ajax'); |
This file contains 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 | |
// then add this to the product-page where you want the add-to-wishlist "button" (should work from category page too): | |
?> | |
<div id="edit-wishlist-pro"> | |
<?php | |
$args = array( | |
'type' => 'link', | |
'linklisted' => true, | |
'before' => '<div rel="'.shopp('product','id','return=true').'">', | |
'after' => '</div>' | |
); | |
shopp('wishlist-pro','add-link',$args); | |
?> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment