Get tax rate label of "orderlineitem" in woocommerce and store in xml

How to show tax percentage for each item in order in an xml. i create xml when order is paid. but need to show item tax labels for each items for example. 7% Mswt, 19% Mswt.

    function get_line_items( $order ) {

    $items = [];
    

    /** @var \WC_Order_Item_Product $line_items */
    $line_items = $order-get_items( 'line_item' );

    // loop through each item in order
    foreach ( $line_items as $item_id = $item ) {

        // get the product
        /** @var \WC_Product $product */
        $product = $item-get_product();

        // instantiate line item meta
        $meta_data    = $item-get_formatted_meta_data( '_', true );
        $display_meta = [];

        foreach ( $meta_data as $meta ) {
            $display_meta[] = {$meta-display_key}: {$meta-display_value};
        }

        $item_meta = implode( ', ', $display_meta );

        // remove all HTML
        $item_meta = wp_strip_all_tags( $item_meta );

        // strip HTML in legacy format - note: in modern formats,
        // SV_WC_Helper::array_to_xml will automatically escape HTML and newlines by wrapping
        // the contents of the tag in CDATA when necessary
        if ( 'legacy' === $this-export_format ) {
            // remove control characters
            $item_meta = str_replace( [ \r, \n, \t ], '', $item_meta );
        }

        // remove any html entities
        $item_meta = preg_replace( '/\(?:[a-z,A-Z,0-9]+|#\d+|#x[0-9a-f]+);/', '', $item_meta );

        $item_data = [];

        $item_data['Id']               = $item_id;
        $item_data['Name']             = html_entity_decode( $product ? $product-get_title() : $item['name'], ENT_NOQUOTES, 'UTF-8' );
        $item_data['ProductId']        = $product ? $product-get_id() : '';  // handling for permanently deleted product
        $item_data['SKU']              = $product ? $product-get_sku() : '';  // handling for permanently deleted product
        $item_data['Quantity']         = $item['qty'];
        $item_data['Price']            = $this-format_decimal( $order-get_item_total( $item ), 2 );
        $item_data['Subtotal']         = $this-format_decimal( $order-get_line_subtotal( $item ), 2 );
        //$item_data['SubtotalTax']      = $this-format_decimal( $item['line_subtotal_tax'], 2 );
        $item_data['Total']            = $this-format_decimal( $order-get_line_total( $item ), 2 );
        $item_data['TaxPercentage']    = $item -get_tax_class()-labels;
        $item_data['TotalTax']         = $this-format_decimal( $order-get_line_tax( $item ), 2 );
        
        //$item_data['Refunded']         = $this-format_decimal( $order-get_total_refunded_for_item( $item ), 2 );
        //$item_data['RefundedQuantity'] = $order-get_qty_refunded_for_item( $item_id );

        /*if ( 'yes' === get_option( 'woocommerce_calc_taxes' )  'yes' === get_option( 'woocommerce_prices_include_tax' ) ) {
            $item_data['PriceInclTax'] = $this-format_decimal( $order-get_item_total( $item, true ), 2 );
            $item_data['TotalInclTax'] = $this-format_decimal( $order-get_line_total( $item, true ), 2 );
        }*/

        $item_data['Meta'] = $item_meta;

        //$item_data['Taxes'] = $this-get_tax_details( $item );

        // Keep order items backwards-compatible with legacy version
        if ( 'legacy' === $this-export_format ) {

            // rename fields to be compatible with pre 2.0.0
            $item_data['ItemName']  = $item_data['Name'];
            $item_data['LineTotal'] = $item_data['Total'];

            if ( 'yes' === get_option( 'woocommerce_calc_taxes' )  'yes' === get_option( 'woocommerce_prices_include_tax' ) ) {
                $item_data['LineTotalInclTax'] = $item_data['TotalInclTax'];
            }

            // remove data that wasn't present pre 2.0.0
            unset( $item_data['Id'], $item_data['Name'], $item_data['ProductId'], $item_data['Subtotal'], $item_data['SubtotalTax'], $item_data['Total'], $item_data['TotalTax'], $item_data['Refunded'], $item_data['RefundedQuantity'], $item_data['TotalInclTax'], $item_data['Taxes'] );
        }

        /**
         * Allow actors to modify the line item data / format
         *
         * In 2.0.0 renamed from `wc_customer_order_xml_export_suite_order_export_line_item_format`
         * to `wc_customer_order_xml_export_suite_order_line_item`
         *
         * @since 5.0.0
         *
         * @param array $item_data
         * @param \WC_Order $order Related order
         * @param array $item Order line item
         */
        $items['OrderLineItem'][] = apply_filters( 'wc_customer_order_export_xml_order_line_item', $item_data, $order, $item );
    }

    return ! empty( $items ) ? $items : null;
}

Topic woocommerce-offtopic php Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.