Search code examples
phpwordpresswoocommercepluginspayment-gateway

Calculate before shipping - Woocommerce


i need help...i cant make this to calculate before shipping cost..i want to give 5% off on products in checkout on specific payment gateway (bacs)..but i want to calculate it before shipping cost..when 5% off is applied to products then calculate shipping cost...

for example..

  1. My free shipping is when customer buys over 250$
  2. Lets say we have products in cart about 257$
  3. then get 5% off on bacs payment gateway and that is 244$
  4. Then shipping will not be free (only when cart subtotal(without shipping cost) with 5% off on payment gateway is over than 250$ then apply free shipping)

Code:

add_action( 'woocommerce_cart_calculate_fees','popust_za_avansno_placanje', 20, 1 );
function popust_za_avansno_placanje( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Provjera ID-a vrste plaćanja
    $payment_method = 'bacs';

    // Podešavanje vrijednosti popusta
    $percent = 5; // 5%

    //$cart_total = $cart_object->subtotal_ex_tax + $cart_object->shipping_total;
    $cart_total = $cart_object->subtotal_ex_tax;
    $chosen_payment_method = WC()->session->get('chosen_payment_method');

    if( $payment_method == $chosen_payment_method ){
        $label_text = __( "Popust za Avansno plaćanje 5%" );
        // Kalkulacija
        $discount = number_format(($cart_total / 100) * $percent, 2);
        // Dodaj popust
        $cart_object->add_fee( $label_text, -$discount, false );
    }
}

add_action( 'woocommerce_review_order_before_payment', 'osvjezi_metodu_placanja' );
function osvjezi_metodu_placanja(){
    // jQuery kod
    ?>
    <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
            });
        })(jQuery);
    </script>
    <?php
}

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
    $session_data = WC()->session->get_session_data();
    $cart_totals = maybe_unserialize( $session_data['cart_totals'] );
    $payment_method = 'bacs';
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    $sub_total_after_item_discount = $cart_totals['subtotal'] + $cart_totals['fee_total'];
    
    if( $payment_method == $chosen_payment_method && $sub_total_after_item_discount < 250){
        $free_shipping_id = 'free_shipping:2';
        unset($packages[0]['rates'][ $free_shipping_id ]);
    }
    return $packages;
} );

Solution

  • Remove free shipping option if total is under 250

    add_filter( 'woocommerce_shipping_packages', function( $packages ) {
        $session_data = WC()->session->get_session_data();
        $cart_totals = maybe_unserialize( $session_data['cart_totals'] );
        $payment_method = 'bacs';
        $chosen_payment_method = WC()->session->get('chosen_payment_method');
        $sub_total_after_item_discount = $cart_totals['subtotal'] + $cart_totals['fee_total'];
        
        if( $payment_method == $chosen_payment_method && $sub_total_after_item_discount < 250){
            //Where free_shipping_id is instance id from shipping settings e.g 4
            $free_shipping_id = 'free_shipping:4';
            unset($packages[0]['rates'][ $free_shipping_id ]);
        }
        return $packages;
    } );