class-affiliates-wc-method.php 10.2 KB
<?php

class Affiliates_WC_Method {

    public static function init() {
        if (method_exists('Affiliates_Referral', 'register_referral_amount_method')) {
            Affiliates_Referral::register_referral_amount_method(array(__CLASS__, 'product_rates'));
            Affiliates_Referral::register_referral_amount_method(array(__CLASS__, 'product_fixed_rates'));
        }
    }

    public static function product_rates($IXAPWO0, $IXAPWO1) {
        $IXAPWO2 = '0';
        if (isset($IXAPWO1['post_id'])) {
            $IXAPWO2 = self::calculate(intval($IXAPWO1['post_id']), $IXAPWO0, $IXAPWO1);
        } else if (isset($IXAPWO1['base_amount'])) {
            $IXAPWO3 = get_option(Affiliates_WooCommerce_Integration::PLUGIN_OPTIONS, array());
            $IXAPWO4 = isset($IXAPWO3[Affiliates_WooCommerce_Integration::IXAPWO5]) ? $IXAPWO3[Affiliates_WooCommerce_Integration::IXAPWO5] : 0;
            $IXAPWO2 = bcmul($IXAPWO1['base_amount'], $IXAPWO4, AFFILIATES_REFERRAL_AMOUNT_DECIMALS);
        } else if (isset($IXAPWO1['amount'])) {
            $IXAPWO2 = $IXAPWO1['amount'];
        } return $IXAPWO2;
    }

    public static function product_fixed_rates($IXAPWO0, $IXAPWO1) {
        return self::check_product_apply_rates(intval($IXAPWO1['post_id']), $IXAPWO0, $IXAPWO1);
    }

    public static function check_product_apply_rates($IXAPWO6, $IXAPWO0 = null, $IXAPWO1 = null) {
        $IXAPWO7 = '0';
        if ($IXAPWO8 = Affiliates_WooCommerce_Integration::get_order($IXAPWO6)) {
            // Check is first order
            $customer_orders = get_posts(array(
                'numberposts' => -1,
                'meta_key' => '_customer_user',
                'meta_value' => get_current_user_id(),
                'post_type' => wc_get_order_types(),
                'post_status' => array_keys(wc_get_order_statuses()),
            ));
            if (count($customer_orders) == 1) {
                $IXAPWO9 = $IXAPWO8->get_items();
                foreach ($IXAPWO9 as $IXAPWO10 => $IXAPWO11) {
                    $IXAPWO12 = $IXAPWO8->get_product_from_item($IXAPWO11);
                    if ($IXAPWO12->exists()) {
                        $IXAPWO13 = $IXAPWO12->id;
                        $IXAPWO14 = get_post_meta($IXAPWO13, '_affiliates_rate', true);
                        if (strlen((string) $IXAPWO14) == 0) {
                            $IXAPWO14 = $IXAPWO4;
                        } $IXAPWO14 = apply_filters('affiliates_wc_method_product_rate', $IXAPWO14, $IXAPWO13, $IXAPWO0, $IXAPWO1);
                        if (strlen((string) $IXAPWO14) > 0) {
                            $IXAPWO15 = $IXAPWO8->get_line_total($IXAPWO11);
                            if ($IXAPWO15 > 0) {
                                $IXAPWO7 = $IXAPWO14;
                            }
                        }
                    }
                }
            }
        }
        return $IXAPWO7;
    }

    public static function calculate($IXAPWO6, $IXAPWO0 = null, $IXAPWO1 = null) {
        $IXAPWO7 = '0';
        if ($IXAPWO8 = Affiliates_WooCommerce_Integration::get_order($IXAPWO6)) {
            $IXAPWO9 = $IXAPWO8->get_items();
            $IXAPWO3 = get_option(Affiliates_WooCommerce_Integration::PLUGIN_OPTIONS, array());
            $IXAPWO4 = isset($IXAPWO3[Affiliates_WooCommerce_Integration::IXAPWO5]) ? $IXAPWO3[Affiliates_WooCommerce_Integration::IXAPWO5] : 0;
            foreach ($IXAPWO9 as $IXAPWO10 => $IXAPWO11) {
                $IXAPWO12 = $IXAPWO8->get_product_from_item($IXAPWO11);
                if ($IXAPWO12->exists()) {
                    $IXAPWO13 = $IXAPWO12->id;
                    $IXAPWO14 = get_post_meta($IXAPWO13, '_affiliates_rate', true);
                    if (strlen((string) $IXAPWO14) == 0) {
                        $IXAPWO14 = $IXAPWO4;
                    } $IXAPWO14 = apply_filters('affiliates_wc_method_product_rate', $IXAPWO14, $IXAPWO13, $IXAPWO0, $IXAPWO1);
                    if (strlen((string) $IXAPWO14) > 0) {
                        $IXAPWO15 = $IXAPWO8->get_line_total($IXAPWO11);
                        if ($IXAPWO15 > 0) {
                            $IXAPWO16 = bcmul($IXAPWO15, $IXAPWO14, AFFILIATES_REFERRAL_AMOUNT_DECIMALS);
                            $IXAPWO7 = bcadd($IXAPWO7, $IXAPWO16, AFFILIATES_REFERRAL_AMOUNT_DECIMALS);
                        }
                    }
                }
            }
        } return $IXAPWO7;
    }

    public static function get_discount_after_tax_for_order_item(&$IXAPWO8, &$IXAPWO11) {
        $IXAPWO17 = 0;
        $IXAPWO18 = $IXAPWO8->get_used_coupons();
        $IXAPWO12 = $IXAPWO8->get_product_from_item($IXAPWO11);
        foreach ($IXAPWO18 as $IXAPWO19) {
            $IXAPWO20 = new WC_Coupon($IXAPWO19);
            if (!$IXAPWO20->apply_before_tax()) {
                $IXAPWO21 = wp_get_post_terms($IXAPWO11['product_id'], 'product_cat', array("fields" => "ids"));
                $IXAPWO22 = woocommerce_get_product_ids_on_sale();
                $this_item_is_discounted = false;
                if (sizeof($IXAPWO20->product_ids) > 0) {
                    if (in_array($IXAPWO11['product_id'], $IXAPWO20->product_ids) || in_array($IXAPWO11['variation_id'], $IXAPWO20->product_ids) || in_array($IXAPWO12->get_parent(), $IXAPWO20->product_ids))
                        $this_item_is_discounted = true;
                } elseif (sizeof($IXAPWO20->product_categories) > 0) {
                    if (sizeof(array_intersect($IXAPWO21, $IXAPWO20->product_categories)) > 0)
                        $this_item_is_discounted = true;
                } else {
                    $this_item_is_discounted = true;
                } if (sizeof($IXAPWO20->exclude_product_ids) > 0) {
                    if (in_array($IXAPWO11['product_id'], $IXAPWO20->exclude_product_ids) || in_array($IXAPWO11['variation_id'], $IXAPWO20->exclude_product_ids) || in_array($IXAPWO12->get_parent(), $IXAPWO20->exclude_product_ids)) {
                        $this_item_is_discounted = false;
                    }
                } if (sizeof($IXAPWO20->exclude_product_categories) > 0) {
                    if (sizeof(array_intersect($IXAPWO21, $IXAPWO20->exclude_product_categories)) > 0) {
                        $this_item_is_discounted = false;
                    }
                } if ($IXAPWO20->exclude_sale_items == 'yes') {
                    if (in_array($IXAPWO11['product_id'], $IXAPWO22, true) || in_array($IXAPWO11['variation_id'], $IXAPWO22, true) || in_array($IXAPWO12->get_parent(), $IXAPWO22, true)) {
                        $this_item_is_discounted = false;
                    }
                } $IXAPWO23 = $IXAPWO11;
                $IXAPWO23['data'] = $IXAPWO12;
                $IXAPWO23['quantity'] = $IXAPWO11['qty'];
                $this_item_is_discounted = apply_filters('woocommerce_item_is_discounted', $this_item_is_discounted, $IXAPWO23, $IXAPWO24 = false, $IXAPWO20);
                if ($this_item_is_discounted) {
                    $IXAPWO25 = $IXAPWO8->get_item_total($IXAPWO11, true);
                    if ($IXAPWO20->type == 'fixed_product') {
                        if ($IXAPWO25 < $IXAPWO20->amount) {
                            $IXAPWO26 = $IXAPWO25;
                        } else {
                            $IXAPWO26 = $IXAPWO20->amount;
                        } $IXAPWO17 += $IXAPWO26 * $IXAPWO11['qty'];
                    } else if ($IXAPWO20->type == 'percent_product') {
                        $IXAPWO27 = (int) get_option('woocommerce_price_num_decimals');
                        $IXAPWO17 += round(( $IXAPWO25 / 100 ) * $IXAPWO20->amount, $IXAPWO27) * $IXAPWO11['qty'];
                    }
                }
            }
        } return $IXAPWO17;
    }

    public static function get_net_item_totals($IXAPWO6, $IXAPWO28 = true) {
        $IXAPWO2 = null;
        if ($IXAPWO6 && class_exists('WC_Order')) {
            $IXAPWO8 = new WC_Order();
            if ($IXAPWO8->get_order($IXAPWO6)) {
                if (method_exists('WC_Order', 'get_total_discount')) {
                    $IXAPWO29 = $IXAPWO8->get_total_discount();
                } else {
                    $IXAPWO29 = $IXAPWO8->get_order_discount();
                } $IXAPWO30 = 0;
                $IXAPWO31 = array();
                $IXAPWO32 = 0;
                foreach ($IXAPWO8->get_items() as $IXAPWO10 => $IXAPWO11) {
                    $this_item_total = $IXAPWO8->get_item_total($IXAPWO11, false, false);
                    $this_item_total_inc_tax = $IXAPWO8->get_item_total($IXAPWO11, true, false);
                    $IXAPWO30 += $this_item_total_inc_tax;
                    $IXAPWO33 = 0;
                    if ($this_item_total > 0) {
                        $IXAPWO17 = self::get_discount_after_tax_for_order_item($IXAPWO8, $IXAPWO11);
                        if ($IXAPWO17 > 0) {
                            $IXAPWO32 += $IXAPWO17;
                            $IXAPWO34 = $this_item_total_inc_tax / $this_item_total - 1;
                            if ($IXAPWO34 > 0) {
                                $IXAPWO33 = $IXAPWO17 / ( 1 + $IXAPWO34 );
                            }
                        }
                    } $IXAPWO35 = $this_item_total * $IXAPWO11['qty'] - $IXAPWO33;
                    if ($IXAPWO35 < 0) {
                        $IXAPWO35 = 0;
                    } $IXAPWO31[$IXAPWO10] = $IXAPWO35;
                } $IXAPWO36 = $IXAPWO29 - $IXAPWO32;
                if ($IXAPWO28) {
                    $IXAPWO37 = array();
                    foreach ($IXAPWO8->get_items() as $IXAPWO10 => $IXAPWO11) {
                        $this_item_total = $IXAPWO8->get_item_total($IXAPWO11, false, false);
                        $this_item_total_inc_tax = $IXAPWO8->get_item_total($IXAPWO11, true, false);
                        $this_prorated_discount = 0;
                        if ($IXAPWO30 > 0) {
                            $this_prorated_discount = $this_item_total * $IXAPWO36 / $IXAPWO30;
                        } $IXAPWO37[$IXAPWO10] = $this_prorated_discount;
                    } $IXAPWO38 = count($IXAPWO31);
                    foreach ($IXAPWO8->get_items() as $IXAPWO10 => $IXAPWO11) {
                        $IXAPWO31[$IXAPWO10] -= $IXAPWO37[$IXAPWO10];
                    }
                } $IXAPWO2 = $IXAPWO31;
            }
        } return $IXAPWO2;
    }

}

Affiliates_WC_Method::init();