• Resolved coopersita

    (@coopersita)


    I’m using a plugin that adds custom fields for products and you can add prices to each field. The problem is that the plugin bundles the prices of the addons and the regular price of the items, and the item itself has one tax class, and the addons have others.

    I was thinking of setting the product as tax-free (Zero-rate), and adding the taxes as needed on the cart/checkout. I could add them like fees, except the store requires that prices are shown inclusive of taxes, so I need to add them as taxes so they get added to the price.

    In this example code, I’m adding a GST tax to the base cost of the product, but how do I add it as taxes?

    
    function change_tax_class_for_deposits( $cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ){
    
            if( $cart_item['is_ph_deposit'] == "1") { // I will eventually have other checks to apply different taxes
                $_tax = new WC_Tax();
                $taxes =  $_tax->get_rates();
                $GST = 0;
                foreach($taxes as $tax) {
                    if($tax['label'] === "GST"){
                        $GST = $tax['rate'] / 100;
                    }
                }
                $fee =  $cart_item['price_without_addon_price'] * $GST;
    
                $cart_item['data']->? // What do I put here, so the fee gets added as a tax?
            }
        }
    

    PS: I can’t use grouped products because I’m using another plugin to create custom products that don’t work as part of a group.

    • This topic was modified 3 years, 1 month ago by coopersita. Reason: formatting code
Viewing 3 replies - 1 through 3 (of 3 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @coopersita,

    There isn’t a built-in way to do that with WooCommerce. In this case, you can use a customized solution or a plugin that I’m not aware of at the moment. You can have a look at our extension page and check if there is an extension that can help with that.

    If you require assistance with custom development tasks you could reach out to some of the official WooCommerce development partners via this link: https://woocommerce.com/customizations/.

    That said, I’m going to leave this thread open for a bit in case anyone else wants to chime in to help you out.

    Thanks.

    Thread Starter coopersita

    (@coopersita)

    Thanks. I think I will look into breaking the price into parts, and add the part with tax as a fee.

    Mirko P.

    (@rainfallnixfig)

    That sounds like a plan. Thank you for sharing with us what you would do in this situation.

    If you have any further questions please do let us know.

    Cheers.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add taxes dynamically to a portion of the price’ is closed to new replies.