ホームページ >バックエンド開発 >PHPチュートリアル >「予約注文」や「お問い合わせ」などのカスタム在庫ステータスを WooCommerce 製品に追加するにはどうすればよいですか?

「予約注文」や「お問い合わせ」などのカスタム在庫ステータスを WooCommerce 製品に追加するにはどうすればよいですか?

Linda Hamilton
Linda Hamiltonオリジナル
2024-11-02 22:32:291133ブラウズ

How to Add Custom Stock Statuses like “Preorder” and “Contact Us” to WooCommerce Products?

WooCommerce 4 で商品にカスタム在庫ステータスを追加する方法

問題

「予約注文」や「お問い合わせ」などのカスタム在庫ステータス、" が WooCommerce 4 の製品オプションにありません。

解決策

次のコードをfunctions.php ファイルに追加します:

// Add new stock status options
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
function filter_woocommerce_product_stock_status_options( $status ) {
    $status['pre_order'] = __( 'Pre order', 'woocommerce' );
    $status['contact_us'] = __( 'Contact us', 'woocommerce' );
    return $status;
}

// Availability text
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
function filter_woocommerce_get_availability_text( $availability, $product ) {
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $availability = __( 'Pre order', 'woocommerce' );
        break;
        case 'contact_us':
            $availability = __( 'Contact us', 'woocommerce' );
        break;
    }
    return $availability; 
}

// Availability CSS class
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
function filter_woocommerce_get_availability_class( $class, $product ) {
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $class = 'pre-order';
        break;
        case 'contact_us':
            $class = 'contact-us';
        break;
    }
    return $class;
}

// Admin stock html
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
    switch( $product->get_stock_status() ) {
        case 'pre_order':
            $stock_html = '<mark class=&quot;pre-order&quot; style=&quot;background:transparent none;color:#33ccff;font-weight:700;line-height:1;&quot;>' . __( 'Pre order', 'woocommerce' ) . '</mark>';
        break;
        case 'contact_us':
            $stock_html = '<mark class=&quot;contact-us&quot; style=&quot;background:transparent none;color:#cc33ff;font-weight:700;line-height:1;&quot;>' . __( 'Contact us', 'woocommerce' ) . '</mark>';
        break;
    }
    return $stock_html;
}

追加の注意事項:

  • これらの変更は既存の在庫状況には影響しません。
  • 新しい在庫状況は商品ページに表示されます。単一の製品ページ、および管理製品リスト テーブル。
  • 必要に応じて、$product オブジェクトに既にアクセスできるフックでカスタム在庫ステータスを使用できます。

以上が「予約注文」や「お問い合わせ」などのカスタム在庫ステータスを WooCommerce 製品に追加するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。