WooCommerce : 세금 라인으로 주문 내보내기 받기

Dec 01 2020

세율로 구분 된 세금 라인으로 모든 주문을 내보내고 싶습니다.

예 : 주문의 세율이 7 % 및 19 % 일 수 있습니다. 주문에 세율이 모두있는 제품이있는 경우 하나의 통합 세액 만받습니다.

주문에 각각 100 유로에 두 개의 제품이 있다고 가정 해 보겠습니다. 다음과 같은 세금이 있습니다.

  • 할인 된 요금으로 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 €

세율에 대한 금액이 없으면 열이 비어있을 수 있습니다.

라는 주문 속성이 있다는 것을 알고 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,
)

그런 다음 세율 항목이있는 foreach 루프와 다음과 같이 세율 백분율을 얻는 WC_Tax방법get_rate_percent() 을 사용 합니다.

// 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에서 주문 세금 세부 정보 및 요율을 얻는 방법은 무엇입니까?
  • 세금 분류를 사용하여 별도의 주문 총액을 가져옵니다.