WooCommerce 4 にカスタム在庫ステータス機能を追加するにはどうすればよいですか?
Nov 02, 2024 pm 07:11 PMWooCommerce にカスタム在庫ステータス機能を追加する方法 4
はじめに
WooCommerce 製品にカスタム在庫ステータスを追加すると、次のことが可能になります。利用可能かどうかについての詳細情報を提供してください。これにより、顧客のショッピング エクスペリエンスが向上し、混乱が軽減されます。
カスタム在庫ステータス オプション
カスタム在庫ステータス機能を実現するには、次の手順に従います。
-
カスタム在庫ステータスの追加:
<code class="php">function filter_woocommerce_product_stock_status_options( $status ) { // Add new statuses $status['pre_order'] = __( 'Pre order', 'woocommerce' ); $status['contact_us'] = __( 'Contact us', 'woocommerce' ); return $status; } add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );</code>
ログイン後にコピー -
在庫状況テキストとクラスの更新:
<code class="php">function filter_woocommerce_get_availability_text( $availability, $product ) { // Get stock status switch( $product->get_stock_status() ) { case 'pre_order': $availability = __( 'Pre order', 'woocommerce' ); break; case 'contact_us': $availability = __( 'Contact us', 'woocommerce' ); break; } return $availability; } add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 ); function filter_woocommerce_get_availability_class( $class, $product ) { // Get stock status switch( $product->get_stock_status() ) { case 'pre_order': $class = 'pre-order'; break; case 'contact_us': $class = 'contact-us'; break; } return $class; } add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );</code>
ログイン後にコピー -
管理製品リストにカスタム ステータスを表示:
<code class="php">function filter_woocommerce_admin_stock_html( $stock_html, $product ) { // Simple if ( $product->is_type( 'simple' ) ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Variable } elseif ( $product->is_type( 'variable' ) ) { foreach( $product->get_visible_children() as $variation_id ) { // Get product $variation = wc_get_product( $variation_id ); // Get stock status $product_stock_status = $variation->get_stock_status(); } } // Stock status switch( $product_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; } add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );</code>
ログイン後にコピー -
フックでカスタム ステータスを使用:
オプション 1: グローバル $product を使用する
<code class="php">function woocommerce_my_callback() { // Get the global product object global $product; // Is a WC product if ( is_a( $product, 'WC_Product' ) ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Use this line during testing, delete afterwards! echo '<p style=&quot;color:red;font-size:20px;&quot;>DEBUG INFORMATION = ' . $product_stock_status . '</p>'; // Compare if ( $product_stock_status == 'My custom stock status' ) { // Etc.. } } } add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_my_callback', 10 ); add_action( 'woocommerce_single_product_summary', 'woocommerce_my_callback', 10 );</code>
ログイン後にコピーオプション 2: $product をコールバックに渡す
<code class="php">function filter_woocommerce_get_price_html( $price, $product ) { // Is a WC product if ( is_a( $product, 'WC_Product' ) ) { // Get stock status $product_stock_status = $product->get_stock_status(); // Use this line during testing, delete afterwards! echo '<p style=&quot;color:red;font-size:20px;&quot;>DEBUG INFORMATION = ' . $product_stock_status . '</p>'; // Compare if ( $product_stock_status == 'My custom stock status' ) { // Etc.. // $price .= ' my text'; } } return $price; } add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );</code>
ログイン後にコピー
結論
WooCommerce にカスタム在庫ステータスを追加すると、顧客に柔軟性と情報が提供されます。これらの手順に従うことで、WooCommerce のデフォルト機能を簡単に拡張し、ユーザーのショッピング エクスペリエンスを向上させることができます。
以上がWooCommerce 4 にカスタム在庫ステータス機能を追加するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

人気の記事
レポ:チームメイトを復活させる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
1週間前
By 尊渡假赌尊渡假赌尊渡假赌
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌

人気の記事
レポ:チームメイトを復活させる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
1週間前
By 尊渡假赌尊渡假赌尊渡假赌
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7283
9


Java チュートリアル
1622
14


CakePHP チュートリアル
1342
46


Laravel チュートリアル
1259
25


PHP チュートリアル
1205
29



LaravelのバックエンドでReactアプリを構築する:パート2、React
