WC_Tax::get_shipping_tax_class_from_cart_items
Get the shipping tax class from the cart items.
Determines the appropriate tax class for shipping based on cart contents. Standard tax class takes priority, followed by the first non-standard class found in the configured tax class hierarchy.
Method of the class: WC_Tax{}
No Hooks.
Returns
String|null. The shipping tax class slug, or null if no taxable items are found.
Usage
$result = WC_Tax::get_shipping_tax_class_from_cart_items();
WC_Tax::get_shipping_tax_class_from_cart_items() WC Tax::get shipping tax class from cart items code WC 10.3.6
private static function get_shipping_tax_class_from_cart_items() {
$standard_tax_class = '';
$cart = WC()->cart;
// Check if cart has items before proceeding.
if ( ! $cart->get_cart() ) {
return $standard_tax_class;
}
$cart_tax_classes = $cart->get_cart_item_tax_classes_for_shipping();
// No tax classes = no taxable items.
if ( empty( $cart_tax_classes ) ) {
return null;
}
// Standard tax class takes priority over any other tax class.
if ( in_array( $standard_tax_class, $cart_tax_classes, true ) ) {
return $standard_tax_class;
}
// If only one tax class, use it directly.
if ( 1 === count( $cart_tax_classes ) ) {
return reset( $cart_tax_classes );
}
// For multiple classes, use the first one found using the order defined in settings.
static $tax_class_slugs = null;
if ( null === $tax_class_slugs ) {
$tax_class_slugs = self::get_tax_class_slugs();
}
foreach ( $tax_class_slugs as $tax_class_slug ) {
if ( in_array( $tax_class_slug, $cart_tax_classes, true ) ) {
return $tax_class_slug;
}
}
// Default to standard tax class if nothing else matches.
return $standard_tax_class;
}