Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable price option support. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions edd-external-purchase-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public function query_vars( $vars ) {
$vars[] = 'trans_type';
$vars[] = 'payment_id';
$vars[] = 'product_id';
$vars[] = 'price_id';
$vars[] = 'quantity';
$vars[] = 'price';
$vars[] = 'first_name';
$vars[] = 'last_name';
Expand Down Expand Up @@ -192,7 +194,7 @@ public function get_product_type( $product_id ) {
* @param int $product_id product ID in the database
* @return string
*/
public function get_product_files( $product_id ) {
public function get_product_files( $product_id, $price_id = null ) {

// check item type
$itemtype = $this->get_product_type( $product_id );
Expand All @@ -201,7 +203,7 @@ public function get_product_files( $product_id ) {
if ( $itemtype == 'default' ) :
$data[] = array(
'id' => absint( $product_id ),
'file' => edd_get_download_files( $product_id ),
'file' => edd_get_download_files( $product_id, $price_id ),
'name' => $this->get_product_name( $product_id ),
);

Expand Down Expand Up @@ -856,7 +858,7 @@ public function process_payment( $wp_query ) {

// fetch my default price and check for custom passed
$default = $this->get_product_price( $wp_query->query_vars['product_id'] );
$price = ! isset( $wp_query->query_vars['price'] ) || empty( $wp_query->query_vars['price'] ) ? $default : $wp_query->query_vars['price'];
$price = ! isset( $wp_query->query_vars['price'] ) ? $default : $wp_query->query_vars['price'];

// set up an array of external data stuff
$source_name = ! empty( $wp_query->query_vars['source_name'] ) ? $wp_query->query_vars['source_name'] : '';
Expand All @@ -870,6 +872,8 @@ public function process_payment( $wp_query ) {
// build data array of purchase info
$data = array(
'product_id' => absint( $wp_query->query_vars['product_id'] ),
'price_id' => isset($wp_query->query_vars['price_id']) ? absint( $wp_query->query_vars['price_id'] ) : false,
'quantity' => isset($wp_query->query_vars['quantity']) ? absint( $wp_query->query_vars['quantity'] ) : 1,
'price' => edd_sanitize_amount( $price ),
'first' => esc_attr( $wp_query->query_vars['first_name'] ),
'last' => esc_attr( $wp_query->query_vars['last_name'] ),
Expand Down Expand Up @@ -1002,25 +1006,40 @@ public function create_payment( $data ) {
);

$price = edd_sanitize_amount( strip_tags( trim( $data['price'] ) ) );

// Handle price options
$price_id = isset( $data['price_id'] ) ? $data['price_id'] : null;

if( ! is_null($price_id) ) {
$item_number = array(
'id' => $data['product_id'],
'options' => array(
'price_id' => $price_id,
'quantity' => $data['quantity'],
)
);
} else {
$item_number = $data['product_id'];
}

// fetch download files
$downloads = $this->get_product_files( $data['product_id'] );

$downloads = $this->get_product_files( $data['product_id'], $price_id );
// set up cart details
$cart_details[] = array(
'name' => $this->get_product_name( $data['product_id'] ),
'id' => $data['product_id'],
'item_number' => $data['product_id'],
'item_number' => $item_number,
'price' => $price,
'quantity' => 1,
'quantity' => $data['quantity'],
'tax' => 0,
);

$date = ! empty( $data['date'] ) ? strip_tags( trim( $data['date'] ) ) : 'NOW';
$date = date( 'Y-m-d H:i:s', strtotime( $date, current_time( 'timestamp' ) ) );

$purchase_data = array(
'price' => edd_sanitize_amount( $price ),
'price' => $price,
'tax' => 0,
'post_date' => $date,
'purchase_key' => strtolower( md5( uniqid() ) ), // random key
Expand Down Expand Up @@ -1160,4 +1179,4 @@ public function whitelist( $sites ) {

register_activation_hook( __FILE__, array( 'EDD_External_Purchase_API', 'activate' ) );

new EDD_External_Purchase_API();
new EDD_External_Purchase_API();