概要
woocommerce_shipping_method_add_rate
アクションは、WooCommerceの配送方法に新しい配送料金を追加するためのフックです。このアクションは、店舗オーナーが特定の条件に基づいて独自の配送料金を追加したり、既存の配送メソッドを拡張したりする際に頻繁に使用されます。このアクションは、特に以下の機能を実装する際に役立ちます。
- 特定の地域や国向けのカスタム配送料金の追加
- 購入金額に基づいた料金の変更
- 商品の重量やサイズに基づく特別な配送オプションの作成
- 会員向けの特別価格の提供
- プロモーションやセールに基づく割引配送の実装
- 他のプラグインとの連携によるカスタム配送オプションの実装
構文
add_action( 'woocommerce_shipping_method_add_rate', 'my_custom_shipping_method', 10, 3 );
パラメータ
WC_Shipping_Method
: 使用する配送メソッドのインスタンス。double
: 配送料金。array
: 配送に関連する設定。
戻り値
このアクションは特定の値を返さず、配送メソッドに新しい値を追加する目的で使用されます。
互換性
- WooCommerce バージョン: 2.0以降
- WordPress バージョン: 4.0以降
この関数のアクションでの使用可能性
アクション | 使用可能 |
---|---|
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: 地域別の追加送料
このサンプルコードは、特定の地域に対して追加の送料を設定します。例えば、北海道への配送には追加の500円を加算します。
add_action( 'woocommerce_shipping_method_add_rate', 'add_hokkaido_shipping_fee', 10, 3 );
function add_hokkaido_shipping_fee( $method, $index, $instance ) {
$cost = 500; // 追加送料
$method->add_rate( array(
'id' => 'hokkaido_shipping',
'label' => '北海道追加送料',
'cost' => $cost,
'calc_tax' => 'per_item'
));
}
引用元: https://docs.woocommerce.com/wc-apidocs/class-WC_Shipping_Method.html
サンプル 2: 定額配送設定
このコードでは、特定の条件を満たす利用者に対して定額送料を提供します。
add_action( 'woocommerce_shipping_method_add_rate', 'fixed_shipping_rate', 10, 3 );
function fixed_shipping_rate( $method, $index, $instance ) {
$method->add_rate( array(
'id' => 'fixed_rate',
'label' => '定額送料',
'cost' => '1500',
'calc_tax' => 'per_item'
));
}
引用元: https://woocommerce.com/document/using-the-woocommerce-shipping-api/
サンプル 3: 無料配送クーポン適用
このサンプルコードは、特定のクーポンコードが使用されている場合に無料配送を提供します。
add_action( 'woocommerce_shipping_method_add_rate', 'free_shipping_with_coupon', 10, 3 );
function free_shipping_with_coupon( $method, $index, $instance ) {
if ( WC()->cart->has_discount( 'FREESHIP' ) ) {
$method->add_rate( array(
'id' => 'free_shipping',
'label' => '無料配送',
'cost' => '0',
'calc_tax' => 'per_item'
));
}
}
引用元: https://www.businessbloomer.com/woocommerce-free-shipping-method/
サンプル 4: 商品の重さに基づく料金設定
商品の重量に基づいて異なる料金を設定する例です。
add_action( 'woocommerce_shipping_method_add_rate', 'weight_based_shipping', 10, 3 );
function weight_based_shipping( $method, $index, $instance ) {
$weight = WC()->cart->get_cart_contents_weight();
$cost = ( $weight > 10 ) ? 2000 : 1000; // 重さによって料金を決定
$method->add_rate( array(
'id' => 'weight_based_shipping',
'label' => '重さベース配送',
'cost' => $cost,
'calc_tax' => 'per_item'
));
}
引用元: https://docs.woocommerce.com/document/woocommerce-shipping-classes/
サンプル 5: 会員専用の特別配送方法
このサンプルコードは、特定のユーザーグループに対して特別な配送方法を追加します。
add_action( 'woocommerce_shipping_method_add_rate', 'members_only_shipping', 10, 3 );
function members_only_shipping( $method, $index, $instance ) {
if ( is_user_logged_in() && current_user_can( 'member_role' ) ) {
$method->add_rate( array(
'id' => 'members_only',
'label' => '会員限定配送',
'cost' => '800',
'calc_tax' => 'per_order'
));
}
}
引用元: https://wpsitesuccess.com/woocommerce-shipping/
以上が woocommerce_shipping_method_add_rate
アクションの概要とサンプルコードの解説です。このアクションを利用することにより、カスタマイズされた配送オプションを提供することができます。