Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Created June 24, 2016 20:44
Show Gist options
  • Save Shelob9/5dea674590ffcdc73d14d31bdc04cf1f to your computer and use it in GitHub Desktop.
Save Shelob9/5dea674590ffcdc73d14d31bdc04cf1f to your computer and use it in GitHub Desktop.
Add upsells to higher variable price in EDD checkout. See README

This adds an upsell option in the EDD checkout for products with variable prices. Each option that has a higher price is shown, and then when you click that, it changes the price.

To see it in action, go to https://CalderaWP.com/caldera-forms-add-ons/ choose and add-on and add it to cart. Then you should see the two more expensive options listed. Click them to get upsold.

This requires EDD 2.6 or the hook will not have the 2nd arg (awesomemotive/easy-digital-downloads#4685)

Things I plan to add to this:

  • Better CSS
  • Analytics tracking
  • Ingot powered A/B testing for how to phrase the the upsell text.
/**
* Add upsell options to cart items on variable prices options
*/
add_action( 'edd_checkout_cart_item_title_after', function( $item, $key ){
//Register JS -- probably should do this earlier
wp_register_script( 'cwp-cart', get_stylesheet_directory_uri() . '/assets/js/cart.js', [ 'jquery' ] );
$id = $item[ 'id' ];
//check if we have variable prices, else bail
if( edd_has_variable_prices( $id ) ){
//load the JS we need.
wp_enqueue_script( 'cwp-cart' );
//find current price and all prices
$cart = edd_get_cart_content_details();
$current_price = $cart[ $key ][ 'price' ];
$prices = edd_get_variable_prices( $id );
foreach ( $prices as $price_id => $price ){
if( isset( $price[ 'index' ] ) && is_numeric( $price[ 'index' ] ) ){
$price_id = $price[ 'index' ];
}
$variant_price = (float) $price[ 'amount'] ;
//don't show same or lower price options
if( $variant_price == $current_price || $variant_price < $current_price ){
continue;
}
//output the upsells
$nonce = wp_create_nonce( $price_id . $id );
printf( '<div><a class="cwp-cart-change" data-price="%d" data-id="%d" data-nonce="%s" data-key="%s">Change To %s License <span>%s</span></a></div>',
esc_attr( $price_id ),
esc_attr( $id ),
esc_attr( $nonce ),
esc_attr( $key ),
esc_html( $price[ 'name' ] ),
edd_sanitize_amount( $price[ 'amount' ] ) );
}
}
}, 10, 2 );
/**
* AJAX callback for upsells
*/
add_action( 'wp_ajax_cwp_change_price', 'cwp_sf_child_change_price' );
add_action( 'wp_ajax_nopriv_cwp_change_price', 'cwp_sf_child_change_price' );
function cwp_sf_child_change_price(){
if( isset( $_POST[ 'id' ], $_POST[ 'price' ], $_POST[ 'nonce' ], $_POST[ 'key' ] ) ){
$cart_key = absint( $_POST[ 'key' ] );
$price_id = absint( $_POST[ 'price' ] );
$id = absint( $_POST[ 'id' ] );
$verified = wp_verify_nonce( $_POST[ 'nonce' ], $price_id . $id );
if( $verified ){
$added = edd_add_to_cart( $id, [ 'price_id' => $price_id ] );
if( $added ) {
edd_remove_from_cart( $cart_key );
}
}
}
}
jQuery( document ).ready( function ( $ ) {
$( '.cwp-cart-change' ).on( 'click', function ( e ) {
e.preventDefault();
var $item = $( this );
var data = {
action: 'cwp_change_price',
id: $item.attr( 'data-id' ),
price: $item.attr( 'data-price' ),
nonce: $item.attr( 'data-nonce' ),
key: $item.attr( 'data-key' )
};
$.ajax( {
type: "POST",
data: data,
dataType: "json",
url: edd_scripts.ajaxurl,
xhrFields: {
withCredentials: true
},
complete: function ( response ) {
//refresh window when complete
//would be cool to just refresh cart, but would make Stripe token wrong:(
window.location.reload();
}
} );
});
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment