WooCommerce:税ラインで注文のエクスポートを取得

Dec 01 2020

税率で区切られた税ラインを使用して、すべての注文をエクスポートしたいと思います。

例:注文の税率は7%と19%になります。注文に両方の税率の商品が含まれている場合、合計税額は1つだけです。

注文にそれぞれ100€の2つの製品があるとします。次の税金がかかります。

  • 割引料金で100€:7€
  • デフォルトレートで100€:19€
  • 合計税額:26€

次のコード(抜粋)を使用して注文データをエクスポートしています:

$order_data = $order->get_data(); // The Order data 
$order_date_created = $order_data['date_created']->date('d.m.Y'); // Example for oder creation date

データを取得したら、それらを配列に格納する必要があります。

$custom_data = array( 'custom_order_date_created' => $custom_order_date_created,
)

必要なのは、$order_date_createdすべての税率の固定列(上記のような)です。

お気に入り:

  • 列1:税率-7%
  • 列2:税額-7€
  • 列3:税率-19%
  • 列4:税額-19€

税率の金額がない場合、列は空である可能性があります。

と呼ばれるorderプロパティがあることを知っていますtax_lines。しかし、それは配列であり、私はそれを使用する方法がわかりません。プロパティにtax_linesは、いくつかの独自の税ラインプロパティがあります。

誰かが注文からこれらの値を取得する方法の手がかりを持っていますか?

編集:私は別の質問でスニペットを見つけました:

foreach ( $order->get_tax_totals() as $rate_code => $tax ) {
            $tax_rate_id = $tax->rate_id;
            $tax_label = $tax->label;
            $tax_amount = $tax->amount;
            $tax_f_amount = $tax->formatted_amount;
            $compound = $tax->is_compound;
            echo '<tr><td>' . $tax_label . ': </td><td>' . $tax_f_amount . '</td></tr>';
        }

見た目は正しいですが、コード例で使用して配列に格納する方法がわかりません。

編集2:もう少し掘り下げた後、注文税項目が含まれているWC_Order_Item_Tax オブジェクトを見つけました。そこで、そのデータを使おうとしましたが、それは巨大なオブジェクトであり、データを選択して右の列に表示する方法がわかりません。

このように見えます(もっとたくさんあります)。そして、私rate_idは例えばによって税を選択する必要があります:

[4543] => WC_Order_Item_Tax Object
        (
            [extra_data:protected] => Array
                (
                    [rate_code] => 
                    [rate_id] => 0
                    [label] => 
                    [compound] => 
                    [tax_total] => 0
                    [shipping_tax_total] => 0
                    [rate_percent] => 
                )

            [data:protected] => Array
                (
                    [order_id] => 11244
                    [name] => 
                    [rate_code] => DE-MWST.-1
                    [rate_id] => 1
                    [label] => MwSt.
                    [compound] => 1
                    [tax_total] => 7.54
                    [shipping_tax_total] => 0
                    [rate_percent] => 16
                )

            [cache_group:protected] => order-items
            [meta_type:protected] => order_item
            [object_type:protected] => order_item
            [id:protected] => 4543
            [changes:protected] => Array
                (
                )

            [object_read:protected] => 1
            [default_data:protected] => Array
                (
                    [order_id] => 0
                    [name] => 
                    [rate_code] => 
                    [rate_id] => 0
                    [label] => 
                    [compound] => 
                    [tax_total] => 0
                    [shipping_tax_total] => 0
                    [rate_percent] => 
                )

回答

1 LoicTheAztec Dec 02 2020 at 01:27

あなたはこのコード行の後にあるかもしれないこのようにそれを使うことができます

$custom_data = array( 'custom_order_date_created' => $custom_order_date_created,
)

次に、税率項目を使用し、WC_Taxメソッドget_rate_percent()を使用して次のように税率パーセンテージを取得するforeachループを実行します。

// Loop through order tax items
foreach ( $order->get_tax_totals() as $rate_code => $tax ) { // Store it in the array $custom_data['taxes'][] = array(
        'tax_rate'   =>  WC_Tax::get_rate_percent($tax->rate_id), // <=== the tax rate percentage 'tax_amount' => $tax->formatted_amount
    );
}

関連:

  • WooCommerceで注文税の詳細と料金を取得するにはどうすればよいですか?
  • 税クラスで個別の注文の税合計金額を取得する