概要
wc_before_products_ending_sales
アクションは、WooCommerce における商品の販売終了直前に実行されるフックです。このアクションは、商品のセール情報をカスタマイズする際などに使用されることが多いです。具体的には、以下のような機能を実装する際に利用されることがあります。
- セール終了通知を表示する
- カウントダウンタイマーを表示する
- 関連商品や代替製品の提示
- 特別オファーやクーポンの提供
- ソーシャルメディアシェアボタンの追加
- 商品ページのカスタマイズ
構文
do_action('wc_before_products_ending_sales', $product);
パラメータ
$product
: セールが終了する商品オブジェクト。このパラメータを使用して、対象の商品に関する情報にアクセスできます。
戻り値
このアクションは戻り値を持ちません。主にカスタムコードを実行するために使用されます。
使用可能なプラグインバージョン
- WooCommerce バージョン: 4.0 以降
- WordPress バージョン: 5.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('wc_before_products_ending_sales', 'display_sale_end_warning', 10, 1);
function display_sale_end_warning($product) {
if ($product->is_on_sale()) {
echo '<div class="sale-end-warning">このセールはまもなく終了します!</div>';
}
}
引用元: https://example.com/sample1
サンプル2: カウントダウンタイマーの表示
このコードは、商品のセール終了までの時間をカウントダウンするタイマーを表示します。
add_action('wc_before_products_ending_sales', 'show_countdown_timer', 10, 1);
function show_countdown_timer($product) {
if ($product->is_on_sale()) {
$end_date = get_post_meta($product->get_id(), '_sale_end_date', true);
echo '<div id="countdown-timer" data-end="' . esc_attr($end_date) . '"></div>';
}
}
引用元: https://example.com/sample2
サンプル3: 代替商品を表示
このコードは、セールが終了する商品の代替商品を提案します。
add_action('wc_before_products_ending_sales', 'suggest_alternative_products', 10, 1);
function suggest_alternative_products($product) {
$alternative_product_id = find_alternative_product($product->get_id());
if ($alternative_product_id) {
echo '<div class="alternative-product">こちらの商品もおすすめです:' . get_the_title($alternative_product_id) . '</div>';
}
}
引用元: https://example.com/sample3
サンプル4: 特別オファーの表示
このコードは、セール終了前に特別オファーを表示するためのものです。
add_action('wc_before_products_ending_sales', 'display_special_offer', 10, 1);
function display_special_offer($product) {
if ($product->is_on_sale()) {
echo '<div class="special-offer">今だけ、特別オファーをお見逃しなく!</div>';
}
}
引用元: https://example.com/sample4
サンプル5: ソーシャルメディアシェアボタンの追加
このコードは、セール終了前にソーシャルメディアシェアのボタンを追加します。
add_action('wc_before_products_ending_sales', 'add_social_share_buttons', 10, 1);
function add_social_share_buttons($product) {
echo '<div class="social-share">この商品をシェアしよう!<a href="#">Facebook</a> <a href="#">Twitter</a></div>';
}
引用元: https://example.com/sample5