ログイン
始める

Easy Digital Downloads ドキュメント

Easy Digital Downloads のドキュメント、参考資料、チュートリアル

EDD\Fees\Handler クラス

EDD\Fees\Handler クラスを使用して、ショッピングカートに任意の料金(プラスマイナス(割引)の両方)を追加できます。たとえば、特定のユーザーロールを持つすべての登録済みユーザーに自動的に特別な10%オフの料金を追加したり、特定のカテゴリ内のすべての製品に「手数料」を追加したりできます。オプションは無限です。

料金はセッション変数として保存されるため、ユーザーに設定されると、ユーザーがチェックアウトするか、決定または設定に基づいてコードによって削除されるまで持続します。

料金の種類

料金には2つのタイプがあります。

  • fee:これはその名の通り、購入全体に追加される追加費用または割引です。
  • item:これは他のものに関連付けられていない料金です。その場で作成された一時的な製品のようなものと考えてください。ウォレットのデポジットはこの種の料金の例です。

注意:マイナスの料金は常に no_tax を true に設定します。

add_fee()

費用を追加することも、割引を提供することも、どちらも料金を追加することと見なされます。実際には、一方はお金を減らしていますが。

料金の追加は、次のようなコードで行われます。

function eddwp_set_admin_discount() {
// Check to see the customer is a site admin.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

// find 20% of the total
$amount = edd_get_cart_subtotal() * 0.20;

// Flip the 20% from a positive number to a negative number
$amount = $amount * -1;

// Add the fee, sending the amount, the reason printed to the front end, and the handle
EDD()->fees->add_fee(
array(
'price_id' => null, // set this to link the fee to a specific price ID.
'amount' => $amount,
'label' => 'You are an admin special!',
'id' => 'admin_special',
'type' => 'fee',
'no_tax' => false,
'download_id' => 0, // set this to link the fee to a specific product.
)
);
}
add_action( 'template_redirect', 'eddwp_set_admin_discount' );

この例では、現在のユーザーが管理者である場合に自動的に20%の割引が追加されます。

手数料を追加する別の例を次に示します。

function eddwp_add_handling_fee() {
// Add a $10 handling fee no matter what
EDD()->fees->add_fee(
array(
'amount' => 10,
'label' => 'Handling fee',
'id' => 'handling_fee',
'type' => 'fee',
'no_tax' => false,
'download_id' => 0,
)
);
}
add_action( 'template_redirect', 'eddwp_add_handling_fee' );

上記の両方の例が同時に適用されているスクリーンショットを次に示します:

remove_fee()

料金を削除するには、料金ID(上記の3番目のパラメータ)を remove_fee() メソッドに渡すだけです。

function pw_remove_handling_fee() {
EDD()->fees->remove_fee( 'handling_fee' );
}
add_action( 'template_redirect', 'pw_remove_handling_fee', 9999 );

その他のメソッド

料金の追加と削除だけでなく、バックエンドで料金を操作するための多くのメソッドがあります。

has_fees( string $type = ‘fee’  )

このメソッドは、料金が存在するかどうかを確認するだけです。  デフォルトは料金タイプですが、item も受け入れることができます。

例:

if ( EDD()->fees->has_fees() ) {
	echo 'yep, we can haz fees!';
}

返り値: true または false。

get_fees( string $type = ‘fee’, integer $download_id = 0  )

このメソッドは、存在するすべての料金の配列を取得します。  タイプおよび/またはダウンロードIDで制限できます。

Simple Shipping extension は、EDD_Fees クラスの使用方法の完璧な例です。

$fees = EDD()->fees->get_fees();

次のようなものを返します。

Array
(
    [handling_fee] => Array
        (
            [amount]      => 11.00
            [label]       => Handling Fee
            [type]        => fee
            [no_tax]      => 
            [download_id] => 0
            [price_id]    =>
        )

    [admin_special] => Array
        (
            [amount]      => -2
            [label]       => You are an admin special!
            [type]        => fee
            [no_tax]      => 
            [download_id] => 0
            [price_id]    =>
        )

    [tophers_fee] => Array
        (
            [amount]      => 42.00
            [label]       => Topher's fee
            [no_tax]      => 
            [type]        => fee
            [download_id] => 114
            [price_id]    =>
        )

)
get_fee( string $id = ”  )

このメソッドは、作成時に付けられた名前で識別される特定の料金の配列を取得します。  

例: 

$my_custom_fee = EDD()->fees->get_fee( 'my_custom_fee' );

次のようなものを返します。

 Array
(
    [amount] => 42.00
    [label] => The Final Fee
    [no_tax] => 
    [type] => fee
    [download_id] => 114
)
type_total( string $type = ‘fee’  )

このメソッドは、特定のタイプの料金の合計を計算します。  サポートされているタイプは「fee」と「item」です。

例: 

$item_total = EDD()->fees->type_total( 'item' );

51.00 のような数値を返します

合計( integer $download_id = 0  )

このメソッドは、すべての手数料、または特定のダウンロードに添付されたすべて手数料の合計を計算します。

注意: 商品に手数料と変動価格がある場合、手数料は取引ごとに1回のみ適用され、商品ごとには適用されません。

例:

$total = EDD()->fees->total();

$total = EDD()->fees->total( '42' );

上記の例では、最初の場合、すべて手数料の合計である51.00のような数値が返されます。

2番目の例では、IDが42のダウンロードに固有の手数料の合計である51.00のような数値が返されます。

record_fees( array $payment_meta, array $payment_data )

このメソッドは、特定のトランザクションの手数料情報を記録します。直接呼び出すべきではありません。

この記事は役に立ちましたか?

今日から販売を開始しましょう!

50,000人以上のスマートなストアオーナーに参加して、WordPressでデジタル製品を販売する最も簡単な方法を使い始めましょう。

Copyright © 2025 Sandhills Development, LLC

[universally_switcher]