概要
woocommerce_widget_shopping_cart_before_buttons
アクションは、WooCommerceのショッピングカートウィジェットで、ボタンの前にカスタムコンテンツを追加するためのフックです。このフックは、カートのボタンの前に特定の情報や機能を追加する際に便利です。よく使われるケースは以下の通りです:
- カスタムメッセージを表示
- プロモーションコードの入力フォームを追加
- 配送情報やアクセス情報の案内
- カート内容のカスタムサマリーを表示
- ショッピングカートの他の製品を提案するフェアを追加
- メンバーシップやロイヤリティプログラムの情報を表示
構文
add_action('woocommerce_widget_shopping_cart_before_buttons', 'your_custom_function');
function your_custom_function() {
// Your custom code here
}
パラメータ
- このアクションには引数はありません。
戻り値
- このアクションは何も戻しません。
使用可能なバージョン
- WooCommerceバージョン: 3.0以上
- WordPressバージョン: 4.0以上
サンプルコード
サンプルコード 1: カスタムメッセージを表示
add_action('woocommerce_widget_shopping_cart_before_buttons', 'custom_cart_message');
function custom_cart_message() {
echo '<p style="color: green;">お買い物ありがとうございます!</p>';
}
このサンプルコードは、ショッピングカートに「お買い物ありがとうございます!」というメッセージを緑色で追加します。
サンプルコード 2: プロモーションコード入力フォームの追加
add_action('woocommerce_widget_shopping_cart_before_buttons', 'promo_code_form');
function promo_code_form() {
echo '<input type="text" placeholder="プロモーションコードを入力" name="promo_code" />';
}
このサンプルでは、ショッピングカートのボタンの前にプロモーションコードを入力するためのテキストボックスを追加します。
サンプルコード 3: 配送オプションの情報を表示
add_action('woocommerce_widget_shopping_cart_before_buttons', 'shipping_info');
function shipping_info() {
echo '<p>全ての注文に対して送料無料です!</p>';
}
このコードは、ユーザーに無料配送の情報をカートのボタンの前に表示します。
サンプルコード 4: カート内の合計額を表示
add_action('woocommerce_widget_shopping_cart_before_buttons', 'display_cart_total');
function display_cart_total() {
$cart_total = WC()->cart->get_cart_total();
echo '<p>カートの合計: ' . $cart_total . '</p>';
}
このサンプルは、ショッピングカートのボタンの前にカートの合計額を表示します。
サンプルコード 5: おすすめ製品の提案
add_action('woocommerce_widget_shopping_cart_before_buttons', 'suggest_related_products');
function suggest_related_products() {
echo '<p>あなたへのおすすめ商品:</p>';
// 商品をリストするロジックをここに追加
}
このコードは、ショッピングカートに「あなたへのおすすめ商品」とのテキストを追加します。続けておすすめ商品をリストするロジックを追加できます。
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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 |