[英]Custom stock field calculation based on current stock value of WooCommerce Product
我正在努力改善我的庫存管理,並直接從 WooCommerce 開始(這樣就沒有必要使用外部 CSV 文件計算)
我需要知道每種產品需要訂購多少(缺貨)。 為了確定這一點,我使用_stock原生值和 2 個自定義字段進行計算:
_missing_stock :缺少的庫存數量/我需要訂購多少庫存
_ref_stock :參考庫存編號(手動設置,倉庫中所需的庫存數量)
所以:缺失庫存 = 參考庫存 - 庫存價值
目的是根據已設置的參考庫存和產品的當前庫存自動計算和更新缺失庫存值。 由於產品庫存隨着購買而降低,因此我可以快速查看每種產品需要訂購多少庫存。
基於基於Woocommerce 3 答案代碼中的 2 個自定義字段的產品正常價格計算,我已經設法創建了自定義字段並到目前為止。
但是我無法弄清楚如何最終自動更新 _missing_stock 值。
這是我設法提出的代碼,自然(並且可能)不完全正確。
// Adding and displaying additional Stock custom fields
add_action( 'woocommerce_product_options_stock_status', 'additional_product_stock_option_fields', 50 );
function additional_product_stock_option_fields() {
$domain = "woocommerce";
global $post;
echo '</div><div class="options_group stock show_if_simple show_if_external show_if_composite">';
woocommerce_wp_text_input( array(
'id' => '_ref_stock',
'label' => __("Reference Stock", $domain ),
'placeholder' => '',
'description' => __("Amount of desired target stock in warehouse )", $domain ),
'desc_tip' => true,
) );
woocommerce_wp_text_input( array(
'id' => '_missing_stock',
'label' => __("Missing Stock", $domain ) . ' ('. get_woocommerce_currency_symbol() . ')',
'placeholder' => '',
'description' => __("Amount of stock that needs to be ordered", $domain ),
'desc_tip' => true,
) );
echo '<input type="hidden" name="_custom_stock_nonce" value="' . wp_create_nonce() . '">';
}
// Utility function that save "Reference Stock" and "missing_stock" custom fields values
function saving_ref_stock_and_missing_stock( $product ) {
// Security check
if ( isset($_POST['_custom_stock_nonce']) && ! wp_verify_nonce($_POST['_custom_stock_nonce']) ) {
return;
}
// Save "Reference Stock" and "missing_stock" custom fields values
if( isset($_POST['_ref_stock']) && isset($_POST['_missing_stock']) ) {
$product->update_meta_data('_ref_stock', sanitize_text_field( (float) $_POST['_ref_stock'] ) );
$product->update_meta_data('_missing_stock', sanitize_text_field( (float) $_POST['_missing_stock'] ) );
}
}
// Utility function: Calculate and save product "missing stock" custom field from the "Reference Stock" custom field and the "Stock" field
function calculate_and_save_new_product_stock( $product ) {
// Check if product stock management is enabled
if ( !$product->managing_stock() ) {
// Calculate and save the missing stock
if( isset($_POST['_ref_stock']) && isset($_POST['_missing_stock'])
&& $_POST['_ref_stock'] > 0 && $_POST['_missing_stock'] > 0 ) {
// Catch the stock data
$ref_stock = (float) $_POST['_ref_stock'];
$missing_stock = (float) $_POST['_missing_stock'];
$current_stock = (float) $product->get_stock_quantity();
// Calculating missing stock
$missing_stock = $ref_stock - $current_stock;
}
}
}
// Saving and calculating Stock values
add_action( 'woocommerce_admin_process_product_object', 'update_product_meta_data', 100, 1 );
function update_product_meta_data( $product ) {
// Saving "Reference Stock" and "missing_stock" custom fields values
saving_ref_stock_and_missing_stock( $product ); // <== To be removed if not used with the first function
// Calculate and save Missing Stock from the "Reference Stock" and the "Stock" custom fields
calculate_and_save_new_product_missing_stock( $product );
}
提前感謝您的關注和建議。
您已經使用並已調整的代碼比您的問題稍微廣泛,因此以下是簡化版本。
當您要在后端保存調整以確定自定義字段的值時,您確實可以使用woocommerce_admin_process_product_object
。
但是,在訂單發生庫存調整后,您的自定義字段的值不會自動調整。
為此,您可以使用woocommerce_product_set_stock
操作掛鈎。
此代碼是為“簡單”產品編寫的,但如果需要,可以很容易地擴展到其他類型的產品。
通過在代碼中添加的注釋標簽進行解釋
// Adding and displaying additional custom fields
function action_woocommerce_product_options_stock_status() {
$domain = 'woocommerce';
echo '</div><div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_ref_stock',
'label' => __( 'Reference Stock', $domain ),
'placeholder' => '',
'description' => __( 'Amount of desired target stock in warehouse', $domain ),
'desc_tip' => true,
));
woocommerce_wp_text_input( array(
'id' => '_missing_stock',
'label' => __( 'Missing Stock', $domain ),
'placeholder' => '',
'description' => __( 'Amount of stock that needs to be ordered', $domain ),
'desc_tip' => true,
'custom_attributes' => array( 'readonly' => 'readonly' ),
));
}
add_action( 'woocommerce_product_options_stock_status', 'action_woocommerce_product_options_stock_status', 10, 1 );
// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset
if ( isset( $_POST['_ref_stock'] ) ) {
// ID & value
$ref_stock_id = '_ref_stock';
$ref_stock_val = sanitize_text_field( $_POST['_ref_stock'] );
// Update ref stock
$product->update_meta_data( $ref_stock_id, $ref_stock_val );
// Get stock quantity
$current_stock = (float) $product->get_stock_quantity();
// NOT empty
if ( ! empty ( $current_stock ) ) {
// ID
$missing_stock_id = '_missing_stock';
// Calculating missing stock
$missing_stock_val = $ref_stock_val - $current_stock;
// Update missing stock
$product->update_meta_data( $missing_stock_id, $missing_stock_val );
}
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// When the stock changed
function action_woocommerce_product_set_stock ( $product_with_stock ) {
global $pagenow;
// Exit
if ( is_admin() && $pagenow === 'post.php' )
return;
// Get meta
$ref_stock_val = $product_with_stock->get_meta( '_ref_stock' );
// NOT empty
if ( ! empty ( $ref_stock_val ) ) {
// Get stock quantity
$current_stock = (float) $product_with_stock->get_stock_quantity();
// NOT empty
if ( ! empty ( $current_stock ) ) {
// ID
$missing_stock_id = '_missing_stock';
// Calculating missing stock
$missing_stock_val = $ref_stock_val - $current_stock;
// Update missing stock
$product_with_stock->update_meta_data( $missing_stock_id, $missing_stock_val );
// Save
$product_with_stock->save();
}
}
}
add_action( 'woocommerce_product_set_stock', 'action_woocommerce_product_set_stock', 10, 1 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.