recurring-totals.php 13.9 KB
<?php
/**
 * Recurring totals
 *
 * @author 		Prospress
 * @package 	WooCommerce Subscriptions/Templates
 * @version     2.0
 */
if (!defined('ABSPATH')) {
    exit;
}
global $wpdb;

$display_th = true;
$isUpgrade = false;
$cart_content = WC()->cart->cart_contents;

if (count($cart_content) > 0) {
    $shipping_cost = 0;
    $upgrade_total = 0;
    $number_drink = 0;
    foreach ($recurring_carts as $_key => $_value) {
        $upgrade_total = $_value->total - $_value->shipping_total;
    }
    foreach ($cart_content as $key => $value) {
        if (isset($value['subscription_switch'])) {
//            var_dump($value['variation_id']);
            $number_drink = $value['variation']['attribute_drinks'];
            $isUpgrade = true;
            $weight = get_post_meta($value['variation_id'], '_weight')[0];
            if ($weight == '') {
                $weight = get_post_meta($value['product_id'], '_weight')[0];
            }
            $weight = (float) $weight;
            $result_shipping_zones = get_option('be_woocommerce_shipping_zones');

            $shipping_country = $_POST['s_country'];

            if (count($result_shipping_zones)) {
                foreach ($result_shipping_zones as $value) {
                    if ($value['zone_country'] == $shipping_country) {
                        $zone_id = $value['zone_id'];
                    }
                }
            }
            if (isset($zone_id)) {

                $result_shipping_rates_table = get_option('woocommerce_table_rates');
//                            var_dump($result_shipping_rates_table);
                foreach ($result_shipping_rates_table as $value) {
                    if ($value['zone'] == $zone_id && $value['cond'] == 'weight') {
                        if ($weight >= $value['min'] && $weight <= $value['max']) {
                            $shipping_cost = $value['cost'];
//                                        $shipping_label = $value['title'];
                        }
                    }
                }
                $shipping_label = $woocommerce->countries->countries[$shipping_country];
            }
        }
    }
}
$upgrade_total = $upgrade_total + $shipping_cost;
?>
<?php
if ($isUpgrade) {
    ?>
    <tr class="shipping shipping-upgrade text-uppercase robotoregular">
        <th class="font18">SHIPPING</th>
        <td class="font18 text-right fontbold">
            <?php
            if ($shipping_cost == 0) {
                echo "Free";
            } else {
                echo wc_price($shipping_cost);
            }
            //echo wp_kses_post(wcs_cart_totals_shipping_method($shipping_method, $recurring_carts[$recurring_cart_key]));
            ?>
        </td>
    </tr>

    <script>
        jQuery(document).ready(function ($) {
            $('.shipping-upgrade').insertBefore($('.order-total.tvhide '));
            $('.order-total.tvhide .amount').html('$<?php echo number_format($upgrade_total, 2) ?>');
            if ($('.message_upgrade').length > 0) {
                $('.message_upgrade').html('Fill out details below and press checkout.<br/>The monthly payment for your new subscription of <?php echo $number_drink; ?> drinks will now be $<?php echo number_format($upgrade_total, 2) ?> - deducted on your regular billing date.');
            }
            else {
                $('.woocommerce-checkout-payment .place-order').append('<span class="message_upgrade">Fill out details below and press checkout.<br/>The monthly payment for your new subscription of <?php echo $number_drink; ?> drinks will now be $<?php echo number_format($upgrade_total, 2) ?> - deducted on your regular billing date.</span>');
            }
        });
    </script>
    <?php
}
?>
<tr class="recurring-totals tvhide">
    <th colspan="2"><?php esc_html_e('Recurring Totals', 'woocommerce-subscriptions'); ?></th>
</tr>

<?php if (WC()->cart->needs_shipping() && WC()->cart->show_shipping() && !empty($shipping_methods)) : ?>

    <?php
    $ii = 0;
    foreach ($shipping_methods as $recurring_cart_key => $shipping_method) :
        if ($recurring_cart_key != 'table_rate_shipping') {
            continue;
        }
        //if ($ii == 0) {
        ?>
        <tr class="shipping recurring-total text-uppercase robotoregular <?php echo $ii; ?> shippingwcs">
            <td class="desktop carthide">&nbsp;</td>
            <?php if ($display_th) : $display_th = false; ?>
                <td class="font18" rowspan="<?php //echo esc_attr(count($shipping_methods));                                                                                                                                                                                                                                                                                          ?>"><?php echo esc_html(sprintf(__('Shipping', 'woocommerce-subscriptions'), $shipping_method->label)); ?></td>
            <?php else: ?>
                <th class="font18">&nbsp;</th>
            <?php endif; ?>
            <td class="font18 text-right fontbold">
                <?php
                if ($shipping_method->cost == 0) {
                    echo "Free";
                } else {
                    echo wc_price($shipping_method->cost);
                }
                //echo wp_kses_post(wcs_cart_totals_shipping_method($shipping_method, $recurring_carts[$recurring_cart_key]));
                ?>
            </td>
        </tr>
        <?php
        //}
        $ii++;
    endforeach;
    ?>
    <?php $display_th = true; ?>

<?php endif; ?>

<?php
$jj = 0;
foreach ($recurring_carts as $recurring_cart_key => $recurring_cart) :
    //if ($jj == 0) {
    ?>
    <?php
    if (0 == $recurring_cart->next_payment_date) :
        ?>
        <?php continue; ?>
    <?php endif; ?>
    <tr class="cart-subtotal recurring-total font18 hide<?php
    echo $jj > 0 ? ' nobordertop' : '';
    echo (count($recurring_carts) > 1 && $jj == 0) ? ' noborderbottom' : '';
    ?>">
        <td class="desktop carthide">&nbsp;</td>
        <?php if ($display_th) : $display_th = false; ?>
            <th class="text-uppercase" rowspan="<?php //echo esc_attr($carts_with_multiple_payments);                                                                                                                                                                                                                                                                                    ?>"><?php esc_html_e('Total per month', 'woocommerce-subscriptions'); ?></th>
        <?php else: ?>
            <th class="font18">&nbsp;</th>
        <?php endif; ?>
    <!--<td><?php //wcs_cart_totals_subtotal_html($recurring_cart);                                                                                                                                                                                                                                                                                           ?></td>-->
        <td class="text-right fontbold"><?php echo $recurring_cart->get_cart_subtotal(); ?></td>
    </tr>
    <?php
    //}
    $jj++;
endforeach;
if (count($recurring_carts) > 1) {
    ?>
    <style type="text/css">
        #order_reviews .shop_table {
            padding-bottom: 135px;
        }
    </style>
    <?php
}
$display_th = true;
?>

<?php foreach (WC()->cart->get_coupons() as $code => $coupon) : ?>
    <?php foreach ($recurring_carts as $recurring_cart_key => $recurring_cart) : ?>
        <?php if (0 == $recurring_cart->next_payment_date) : ?>
            <?php continue; ?>
        <?php endif; ?>
        <?php foreach ($recurring_cart->get_coupons() as $recurring_code => $recurring_coupon) : ?>
            <?php
            if ($recurring_code !== $code) {
                continue;
            }
            ?>
            <tr class="cart-discount coupon-<?php echo esc_attr($code); ?> recurring-total">
                <?php
                if ($display_th) : $display_th = false;
                    if (!session_id())
                        @session_start();
                    $session_coupon_code = isset($_SESSION['data_coupon']['coupon_code']) ? $_SESSION['data_coupon']['coupon_code'] : '';
                    ?>
                    <th rowspan="<?php echo esc_attr($carts_with_multiple_payments); ?>">Gifd Card: <?php echo (in_array($code, array('firstreferee', 'firstreferee407', 'firstreferee408', 'firstreferee409')) || $code == strtolower($session_coupon_code)) ? '' : $code; ?></th>
                <?php endif; ?>
                <td><?php wcs_cart_totals_coupon_html($recurring_coupon, $recurring_cart); ?></td>
            </tr>
        <?php endforeach; ?>
    <?php endforeach; ?>
    <?php $display_th = true; ?>
<?php endforeach; ?>


<?php if (WC()->cart->tax_display_cart === 'excl') : ?>
    <?php if (get_option('woocommerce_tax_total_display') === 'itemized') : ?>

        <?php foreach (WC()->cart->get_tax_totals() as $code => $tax) : ?>
            <?php foreach ($recurring_carts as $recurring_cart_key => $recurring_cart) : ?>
                <?php if (0 == $recurring_cart->next_payment_date) : ?>
                    <?php continue; ?>
                <?php endif; ?>
                <?php foreach ($recurring_cart->get_tax_totals() as $recurring_code => $recurring_tax) : ?>
                    <?php
                    if ($recurring_code !== $code) {
                        continue;
                    }
                    ?>
                    <tr class="tax-rate tax-rate-<?php echo esc_attr(sanitize_title($code)); ?> recurring-total">
                        <?php if ($display_th) : $display_th = false; ?>
                            <th><?php echo esc_html($recurring_tax->label); ?></th>
                        <?php else : ?>
                            <th></th>
                        <?php endif; ?>
                        <td><?php echo wp_kses_post(wcs_cart_price_string($recurring_tax->formatted_amount, $recurring_cart)); ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php endforeach; ?>
            <?php $display_th = true; ?>
        <?php endforeach; ?>

    <?php else : ?>

        <?php foreach ($recurring_carts as $recurring_cart_key => $recurring_cart) : ?>
            <?php if (0 == $recurring_cart->next_payment_date) : ?>
                <?php continue; ?>
            <?php endif; ?>
            <tr class="tax-total recurring-total">
                <?php if ($display_th) : $display_th = false; ?>
                    <th><?php echo esc_html(WC()->countries->tax_or_vat()); ?></th>
                <?php else : ?>
                    <th></th>
                <?php endif; ?>
                <td><?php echo wp_kses_post(wcs_cart_price_string($recurring_cart->get_taxes_total(), $recurring_cart)); ?></td>
            </tr>
        <?php endforeach; ?>
        <?php $display_th = true; ?>
    <?php endif; ?>
<?php endif; ?>

<?php foreach ($recurring_carts as $recurring_cart_key => $recurring_cart) : ?>
    <?php if (0 == $recurring_cart->next_payment_date) : continue; ?>
    <?php endif; ?>
    <tr class="order-total recurring-total">
        <td class="desktop carthide">&nbsp;</td>
        <?php if ($display_th) : $display_th = false; ?>
            <th class="text-uppercase" rowspan="<?php //echo esc_attr($carts_with_multiple_payments);                                                                                                                                                                                                                                                                       ?>"><?php esc_html_e('Total per month', 'woocommerce-subscriptions'); ?></th>
        <?php endif; ?>
        <td class="text-right fontbold">
            <?php // wcs_cart_totals_order_total_html($recurring_cart);                   ?>
            <?php echo $recurring_cart->get_total(); ?>
        </td>
    </tr>
<?php endforeach; ?>



<script>
    jQuery(document).ready(function ($) {

<?php if (!$isUpgrade) { ?>
            $shippingwc = getAmountSubscription('.shippingwc .amount');
            $total = getAmountSubscription('.product-total .amount');
            $discount = getAmountSubscription('.cart-discount  .amount');
            if ($('.order-total .amount').length > 0) {
                if ($('.cart-before-discount').length > 0) {
                    $('.cart-before-discount .amount').html('$' + ($total + $shippingwc).toFixed(2));
                    $('.order-total th').html('First Month Payment:');
                }
                $('.order-total .amount').html('$' + ($total - $discount + $shippingwc).toFixed(2));
            }

    <?php
}
?>
        $('.amount').each(function () {
            current_amount = $(this).html();
            if (current_amount.indexOf('AUD') === -1 && current_amount.indexOf('$') !== -1) {
                $(this).html($(this).html() + ' AUD');
            }
        });
<?php if (isset($shipping_country) && $shipping_country != '' && $shipping_country != 'AUD') { ?>
            //            if ($('.link_term_different_currency').length === 0) {
            //                $('input[name="woocommerce_checkout_place_order"]').closest('div').append('<br/><span class="link_term_different_currency">If you are paying in a different currency from AUD, <a href="#">Click Here</a> to read our Terms and Conditions</span>');
            //            }
            //            else {
            //                $('input[name="woocommerce_checkout_place_order"]').closest('div').find('.link_term_different_currency').remove();
            //                $('input[name="woocommerce_checkout_place_order"]').closest('div').find('br').remove();
            //            }

<?php } ?>
        if ($('.cart-discount').length > 0) {
            $('.add_gift_card').html('Gift Card? Code already applied.');
        }

    });
    function getAmountSubscription(element_class) {
        if (jQuery(element_class).length > 0) {
            return parseFloat(jQuery(element_class).html().replace('$', '').replace('AUD', ''));
        }
        else {
            return 0;
        }
    }
</script>