問題描述
我想在產品的股票選項下拉列表中添加一個新選項.默認情況下,有缺貨",有貨",我想添加第三個選項.
I would like to add a new option to the dropdown list of stocks options for a product. By default, there is "Out of stock", "In stock" and I would like to add a third option.
我找到了顯示下拉菜單的方法(在 class-wc-meta-box-product-data.php 中)
I found the method that displays the dropdown ( in class-wc-meta-box-product-data.php )
// Stock status
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' )
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
do_action( 'woocommerce_product_options_stock_status' );
但我不想直接編輯 Woocommerce 類,這樣我們就可以在不丟失任何自定義代碼的情況下更新 Woocommerce.有沒有辦法覆蓋這個方法?
But I don't want to edit Woocommerce class directly, so that we can update Woocommerce without losing any custom code. Is there a way to override this method ?
推薦答案
對于任何感興趣的人,這里是完整的解決方案,基于 Laila 的方法.警告!我的解決方案僅適用于 WooCommerce 禁用管理庫存"選項!我沒有處理確切數量的庫存物品.像往常一樣,所有代碼都轉到 functions.php
.
for anyone interested, here is complete solution, based on Laila's approach. Warning! My solution is intended to work only with WooCommerce "manage stock" option disabled! I am not working with exact amounts of items in stock. All code goes to functions.php
, as usual.
刪除本地庫存狀態下拉字段.添加 CSS 類以區分我的新自定義字段.下拉菜單現在有新選項應要求".
Removing native stock status dropdown field. Adding CSS class to distinguish my new custom field. Dropdown has now new option "On Request".
function add_custom_stock_type() {
?>
<script type="text/javascript">
jQuery(function(){
jQuery('._stock_status_field').not('.custom-stock-status').remove();
});
</script>
<?php
woocommerce_wp_select( array( 'id' => '_stock_status', 'wrapper_class' => 'hide_if_variable custom-stock-status', 'label' => __( 'Stock status', 'woocommerce' ), 'options' => array(
'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' ),
'onrequest' => __( 'On Request', 'woocommerce' ), // The new option !!!
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
}
add_action('woocommerce_product_options_stock_status', 'add_custom_stock_type');
遺憾的是,WooCommerce 將僅通過其原生功能保存有貨"或缺貨"值.所以在所有產品數據處理之后,我必須再次重新保存我的庫存狀態.
Sadly, WooCommerce will save only "instock" or "outofstock" values with its native functions. So after all product data processing, I have to re-save my stock status again.
function save_custom_stock_status( $product_id ) {
update_post_meta( $product_id, '_stock_status', wc_clean( $_POST['_stock_status'] ) );
}
add_action('woocommerce_process_product_meta', 'save_custom_stock_status',99,1);
模板部分
最后一件事 - 我必須更改產品 get_availability()
函數返回的數據.當管理庫存"關閉時,WooCommerce 再次只知道instock"和outofstock"值.所以我自己檢查庫存狀態.
Template part
And the last thing - I have to alter data returned by product get_availability()
function. When "managing stock" is off, WooCommerce only knows "instock" and "outofstock" values, again. So I have check stock status on my own.
function woocommerce_get_custom_availability( $data, $product ) {
switch( $product->stock_status ) {
case 'instock':
$data = array( 'availability' => __( 'In stock', 'woocommerce' ), 'class' => 'in-stock' );
break;
case 'outofstock':
$data = array( 'availability' => __( 'Out of stock', 'woocommerce' ), 'class' => 'out-of-stock' );
break;
case 'onrequest':
$data = array( 'availability' => __( 'On request', 'woocommerce' ), 'class' => 'on-request' );
break;
}
return $data;
}
add_action('woocommerce_get_availability', 'woocommerce_get_custom_availability', 10, 2);
也許它不是萬無一失的解決方案......我最終會更新它.
Maybe it's not bulletproof solution ... I will update it, eventually.
這篇關于在 WooCommerce 中添加自定義庫存狀態的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!