Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.