Free VAT, and free shipping for specific category and specific payment methods in WooCommerce

Question

In Woocommerce I have enable TAX and shipping. I would like, If visitor have two specific products in the cart (from product category term Ids 234 and 987) and purchase using specific online payment methods, to set free TAX and free shipping.

The shipping method is like:

enter image description here

and inner it like:

enter image description here

Here is my code attempt for now:

// Custom conditional function that check for product categories and payment methods
function check_conditions(){
    // Settings
    $payment_ids = array('hyperpay_mada', 'hyperpay', 'hyperpay_stcpay'); // Payment methods
    $category_1  = array( 746,735 ); // Product category 1
    $taxonomy    = 'product_cat'; // Taxonomy for product category

    // Initializing
    $found_1 = false;

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        // Only on checkout
        if( is_checkout() && ! is_wc_endpoint_url() ) {
            // First category
            if ( has_term( $category_1, $taxonomy, $cart_item['product_id'] ) ) {
                $found_1 = true;
            }
        }
    }
    $payment_condition = in_array(WC()->session->get('chosen_payment_method'), $payment_ids);

    // When both categories are in cart with specific payment method, return true or if not return false
    return $found_1 && $payment_condition ? true : false;
}

// Change all products tax class to "zero rate"
add_filter( 'woocommerce_product_get_tax_class', 'wc_free_tax_rate', 100, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_free_tax_rate', 100, 2 );
function wc_free_tax_rate( $tax_class, $product ) {
    // If both products are in cart, the "Zero Tax" rate is applied
    if ( check_conditions() ) {
        return 'Zero Rate';
    } else {
        return 'Standard';
    }
    return $tax_class;
}

// Set the "Flat rate" shipping method cost to zero
add_filter( 'woocommerce_package_rates', 'flat_rate_cost_to_zero', 100, 2 );
function flat_rate_cost_to_zero( $rates, $package ) {
    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // Set the cost to zero
        if ( check_conditions() ) {
            $rates[$rate_key]->cost = 0;
            $rates[$rate_key]->taxes = array_map( function($a){ return 0; }, $rate->taxes );
            $rates[$rate_key]->label .= ' ' . __('مجانا');
        }
    }
    return $rates;
}

// Refresh checkout on payment method select/change
add_action( 'wp_footer', 'refresh_checkout_for_payment_methods' );
function refresh_checkout_for_payment_methods(){
    // Only on checkout page
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    (function($){
        $('form.checkout').on('change', 'input[name^="payment_method"]', function() {
            $(document.body).trigger("update_checkout");
        });
    })(jQuery);
    </script>
    <?php
    endif;
}
0
, khalid almallahi 3 years 2020-07-12T03:10:20-05:00 0 Answers 71 views 0

Leave an answer

Browse
Browse