0

In Woocommerce, I wanted to complete a certain task, that being that I wanted to charge users a small fee when they select COD and flat_rate shipping or when they are eligible for free_shipping.

The store I am working on also offers local_pickup.

Now, this being said, when I have implemented this type of code in order to attain my goal - which I almost did - I stumble upon this certain issue:

The local_pickup, that I have not included within the array of elements that should be taxed additional(such as flat_rate & free_shipping) receives this certain additional fee, even thought, as I've mentioned I did not include it within the array.

This is the code that I've used. Do you guys have any idea about what could be going wrong?

// Taxa 6 lei + la plata numerar doar pentru livrare
add_action( 'woocommerce_cart_calculate_fees', 'add_cod_fee', 20, 1 );
function add_cod_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ## ------ Setari ID's ------ ##
    $your_payment_id      = 'cod'; // The payment method
    $your_shipping_method = "flat_rate:3" || "free_shipping:5"; // The shipping method
    $fee_amount           = 6; // The fee amount
    ## ----------------------------------- ##

    $chosen_payment_method_id  = WC()->session->get( 'chosen_payment_method' );
    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode( ':', $chosen_shipping_method_id )[0];

    if ( $chosen_shipping_method == $your_shipping_method 
    && $chosen_payment_method_id == $your_payment_id ) {
        $fee_text = __( "Taxă suplimentară plata la livrare, TVA inclus", "woocommerce" );
        $cart->add_fee( $fee_text, $fee_amount, false );
    }
}

// Refresh checkout 
add_action( 'wp_footer', 'refresh_checkout_script' );
function refresh_checkout_script() {
    // Doar checkout page
    if( is_checkout() && ! is_wc_endpoint_url('order-received') ) :
    ?>
    <script type="text/javascript">
    jQuery(function($){
        // Cand se schimba metoda de plata
        $('form.woocommerce-checkout').on( 'change', 'input[name="payment_method"]', function(){
            // Refresh checkout
            $('body').trigger('update_checkout');
        });
    })
    </script>
    <?php
    endif;
}

Could I perhaps exclude local_pickup somehow in php?

  • _"Do you guys have any idea about what could be going wrong?"_ - `$your_shipping_method = "flat_rate:3" || "free_shipping:5";` doesn't make this line an array, so that's where it goes wrong. - Change it to `$your_shipping_methods = array( "flat_rate:3", "free_shipping:5" );` and change `if ( $chosen_shipping_method == $your_shipping_method &&` to `if ( in_array( $chosen_shipping_method, $your_shipping_methods ) &&` – 7uc1f3r May 20 '22 at 11:50

0 Answers0