プラグインWooCommerceのwoocommerce_order_item_shipping_after_calculate_taxesアクションの使用方法・解説

概要

woocommerce_order_item_shipping_after_calculate_taxes は WooCommerce で利用可能なフックであり、注文アイテムの送料に関する税金が計算された後にトリガーされます。このアクションは、送料の税金計算後に追加の処理やカスタマイズを行うために使用されます。このアクションを利用することで、特定のビジネスニーズに応じて税金の計算や手数料の追加などを行うことができます。

以下のような機能を実装する際に、このアクションがよく使われます:

  1. 運送料に特定の手数料を加算する
  2. 地域による異なる税率を適用する
  3. クーポンによる割引後の税額調整
  4. 特定の商品の送料に条件付きの追加料金を加える
  5. カスタムデータを送料の税金に基づいて保存する
  6. ショップの送料設定に基づく動的な調整

このアクションは、WooCommerce 3.0.0 以降で利用可能です。また、WordPress のバージョンは 4.7 以降から対応しています。

構文

do_action( 'woocommerce_order_item_shipping_after_calculate_taxes', $item_id, $order, $shipping_total, $shipping_tax_total, $taxes );

パラメータ

  • $item_id: 注文アイテムの ID。
  • $order: WooCommerce 注文オブジェクト。
  • $shipping_total: 送料の合計。
  • $shipping_tax_total: 送料に対する税金の合計。
  • $taxes: 税金の配列。

戻り値

このアクションには戻り値はありません。あくまでも処理を行うためのフックです。

この関数のアクションでの使用可能性

アクション 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード1: 手数料の追加

このサンプルコードでは、特定の条件を満たす場合に送料に手数料を加算します。

add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', 'add_custom_shipping_fee', 10, 5 );

function add_custom_shipping_fee( $item_id, $order, $shipping_total, $shipping_tax_total, $taxes ) {
    if ( $shipping_total > 50 ) {
        $additional_fee = 5;
        $shipping_total += $additional_fee;
    }
}

サンプルコード2: 地域別の税率適用

このコーディング例では、特定の地域に対して異なる税率を適用します。

add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', 'apply_different_tax_rate', 10, 5 );

function apply_different_tax_rate( $item_id, $order, $shipping_total, $shipping_tax_total, $taxes ) {
    if ( $order->get_shipping_country() === 'JP' ) {
        $taxes[] = array( 'rate_id' => 'flat_rate:1', 'label' => 'Japan Tax', 'amount' => $shipping_total * 0.1 );
    }
}

サンプルコード3: クーポンによる割引調整

この例では、クーポン適用後に送料の税額を調整します。

add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', 'adjust_tax_after_coupon', 10, 5 );

function adjust_tax_after_coupon( $item_id, $order, $shipping_total, $shipping_tax_total, $taxes ) {
    if ( $order->get_discount_total() > 0 ) {
        $shipping_tax_total -= ( $order->get_discount_total() * 0.1 ); // 割引に基づく税調整
    }
}

サンプルコード4: 特定商品の送料への追加料金

このサンプルでは、特定の商品が注文に含まれている場合に送料に追加料金を適用します。

add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', 'add_fee_for_specific_product', 10, 5 );

function add_fee_for_specific_product( $item_id, $order, $shipping_total, $shipping_tax_total, $taxes ) {
    $items = $order->get_items();
    foreach ( $items as $item ) {
        if ( $item->get_product_id() == 123 ) { // 特定の商品ID
            $shipping_total += 10; // 追加料金
            break;
        }
    }
}

サンプルコード5: カスタムデータの保存

この例では、送料の税金に関連してカスタムデータを保存します。

add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', 'save_shipping_tax_data', 10, 5 );

function save_shipping_tax_data( $item_id, $order, $shipping_total, $shipping_tax_total, $taxes ) {
    $custom_data = array( 'tax_data' => $taxes );
    update_post_meta( $item_id, '_shipping_tax_data', $custom_data ); // メタデータに保存
}

以上が woocommerce_order_item_shipping_after_calculate_taxes アクションを使用する際の解説とサンプルコードです。

この関数について質問する


上の計算式の答えを入力してください