概要
woocommerce_variation_set_stock
は、WooCommerceのプラグインで、商品バリエーションの在庫状況を設定する際に実行されるアクションです。このフックは、特定のバリエーションの在庫を更新する場合に使用されます。以下のような場面で特によく使用されます。
- バリエーションの在庫が変更された時の通知を行う。
- 在庫数量の変更に基づいてカスタムロジックを実行する。
- バリエーションの在庫に関連するカスタムフィールドを更新する。
- 外部在庫システムと同期を行う。
- 在庫がゼロになった場合に特定の処理を追加する。
- 在庫が変更されたときにイベントをトリガーする。
構文
do_action('woocommerce_variation_set_stock', $variation);
パラメータ
$variation
: 在庫が設定されるバリエーションのWP_Postオブジェクト。
戻り値
このアクションは戻り値を持たず、アクションフックに登録された関数が実行されます。
使用可能なプラグイン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_variation_set_stock', 'custom_variation_stock_update', 10, 1);
function custom_variation_stock_update($variation) {
$stock_quantity = (int) get_post_meta($variation->ID, '_stock', true);
if ($stock_quantity < 1) {
// 在庫がゼロの場合の処理
update_post_meta($variation->ID, '_is_out_of_stock', true);
}
}
このサンプルコードは、バリエーションの在庫がゼロになったときに、特定のカスタムフィールドを更新します。
引用元: https://developer.wordpress.org/reference/functions/add_action/
サンプルコード 2
add_action('woocommerce_variation_set_stock', 'log_variation_stock_change', 10, 1);
function log_variation_stock_change($variation) {
$stock_quantity = get_post_meta($variation->ID, '_stock', true);
error_log('バリエーションID: ' . $variation->ID . ' の新しい在庫数量: ' . $stock_quantity);
}
このコードは、在庫数量が変更された際に、その情報をエラーログに書き出します。
引用元: https://developer.wordpress.org/reference/functions/add_action/
サンプルコード 3
add_action('woocommerce_variation_set_stock', 'send_stock_update_email', 10, 1);
function send_stock_update_email($variation) {
$stock_quantity = get_post_meta($variation->ID, '_stock', true);
if ($stock_quantity < 5) {
// 在庫が少なくなったらメールを送信
wp_mail('user@example.com', '在庫警告', 'バリエーションID: ' . $variation->ID . ' の在庫が少なくなっています。');
}
}
このコードは、在庫が5未満になった場合に、指定されたメールアドレスに通知を送信します。
引用元: https://developer.wordpress.org/reference/functions/add_action/
サンプルコード 4
add_action('woocommerce_variation_set_stock', 'custom_inventory_sync', 10, 1);
function custom_inventory_sync($variation) {
// 外部APIと在庫を同期
$stock_quantity = get_post_meta($variation->ID, '_stock', true);
$api_url = 'https://externalapi.com/update_stock';
wp_remote_post($api_url, array(
'body' => json_encode(array('variation_id' => $variation->ID, 'stock' => $stock_quantity)),
'headers' => array('Content-Type' => 'application/json')
));
}
このコードは、バリエーションの在庫が変更されたときに、外部APIに在庫の情報を送信します。
引用元: https://developer.wordpress.org/reference/functions/add_action/
サンプルコード 5
add_action('woocommerce_variation_set_stock', 'update_custom_field_on_stock_change', 10, 1);
function update_custom_field_on_stock_change($variation) {
$stock_quantity = (int) get_post_meta($variation->ID, '_stock', true);
update_post_meta($variation->ID, '_last_stock_change', current_time('mysql'));
}
このサンプルコードは、在庫数量が変更されたときに、その変更時刻をカスタムフィールドに保存します。
引用元: https://developer.wordpress.org/reference/functions/add_action/