概要
woocommerce_product_set_stock_status
は、WooCommerceプラグインで商品在庫状況が設定される際に発火するアクションフックです。このアクションは、商品の在庫状況が変更されたときに特定の処理を行いたい場合に使用されます。
このアクションは、次のような機能を実装する際によく使われます。
- 在庫状況の変更に基づくカスタム通知の送信
- 在庫状況が「在庫切れ」の商品のウェブサイト上の表示を変更
- 特定の在庫状況に基づくマーケティング活動のトリガー
- 在庫状況に応じた商品価格の調整
- 在庫状況の変更を他のシステムと連携するためのトリガー
- 商品在庫状況が変更された場合にカスタムデータを保存
構文
add_action( 'woocommerce_product_set_stock_status', 'your_custom_function', 10, 2 );
パラメータ
$product
(WC_Product): 変更された商品のオブジェクト。$status
(string): 新しい在庫状況(’instock’, ‘outofstock’, ‘onbackorder’のいずれか)。
戻り値
このアクションは値を返しません。
使用可能なプラグインのバージョン
- WooCommerce: 2.4.0以降
- WordPress: 4.0以降
サンプルコード
サンプル1: 在庫状況変更時の通知
このサンプルは、商品在庫状況が変更されたときに管理者に通知を送信します。
add_action( 'woocommerce_product_set_stock_status', 'notify_admin_on_stock_change', 10, 2 );
function notify_admin_on_stock_change( $product, $status ) {
$admin_email = get_option( 'admin_email' );
$subject = '在庫状況が変更されました';
$message = '商品名: ' . $product->get_name() . ' の在庫状況が ' . $status . ' に変更されました。';
wp_mail( $admin_email, $subject, $message );
}
引用元: https://wordpress.org/support/article/using-an-action-hook/
サンプル2: 商品ページの在庫表示を変更
このサンプルでは、在庫切れの商品が表示されるごとにカスタムメッセージを追加します。
add_action( 'woocommerce_product_set_stock_status', 'custom_display_message_for_out_of_stock', 10, 2 );
function custom_display_message_for_out_of_stock( $product, $status ) {
if ( 'outofstock' === $status ) {
add_filter( 'woocommerce_get_price_html', function( $price_html ) {
$price_html .= '<p class="out-of-stock-message">この商品は現在在庫切れです。</p>';
return $price_html;
});
}
}
引用元: https://woocommerce.com/document/advanced-variable-product-pricing/
サンプル3: 在庫状況に基づく商品価格の調整
在庫状況の変更に基づいて商品の価格を調整します。
add_action( 'woocommerce_product_set_stock_status', 'adjust_price_on_stock_change', 10, 2 );
function adjust_price_on_stock_change( $product, $status ) {
if ( 'outofstock' === $status ) {
$regular_price = $product->get_regular_price();
$new_price = $regular_price * 0.9; // 価格を10%引き下げ
$product->set_sale_price( $new_price );
$product->save();
}
}
引用元: https://woocommerce.com/document/creating-a-custom-product-type/
サンプル4: 在庫状況の変更に基づくプロモーション
在庫状況が在庫切れに変更された場合に、特定のプロモーションコードを適用します。
add_action( 'woocommerce_product_set_stock_status', 'apply_discount_on_stock_status_change', 10, 2 );
function apply_discount_on_stock_status_change( $product, $status ) {
if ( 'outofstock' === $status ) {
update_option( 'special_discount_code', 'OUTOFSTOCK10' ); // 特別割引コードを設定
}
}
引用元: https://woocommerce.com/document/function-reference/
サンプル5: 外部APIへの通知送信
在庫状況の変更時に、外部APIへデータを送信します。
add_action( 'woocommerce_product_set_stock_status', 'notify_external_api_on_stock_change', 10, 2 );
function notify_external_api_on_stock_change( $product, $status ) {
$api_url = 'https://example.com/api/stock-change';
$args = array(
'body' => json_encode( array(
'product_id' => $product->get_id(),
'status' => $status
) ),
'headers' => array( 'Content-Type' => 'application/json' )
);
wp_remote_post( $api_url, $args );
}
引用元: https://developer.wordpress.org/plugins/http-api/
この関数のアクションでの使用可能性
フック | 使用例 |
---|---|
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 |