src/WooCommerce/Invoice/Creator.php
protected function getShippingVatRateLookupMetadata($taxes)
    {
        $result = array();
        if (is_array($taxes)) {
            // Since version ?.?, $taxes has an indirection by key 'total'.
            if (!is_numeric(key($taxes))) {
                $taxes = current($taxes);
            }
            if (is_array($taxes)) {
                foreach ($taxes as $taxRateId => $amount) {
                    if (!Number::isZero($amount)) {
                        $taxRate = WC_Tax::_get_tax_rate($taxRateId, OBJECT);
                        if ($taxRate) {
                            if (empty($result)) {
                                $result = array(
                                    Meta::VatClassId => $taxRate->tax_rate_class !== '' ? $taxRate->tax_rate_class : 'standard',
                                    // Vat class name is the non-sanitized
                                    // version of the id and thus does not
                                    // convey more information: don't add.
                                    Meta::VatRateLookup => array(),
                                    Meta::VatRateLookupLabel => array(),
                                    Meta::VatRateLookupSource => 'shipping line taxes',
                                );
                            }
                            // get_rate_percent() contains a % at the end of the
                            // string: remove it.
                            $result[Meta::VatRateLookup][] = substr(WC_Tax::get_rate_percent($taxRateId), 0, -1);
                            $result[Meta::VatRateLookupLabel][] = WC_Tax::get_rate_label($taxRate);
                        }
                    }
                }
            }
        }

        if (empty($result)) {
            // Apparently we have free shipping (or a misconfigured shipment
            // method). Use a fall-back: WooCommerce only knows 1 tax rate
            // for all shipping methods, stored in config:
            $shippingTaxClass = get_option('woocommerce_shipping_tax_class');
            if (is_string($shippingTaxClass)) {
                /** @var \WC_Order $order */
                $order = $this->invoiceSource->getOrder()->getSource();

                // Since WC3, the shipping tax class can be "inherited" from the
                // product items (which should be the preferred value for this
                // setting). The code to get the "inherited" tax class is more
                // or less copied from WC_Abstract_Order.
                if ($shippingTaxClass === 'inherit') {
                    $foundClasses = array_intersect(array_merge(array(''), WC_Tax::get_tax_class_slugs()), $order->get_items_tax_classes());
                    $shippingTaxClass = count($foundClasses) === 1 ? reset($foundClasses) : false;
                }

                if (is_string($shippingTaxClass)) {
                    $result = $this->getVatRateLookupMetadataByTaxClass($shippingTaxClass);
                    if (!empty($result)) {
                        $result[Meta::VatRateLookupSource] = "get_option('woocommerce_shipping_tax_class')";
                    }
                }
            }
        }

        return $result;
    }
protected function getShippingVatRateLookupMetadata($taxes)
    {
        $result = array();
        if (is_array($taxes)) {
            // Since version ?.?, $taxes has an indirection by key 'total'.
            if (!is_numeric(key($taxes))) {
                $taxes = current($taxes);
            }
            if (is_array($taxes)) {
                foreach ($taxes as $taxRateId => $amount) {
                    if (!Number::isZero($amount)) {
                        $taxRate = WC_Tax::_get_tax_rate($taxRateId, OBJECT);
                        if ($taxRate) {
                            if (empty($result)) {
                                $result = array(
                                    Meta::VatClassId => $taxRate->tax_rate_class !== '' ? $taxRate->tax_rate_class : 'standard',
                                    // Vat class name is the non-sanitized
                                    // version of the id and thus does not
                                    // convey more information: don't add.
                                    Meta::VatRateLookup => array(),
                                    Meta::VatRateLookupLabel => array(),
                                    Meta::VatRateLookupSource => 'shipping line taxes',
                                );
                            }
                            // get_rate_percent() contains a % at the end of the
                            // string: remove it.
                            $result[Meta::VatRateLookup][] = substr(WC_Tax::get_rate_percent($taxRateId), 0, -1);
                            $result[Meta::VatRateLookupLabel][] = WC_Tax::get_rate_label($taxRate);
                        }
                    }
                }
            }
        }

        if (empty($result)) {
            // Apparently we have free shipping (or a misconfigured shipment
            // method). Use a fall-back: WooCommerce only knows 1 tax rate
            // for all shipping methods, stored in config:
            $shippingTaxClass = get_option('woocommerce_shipping_tax_class');
            if (is_string($shippingTaxClass)) {
                /** @var \WC_Order $order */
                $order = $this->invoiceSource->getOrder()->getSource();

                // Since WC3, the shipping tax class can be "inherited" from the
                // product items (which should be the preferred value for this
                // setting). The code to get the "inherited" tax class is more
                // or less copied from WC_Abstract_Order.
                if ($shippingTaxClass === 'inherit') {
                    $foundClasses = array_intersect(array_merge(array(''), WC_Tax::get_tax_class_slugs()), $order->get_items_tax_classes());
                    $shippingTaxClass = count($foundClasses) === 1 ? reset($foundClasses) : false;
                }

                if (is_string($shippingTaxClass)) {
                    $result = $this->getVatRateLookupMetadataByTaxClass($shippingTaxClass);
                    if (!empty($result)) {
                        $result[Meta::VatRateLookupSource] = "get_option('woocommerce_shipping_tax_class')";
                    }
                }
            }
        }

        return $result;
    }
protected function getShippingVatRateLookupMetadata ( $ taxes ) { $ result = array ( ) ; if ( is_array ( $ taxes ) ) { // Since version ?.?, $taxes has an indirection by key 'total'. if ( ! is_numeric ( key ( $ taxes ) ) ) { $ taxes = current ( $ taxes ) ; } if ( is_array ( $ taxes ) ) { foreach ( $ taxes as $ taxRateId => $ amount ) { if ( ! Number :: isZero ( $ amount ) ) { $ taxRate = WC_Tax :: _get_tax_rate ( $ taxRateId , OBJECT ) ; if ( $ taxRate ) { if ( empty ( $ result ) ) { $ result = array ( Meta :: VatClassId => $ taxRate -> tax_rate_class !== '' ? $ taxRate -> tax_rate_class : 'standard' , // Vat class name is the non-sanitized // version of the id and thus does not // convey more information: don't add. Meta :: VatRateLookup => array ( ) , Meta :: VatRateLookupLabel => array ( ) , Meta :: VatRateLookupSource => 'shipping line taxes' , ) ; } // get_rate_percent() contains a % at the end of the // string: remove it. $ result [ Meta :: VatRateLookup ] [ ] = substr ( WC_Tax :: get_rate_percent ( $ taxRateId ) , 0 , - 1 ) ; $ result [ Meta :: VatRateLookupLabel ] [ ] = WC_Tax :: get_rate_label ( $ taxRate ) ; } } } } } if ( empty ( $ result ) ) { // Apparently we have free shipping (or a misconfigured shipment // method). Use a fall-back: WooCommerce only knows 1 tax rate // for all shipping methods, stored in config: $ shippingTaxClass = get_option ( 'woocommerce_shipping_tax_class' ) ; if ( is_string ( $ shippingTaxClass ) ) { /** @var \WC_Order $order */ $ order = $ this -> invoiceSource -> getOrder ( ) -> getSource ( ) ; // Since WC3, the shipping tax class can be "inherited" from the // product items (which should be the preferred value for this // setting). The code to get the "inherited" tax class is more // or less copied from WC_Abstract_Order. if ( $ shippingTaxClass === 'inherit' ) { $ foundClasses = array_intersect ( array_merge ( array ( '' ) , WC_Tax :: get_tax_class_slugs ( ) ) , $ order -> get_items_tax_classes ( ) ) ; $ shippingTaxClass = count ( $ foundClasses ) === 1 ? reset ( $ foundClasses ) : false ; } if ( is_string ( $ shippingTaxClass ) ) { $ result = $ this -> getVatRateLookupMetadataByTaxClass ( $ shippingTaxClass ) ; if ( ! empty ( $ result ) ) { $ result [ Meta :: VatRateLookupSource ] = "get_option('woocommerce_shipping_tax_class')" ; } } } } return $ result ; }
Looks up and returns vat rate metadata for shipping lines.

In WooCommerce, a shipping line can have multiple taxes. I am not sure if
that is possible for Dutch web shops, but if a shipping line does have
multiple taxes we fall back to the tax class setting for shipping
methods, that can have multiple tax rates itself (@see
getVatRateLookupMetadataByTaxClass()). Anyway, this method will only
return metadata if only 1 rate was found.

@param array|null $taxes
The taxes applied to a shipping line.

@return array
An empty array or an array with keys:
- Meta::VatClassId
- Meta::VatRateLookup (*)
- Meta::VatRateLookupLabel (*)
- Meta::VatRateLookupSource (*)
Looks up and returns vat rate metadata for shipping lines .