Skip to content

Instantly share code, notes, and snippets.

@thomasgriffin
Created February 7, 2014 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasgriffin/8862970 to your computer and use it in GitHub Desktop.
Save thomasgriffin/8862970 to your computer and use it in GitHub Desktop.
Allow variable "custom" pricing for upgrades using a specific input field name "envira-custom-price". Change to whatever suits your needs.
<?php
add_filter( 'edd_add_to_cart_item', 'tgm_cp_add_to_cart_item' );
add_filter( 'edd_ajax_pre_cart_item_template', 'tgm_cp_add_to_cart_item' );
function tgm_cp_add_to_cart_item( $cart_item ) {
if ( ! empty( $_POST['post_data'] ) ) {
$post_data = array();
wp_parse_str( $_POST['post_data'], $post_data );
if ( isset( $post_data['envira-custom-price'] ) ) {
$cart_item['options']['custom_price'] = edd_sanitize_amount( $post_data['envira-custom-price'] );
}
}
return $cart_item;
}
add_filter( 'edd_pre_add_to_cart', 'tgm_cp_pre_add_to_cart', 10, 2 );
function tgm_cp_pre_add_to_cart( $download_id, $options ) {
if ( ! empty( $_POST['post_data'] ) ) {
$post_data = array();
wp_parse_str( $_POST['post_data'], $post_data );
if ( isset( $post_data['envira-custom-price'] ) ) {
$options['custom_price'] = edd_sanitize_amount( $post_data['envira-custom-price'] );
}
}
return $options;
}
add_filter( 'edd_cart_item_price', 'tgm_cp_cart_item_price', 10, 4 );
function tgm_cp_cart_item_price( $price, $item_id, $options = array(), $tax ) {
if ( ! empty( $options['custom_price'] ) ) {
$price = $options['custom_price'];
}
return $price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment