/** * Records a pending referral when a pending payment is created * * @access public * @since 1.0 */ public function add_pending_referral($transaction_id = 0) { // get transaction object $this->transaction = apply_filters('affwp_get_it_exchange_transaction', get_post_meta($transaction_id, '_it_exchange_cart_object', true)); // get affiliate ID from the transaction object if set $affiliate_id = isset($this->transaction->affwp_affiliate_id) ? $this->transaction->affwp_affiliate_id : $this->get_affiliate_id($transaction_id); // set affiliate ID to that of tracked affiliate coupon (if any) if ($this->transaction->coupons && is_array($this->transaction->coupons)) { if (!empty($this->transaction->coupons['cart'])) { foreach ($this->transaction->coupons['cart'] as $coupon) { $affiliate_id = get_post_meta($coupon['id'], 'affwp_coupon_affiliate', true); if (!$affiliate_id) { continue; } if (!affiliate_wp()->tracking->is_valid_affiliate($affiliate_id)) { continue; } break; } } } if ($affiliate_id) { $guest_checkout_email = it_exchange_get_transaction_customer_email($transaction_id); $email = isset($guest_checkout_email) ? $guest_checkout_email : $this->transaction->shipping_address['email']; if ($this->is_affiliate_email($email, $affiliate_id)) { return; // Customers cannot refer themselves } $sub_total = 0; $total = floatval($this->transaction->total); $total_taxes = floatval($this->transaction->taxes_raw); $shipping = floatval($this->transaction->shipping_total); foreach ($this->transaction->products as $product) { $sub_total += $product['product_subtotal']; } $referral_total = $total; if (affiliate_wp()->settings->get('exclude_tax')) { $referral_total -= $total_taxes; } if (affiliate_wp()->settings->get('exclude_shipping') && $shipping > 0) { $referral_total -= $shipping / 100; } $amount = 0; foreach ($this->transaction->products as $product) { if (get_post_meta($product['product_id'], "_affwp_{$this->context}_referrals_disabled", true)) { continue; } $product_percent_of_cart = (double) $product['product_subtotal'] / $sub_total; $referral_product_price = (double) $product_percent_of_cart * (double) $referral_total; $amount += $this->calculate_referral_amount($referral_product_price, $transaction_id, $product['product_id'], $affiliate_id); } $this->insert_pending_referral($amount, $transaction_id, $this->transaction->description, $this->get_products($transaction_id), array('affiliate_id' => $affiliate_id)); if (it_exchange_transaction_is_cleared_for_delivery($transaction_id)) { $this->complete_referral($transaction_id); } } }
/** * Get the recipient for this notification. * * @since 1.0 * * @return \WP_User */ public function get_recipient() { $email = it_exchange_get_transaction_customer_email($this->transaction); $user = it_exchange_guest_checkout_generate_guest_user_object($email); $billing = (array) it_exchange_get_transaction_billing_address($this->transaction); $shipping = (array) it_exchange_get_transaction_shipping_address($this->transaction); $address = wp_parse_args($billing, $shipping); $user->first_name = !empty($address['first-name']) ? $address['first-name'] : ''; $user->last_name = !empty($address['last-name']) ? $address['last-name'] : ''; return $user; }