概要
woocommerce_cart_ready_to_calc_shipping
は、WooCommerceのカートが送料を計算する準備が整ったときに発火するアクションフックです。このフックは、カート内の商品が変わったり、ユーザーが住所情報を入力したりした場合に、送料をカスタマイズしたり、特定の条件に基づいて送料を追加するためによく使用されます。具体的には、以下のような機能を実装する際に利用されることがよくあります。
- 特定の条件に基づいて送料を無料にする
- 特定の商品に対して特別な送料を適用する
- 地域ごとに異なる送料の計算を行う
- 会員専用の送料割引を適用する
- 特定の日時やイベントに基づいて送料を調整する
- ユーザーの選択に基づいて送料オプションを提供する
構文
add_action('woocommerce_cart_ready_to_calc_shipping', 'your_function_name');
パラメータ
このアクションには特定のパラメータはありませんが、WooCommerceのカートが準備できた状態で発火します。
戻り値
このアクション自体は値を返しませんが、フックされた関数内で送料計算のための変更が行われます。
使用可能なバージョン
- WooCommerceのバージョン: 3.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
add_action('woocommerce_cart_ready_to_calc_shipping', 'custom_free_shipping_for_new_customers');
function custom_free_shipping_for_new_customers() {
if (WC()->customer->get_is_new()) {
WC()->session->set('chosen_shipping_methods', array('free_shipping'));
}
}
このコードは、新規顧客に対して自動的に無料配送を選択します。新規顧客の判定は get_is_new()
メソッドに基づいています。
URL: https://woocommerce.com/document/woocommerce-customer-classes/
サンプルコード2
add_action('woocommerce_cart_ready_to_calc_shipping', 'custom_shipping_rate_for_specific_product');
function custom_shipping_rate_for_specific_product() {
$cart = WC()->cart->get_cart();
foreach ( $cart as $cart_item ) {
$product_id = $cart_item['product_id'];
if ( $product_id == 123 ) { // 特定の商品のID
WC()->session->set('chosen_shipping_methods', array('flat_rate:1'));
break;
}
}
}
このコードは、カート内に特定の商品が存在する場合に、その商品に適用される送料を制御します。
URL: https://woocommerce.com/document/conditional-shipping-methods/
サンプルコード3
add_action('woocommerce_cart_ready_to_calc_shipping', 'apply_discount_for_local_shipping');
function apply_discount_for_local_shipping() {
$shipping_methods = WC()->shipping->get_packages();
foreach ($shipping_methods as &$package) {
if (isset($package['destination']['state']) && $package['destination']['state'] == 'CA') {
$package['rates']['flat_rate:1']->cost *= 0.9; // 送料を10%割引
}
}
}
このコードは、特定の州(この例ではカリフォルニア)への配送に対して送料を10%割引します。
URL: https://woocommerce.com/document/setting-up-shipping-zones/
サンプルコード4
add_action('woocommerce_cart_ready_to_calc_shipping', 'add_shipping_fee_for_gift_wrap');
function add_shipping_fee_for_gift_wrap() {
if (WC()->cart->get_cart_contents_count() > 0 && isset($_POST['gift_wrap']) && $_POST['gift_wrap'] === 'yes') {
WC()->cart->add_fee(__('Gift Wrap Fee', 'woocommerce'), 5); // ギフトラッピング料金を追加
}
}
このコードは、ユーザーがギフトラッピングを選択した場合に5ドルの手数料を追加します。
URL: https://woocommerce.com/document/woocommerce-fees-and-charges/
サンプルコード5
add_action('woocommerce_cart_ready_to_calc_shipping', 'offer_fast_shipping_option');
function offer_fast_shipping_option() {
if (WC()->cart->subtotal >= 50) { // 合計金額が50ドル以上
WC()->session->set('chosen_shipping_methods', array('express_shipping'));
}
}
このコードは、カート内の商品合計が50ドル以上の場合に、速達配送オプションを自動選択します。
URL: https://woocommerce.com/document/woocommerce-shipping-options/