1 year ago
#182746
coopersita
How to add tax to only a portion of the price in WooCommerce
I'm using a plugin that adds custom fields for products. 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 to the 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.
woocommerce
tax
0 Answers
Your Answer