プラグインWooCommerceのwoocommerce_cart_shipping_totalフィルタの使用方法・解説

概要

woocommerce_cart_shipping_totalフィルタは、WooCommerceのショッピングカートにおける送料の合計を制御するために使用されます。このフィルタは、カート内の商品に基づいて動的に送料を調整することができ、さまざまな追加機能を実装する際に役立ちます。一般的に、このフィルタは以下のような機能を実装する際によく使われます。

  1. 特定の条件に基づく送料の割引
  2. 地域ごとの送料の変更
  3. プロモーションコードによる送料の調整
  4. 特定の商品カテゴリーへの送料の変更
  5. 新規顧客への特別送料オファー
  6. 定期購入やサブスクリプションの送料管理

構文

add_filter('woocommerce_cart_shipping_total', 'your_custom_function');

パラメータ

  • string $shipping_total: 現在の送料合計の値。

戻り値

  • string: 変更された送料合計の値。

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

  • WooCommerceバージョン: 3.0以上

使用可能なWordPressのバージョン

  • 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: 送料に特別割引を適用する

このサンプルコードでは、カートの合計に基づいて送料を10%割引する機能を実装しています。

add_filter('woocommerce_cart_shipping_total', 'apply_discount_on_shipping');

function apply_discount_on_shipping($shipping_total) {
    $discount = 0.10; // 10%の割引
    $shipping_total = $shipping_total * (1 - $discount);
    return $shipping_total;
}

引用元: https://woocommerce.com/

サンプル2: 地域ごとの送料を変更する

このコードでは、特定の地域(例:東京)に対して送料を特別に設定します。

add_filter('woocommerce_cart_shipping_total', 'change_shipping_for_tokyo');

function change_shipping_for_tokyo($shipping_total) {
    // 顧客の地域を取得
    $customer_country = WC()->customer->get_shipping_country();

    if ($customer_country == 'JP' && WC()->customer->get_shipping_city() == 'Tokyo') {
        $shipping_total = '¥500'; // 東京の送料
    }

    return $shipping_total;
}

引用元: https://woocommerce.com/

サンプル3: プロモーションコードによる送料調整

このサンプルは、特定のプロモーションコードが適用された場合に送料を無料にする機能を実装しています。

add_filter('woocommerce_cart_shipping_total', 'free_shipping_on_promo_code');

function free_shipping_on_promo_code($shipping_total) {
    if (isset($_COOKIE['promo_code']) && $_COOKIE['promo_code'] == 'FREESHIP') {
        return '¥0'; // 送料を無料に
    }

    return $shipping_total;
}

引用元: https://woocommerce.com/

サンプル4: 特定のカテゴリー商品への特別送料

このコードでは、特定のカテゴリー商品(例:本)の注文に対して特別な送料を適用します。

add_filter('woocommerce_cart_shipping_total', 'special_shipping_for_books');

function special_shipping_for_books($shipping_total) {
    $has_books = false;
    foreach (WC()->cart->get_cart() as $cart_item) {
        if (has_term('books', 'product_cat', $cart_item['product_id'])) {
            $has_books = true;
            break;
        }
    }

    if ($has_books) {
        return '¥300'; // 本のカテゴリへの送料
    }

    return $shipping_total;
}

引用元: https://woocommerce.com/

サンプル5: 定期購入向けの送料を管理

このコードでは、定期購入およびサブスクリプションの商品に対して特別な送料を適用します。

add_filter('woocommerce_cart_shipping_total', 'adjust_shipping_for_subscriptions');

function adjust_shipping_for_subscriptions($shipping_total) {
    if (WC_Subscriptions_Product::is_subscription(WC()->cart->get_cart_item(request))); {
        return '¥200'; // 定期購入専用の送料
    }

    return $shipping_total;
}

引用元: https://woocommerce.com/

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


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