概要
woocommerce_product_stock_status_options
は、WooCommerceで商品の在庫ステータスを管理するためのフィルターフックです。このフィルターを使用することで、デフォルトの在庫ステータスのオプションを追加、変更、削除することができます。これにより、サイトのニーズに応じたカスタマイズが可能になります。
よく使われる機能としては以下のようなものがあります。
- 新しい在庫ステータスの追加
- 特定の条件に基づいた在庫ステータスの変更
- ユーザーの視認性を向上させるためのカスタムラベルの設定
- 商品の管理が容易になるような処理の実装
- プロモーションやキャンペーンに基づく在庫ステータスの変更
- 商品の視覚的な区別を容易にするためのカスタマイズ
- 構文:
apply_filters( 'woocommerce_product_stock_status_options', $options );
- パラメータ:
$options
: 在庫ステータスの配列(デフォルトは「instock」「outofstock」「onbackorder」)
- 戻り値: フィルタリングされた在庫ステータスの配列
- 使用可能なWooCommerceのバージョン: 2.1.0以降
- 使用可能なWordPressのバージョン: 4.1.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_filter( 'woocommerce_product_stock_status_options', 'add_custom_stock_status' );
function add_custom_stock_status( $options ) {
$options['preorder'] = 'Pre-order';
return $options;
}
このサンプルコードは、「Pre-order」という新しい在庫ステータスを追加します。
サンプル2: 在庫ステータスの変更
add_filter( 'woocommerce_product_stock_status_options', 'change_stock_status_label' );
function change_stock_status_label( $options ) {
if ( isset( $options['outofstock'] ) ) {
$options['outofstock'] = 'Sold Out'; // ラベルを変更
}
return $options;
}
このサンプルコードは、「outofstock」のラベルを「Sold Out」に変更します。
サンプル3: 在庫ステータスの削除
add_filter( 'woocommerce_product_stock_status_options', 'remove_stock_status' );
function remove_stock_status( $options ) {
unset( $options['onbackorder'] ); // onbackorderのオプションを削除
return $options;
}
このサンプルコードは、在庫ステータスの「onbackorder」を削除します。
サンプル4: 複数の在庫ステータスの追加
add_filter( 'woocommerce_product_stock_status_options', 'add_multiple_stock_status' );
function add_multiple_stock_status( $options ) {
$options['available_soon'] = 'Available Soon';
$options['limited_stock'] = 'Limited Stock';
return $options;
}
このサンプルコードは、「Available Soon」と「Limited Stock」という2つの新しい在庫ステータスを追加します。
サンプル5: デフォルトの在庫ステータスを変更
add_filter( 'woocommerce_product_stock_status_options', 'modify_default_stock_status' );
function modify_default_stock_status( $options ) {
// デフォルトの在庫ステータスを「outofstock」に変更
$options['instock'] = 'Available';
return $options;
}
このサンプルコードは、デフォルトの在庫ステータス「instock」のラベルを「Available」に変更します。