WC_Tax::get_shipping_tax_rates PHP Method

get_shipping_tax_rates() public static method

Gets an array of matching shipping tax rates for a given class.
public static get_shipping_tax_rates ( $tax_class = null ) : mixed
return mixed
    public static function get_shipping_tax_rates($tax_class = null)
    {
        // See if we have an explicitly set shipping tax class
        $shipping_tax_class = get_option('woocommerce_shipping_tax_class');
        if ('inherit' !== $shipping_tax_class) {
            $tax_class = $shipping_tax_class;
        }
        $location = self::get_tax_location($tax_class);
        $matched_tax_rates = array();
        if (sizeof($location) === 4) {
            list($country, $state, $postcode, $city) = $location;
            if (!is_null($tax_class)) {
                // This will be per item shipping
                $matched_tax_rates = self::find_shipping_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class));
            } else {
                // This will be per order shipping - loop through the order and find the highest tax class rate
                $cart_tax_classes = WC()->cart->get_cart_item_tax_classes();
                // If multiple classes are found, use the first one found unless a standard rate item is found. This will be the first listed in the 'additonal tax class' section.
                if (sizeof($cart_tax_classes) > 1 && !in_array('', $cart_tax_classes)) {
                    $tax_classes = self::get_tax_classes();
                    foreach ($tax_classes as $tax_class) {
                        $tax_class = sanitize_title($tax_class);
                        if (in_array($tax_class, $cart_tax_classes)) {
                            $matched_tax_rates = self::find_shipping_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class));
                            break;
                        }
                    }
                    // If a single tax class is found, use it
                } elseif (sizeof($cart_tax_classes) == 1) {
                    $matched_tax_rates = self::find_shipping_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $cart_tax_classes[0]));
                }
            }
            // Get standard rate if no taxes were found
            if (!sizeof($matched_tax_rates)) {
                $matched_tax_rates = self::find_shipping_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city));
            }
        }
        return $matched_tax_rates;
    }

Usage Example

 /**
  * Create a order.
  *
  * @since 2.4
  *
  * @return WC_Order Order object.
  */
 public static function create_order()
 {
     // Create product
     $product = WC_Helper_Product::create_simple_product();
     WC_Helper_Shipping::create_simple_flat_rate();
     $order_data = array('status' => 'pending', 'customer_id' => 1, 'customer_note' => '', 'total' => '');
     $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
     // Required, else wc_create_order throws an exception
     $order = wc_create_order($order_data);
     // Add order products
     $item_id = $order->add_product($product, 4);
     // Set billing address
     $billing_address = array('country' => 'US', 'first_name' => 'Jeroen', 'last_name' => 'Sormani', 'company' => 'WooCompany', 'address_1' => 'WooAddress', 'address_2' => '', 'postcode' => '123456', 'city' => 'WooCity', 'state' => 'NY', 'email' => '*****@*****.**', 'phone' => '555-32123');
     $order->set_address($billing_address, 'billing');
     // Add shipping costs
     $shipping_taxes = WC_Tax::calc_shipping_tax('10', WC_Tax::get_shipping_tax_rates());
     $order->add_shipping(new WC_Shipping_Rate('flat_rate_shipping', 'Flat rate shipping', '10', $shipping_taxes, 'flat_rate'));
     // Set payment gateway
     $payment_gateways = WC()->payment_gateways->payment_gateways();
     $order->set_payment_method($payment_gateways['bacs']);
     // Set totals
     $order->set_total(10, 'shipping');
     $order->set_total(0, 'cart_discount');
     $order->set_total(0, 'cart_discount_tax');
     $order->set_total(0, 'tax');
     $order->set_total(0, 'shipping_tax');
     $order->set_total(40, 'total');
     // 4 x $10 simple helper product
     return wc_get_order($order->id);
 }
All Usage Examples Of WC_Tax::get_shipping_tax_rates