Created
August 2, 2017 19:31
-
-
Save mariovalney/b42543f83e1f7c6d8fc16996a99b3855 to your computer and use it in GitHub Desktop.
Descrição das taxas no carrinho do WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adiciona as taxas ao subtotal no carrinho | |
*/ | |
function vta_cart_item_subtotal( $wc, $cart_item, $cart_item_key ) { | |
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); | |
$subtotal = wc_get_price_including_tax( $_product, array( 'qty' => $cart_item['quantity'] ) ); | |
$subtotal = wc_price( $subtotal ); | |
if ( ! empty( $cart_item['line_subtotal_tax'] ) ) { | |
$_tax = new WC_Tax(); | |
$rates = WC_Tax::get_rates( $_product->get_tax_class() ); | |
if ( ! empty( $rates ) ) { | |
$rates_string = ''; | |
$total = count( $rates ); | |
$rates = array_values( $rates ); | |
foreach ( $rates as $key => $rate ) { | |
$tax = WC_Tax::calc_exclusive_tax( (float) $cart_item['line_subtotal'], array( $rate ) ); | |
$rates_string .= wc_price( $tax[0] ); | |
$rates_string .= ' ' . $rate['label']; | |
if ( $key < ( $total - 2 ) ) { | |
$rates_string .= ', '; | |
} else if ( $key == ( $total - 2 ) ) { | |
$rates_string .= ' e '; | |
} | |
} | |
$subtotal .= '<small class="tax_label" style="display: block;"> (inclui ' . $rates_string . ')</span>'; | |
} | |
} | |
return $subtotal; | |
}; | |
// add the filter | |
add_filter( 'woocommerce_cart_item_subtotal', 'vta_cart_item_subtotal', 10, 3 ); | |
/** | |
* Adiciona as taxas ao subtotal no carrinho | |
*/ | |
function vta_cart_item_price( $wc, $cart_item, $cart_item_key ) { | |
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); | |
return wc_price( $_product->get_price() ); | |
}; | |
// add the filter | |
add_filter( 'woocommerce_cart_item_price', 'vta_cart_item_price', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment