WooCommerce製品の現在の株価に基づくカスタム在庫フィールドの計算

Nov 29 2020

私は在庫管理を改善し、WooCommerceから直接実行するように努力しています(外部のCSVファイル計算を使用する必要がないように)

各商品をどれだけ注文する必要があるかを知る必要があります(在庫がありません)。これを決定するために、_stockネイティブ値と2つのカスタムフィールドを使用して計算します。

_missing_stock:不足している在庫の量/注文する必要のある在庫の量

_ref_stock:参照在庫番号(手動で設定、倉庫内の必要な在庫量)

so:不足在庫=参照在庫-株価


設定されている参照在庫と製品の現在の在庫に基づいて、不足在庫値を自動的に計算して更新することを目的としています。購入すると商品の在庫が少なくなるので、注文する必要のある各商品の在庫をすばやく確認できます。

基づいて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 );
}

ご清聴ありがとうございました。

回答

1 7uc1f3r Nov 29 2020 at 22:26

すでに使用していて適応させたコードは、質問よりも少し広範であるため、以下は簡略化されたバージョンです。

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 );