/** * Create identity and track purchase * @param int $payment_id * @return null */ public static function track_purchase($payment_id) { $user_id = edd_get_payment_user_id($payment_id); $uid = EDD_Segment_Identity::get_uid_from_user_id($user_id); // Send identity $traits = array('name' => edd_email_tag_fullname($payment_id), 'email' => edd_get_payment_user_email($payment_id)); do_action('edd_segment_identify', $uid, $traits); // Track the purchase event $props = array('trans_id' => edd_get_payment_transaction_id($payment_id), 'total' => edd_get_payment_amount($payment_id), 'time' => strtotime(edd_get_payment_completed_date($payment_id))); do_action('edd_segment_track', $uid, 'Checkout', $props); }
/** * Maybe log a recommendation sale * Iterates through items in the payment and if one is a recommendation logs it * * @since 1.2.6 * @param int $payment_id The Payment ID being completed * @return void */ function edd_rp_log_recommendation_sale($payment_id) { $payment_items = edd_get_payment_meta_cart_details($payment_id, true); foreach ($payment_items as $item) { if (!empty($item['item_number']['recommendation_source'])) { $edd_log = new EDD_Logging(); $log_data = array('post_parent' => $item['item_number']['recommendation_source'], 'post_date' => edd_get_payment_completed_date($payment_id), 'log_type' => 'recommendation_sale'); $log_meta = array('payment_id' => $payment_id, 'download_id' => $item['id'], 'price' => $item['price'], 'quantity' => $item['quantity'], 'item_price' => $item['item_price']); $log_entry = $edd_log->insert_log($log_data, $log_meta); } } }
/** * Complete a purchase * * Performs all necessary actions to complete a purchase. * Triggered by the edd_update_payment_status() function. * * @since 1.0.8.3 * @param int $payment_id the ID number of the payment * @param string $new_status the status of the payment, probably "publish" * @param string $old_status the status of the payment prior to being marked as "complete", probably "pending" * @return void */ function edd_complete_purchase($payment_id, $new_status, $old_status) { if ($old_status == 'publish' || $old_status == 'complete') { return; } // Make sure that payments are only completed once // Make sure the payment completion is only processed when new status is complete if ($new_status != 'publish' && $new_status != 'complete') { return; } $creation_date = get_post_field('post_date', $payment_id, 'raw'); $completed_date = edd_get_payment_completed_date($payment_id); $user_info = edd_get_payment_meta_user_info($payment_id); $customer_id = edd_get_payment_customer_id($payment_id); $amount = edd_get_payment_amount($payment_id); $cart_details = edd_get_payment_meta_cart_details($payment_id); do_action('edd_pre_complete_purchase', $payment_id); if (is_array($cart_details)) { // Increase purchase count and earnings foreach ($cart_details as $cart_index => $download) { // "bundle" or "default" $download_type = edd_get_download_type($download['id']); $price_id = isset($download['item_number']['options']['price_id']) ? (int) $download['item_number']['options']['price_id'] : false; // Increase earnings and fire actions once per quantity number for ($i = 0; $i < $download['quantity']; $i++) { // Ensure these actions only run once, ever if (empty($completed_date)) { edd_record_sale_in_log($download['id'], $payment_id, $price_id, $creation_date); do_action('edd_complete_download_purchase', $download['id'], $payment_id, $download_type, $download, $cart_index); } } // Increase the earnings for this download ID edd_increase_earnings($download['id'], $download['price']); edd_increase_purchase_count($download['id'], $download['quantity']); } // Clear the total earnings cache delete_transient('edd_earnings_total'); // Clear the This Month earnings (this_monththis_month is NOT a typo) delete_transient(md5('edd_earnings_this_monththis_month')); delete_transient(md5('edd_earnings_todaytoday')); } // Increase the customer's purchase stats $customer = new EDD_Customer($customer_id); $customer->increase_purchase_count(); $customer->increase_value($amount); edd_increase_total_earnings($amount); // Check for discount codes and increment their use counts if (!empty($user_info['discount']) && $user_info['discount'] !== 'none') { $discounts = array_map('trim', explode(',', $user_info['discount'])); if (!empty($discounts)) { foreach ($discounts as $code) { edd_increase_discount_usage($code); } } } // Ensure this action only runs once ever if (empty($completed_date)) { // Save the completed date edd_update_payment_meta($payment_id, '_edd_completed_date', current_time('mysql')); do_action('edd_complete_purchase', $payment_id); } // Empty the shopping cart edd_empty_cart(); }