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

概要

woocommerce_load_shipping_methodsは、WooCommerceでの配送方法のロードプロセスをフックするためのアクションです。このアクションを使用して、カスタム配送メソッドを追加したり、既存のメソッドを変更したりすることができます。どのような機能を実装する際にこのフックがよく使われるかを以下に示します。

  1. カスタム配送方法の追加
  2. 配送方法の表示条件を変更
  3. 配送方法のコスト計算のカスタマイズ
  4. 特定の条件に基づいた配送オプションの調整
  5. 地域による配送オプションの変更
  6. 配送メソッドのフィルタリング

構文

add_action( 'woocommerce_load_shipping_methods', 'your_function_name' );

パラメータ

  • your_function_name: 実行する関数の名前。

戻り値

  • 特になし。フックされた関数内で処理を行い、必要に応じて配送メソッドを返します。

使用可能なプラグインバージョン

  • WooCommerce: 3.0 以上
  • WordPress: 4.0 以上

サンプルコード1

このサンプルは、カスタム配送メソッド「カスタム配送」を追加します。

add_action( 'woocommerce_load_shipping_methods', 'add_custom_shipping_method' );

function add_custom_shipping_method( $methods ) {
    $methods['custom_shipping'] = 'WC_Custom_Shipping_Method';
    return $methods;
}

class WC_Custom_Shipping_Method extends WC_Shipping_Method {
    public function __construct() {
        $this->id = 'custom_shipping'; 
        $this->method_title = __( 'カスタム配送', 'text-domain' );
        $this->method_description = __( 'カスタム配送方法の説明', 'text-domain' );
        $this->enabled = 'yes'; 
        $this->title = __( 'カスタム配送', 'text-domain' );
    }

    public function calculate_shipping( $package = array() ) {
        // 配送料計算のロジック
        $rate = array(
            'id'    => $this->id,
            'label' => $this->title,
            'cost'  => '10.00',
            'calc_tax' => 'per_item'
        );

        $this->add_rate( $rate );
    }
}

引用元

Homepage

サンプルコード2

このサンプルは、特定の条件(地域)に基づいてカスタム配送メソッドを適用します。

add_action( 'woocommerce_load_shipping_methods', 'conditional_shipping_methods' );

function conditional_shipping_methods( $methods ) {
    if ( WC()->customer->get_shipping_country() == 'JP' ) {
        $methods['japan_shipping'] = 'WC_Japan_Shipping_Method';
    }
    return $methods;
}

class WC_Japan_Shipping_Method extends WC_Shipping_Method {
    public function __construct() {
        $this->id = 'japan_shipping';
        $this->method_title = __( '日本国内配送', 'text-domain' );
        $this->enabled = 'yes'; 
        $this->title = __( '日本国内配送', 'text-domain' );
    }

    public function calculate_shipping( $package = array() ) {
        // 日本国内配送の計算ロジック
        $this->add_rate( array(
            'id' => $this->id,
            'label' => $this->title,
            'cost' => '500',
        ));
    }
}

引用元

Homepage

サンプルコード3

このサンプルコードは、配送方法のタイトルを動的に変更します。

add_action( 'woocommerce_load_shipping_methods', 'modify_shipping_method_title' );

function modify_shipping_method_title( $methods ) {
    if ( isset( $methods['flat_rate'] ) ) {
        $methods['flat_rate']->method_title = __( '標準配送 - 日本', 'text-domain' );
    }
    return $methods;
}

引用元

Homepage

サンプルコード4

このサンプルは、配送方法に基づいて異なる料金を設定します。

add_action( 'woocommerce_load_shipping_methods', 'dynamic_shipping_cost' );

function dynamic_shipping_cost( $methods ) {
    if ( WC()->cart->subtotal >= 10000 ) {
        $methods['flat_rate']->cost = '0'; // 10,000以上は無料
    }
    return $methods;
}

引用元

Homepage

サンプルコード5

このコードは、地域によって異なる配送オプションを提供します。

add_action( 'woocommerce_load_shipping_methods', 'region_based_shipping_methods' );

function region_based_shipping_methods( $methods ) {
    $customer_region = WC()->customer->get_shipping_state();
    if ( $customer_region == 'CA' ) {
        $methods['express_shipping'] = 'WC_Express_Shipping_Method';
    }
    return $methods;
}

class WC_Express_Shipping_Method extends WC_Shipping_Method {
    public function __construct() {
        $this->id = 'express_shipping'; 
        $this->method_title = __( '速達配送', 'text-domain' );
    }

    public function calculate_shipping( $package = array() ) {
        $this->add_rate( array(
            'id' => $this->id,
            'label' => $this->method_title,
            'cost' => '1500',
        ));
    }
}

引用元

Homepage

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

アクション 使用例
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

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


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