基于WooCommerce产品的当前库存值的自定义库存字段计算

海梅·马托斯(Jaime Matos)

我正在努力改善库存管理,并直接从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 );
}

预先感谢您的关注和建议。

7uc1f3r

您正在使用并已改编的代码比您的问题要广泛得多,因此下面是简化版本。

确实可以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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

基于Woocommerce产品变体库存数据的自定义显示复选框

Rails更新产品的库存值

如何在WooCommerce设置的“产品库存”标签中添加自定义字段?

如何在WooCommerce中为自定义产品类型启用价格和库存

在WooCommerce自定义产品类型上显示库存状态

在WooCommerce中通过减少自定义库存数量来使“缺货”产品变型变灰

如何在WooCommerce 4+中为产品添加自定义库存状态

根据WooCommerce产品自定义库存状态禁用添加到购物车按钮

如果未在WooCommerce中启用“管理库存”,则显示自定义库存消息

在Woocommerce中查找并显示产品自定义字段值

计算产品库存

Bukkit自定义库存产卵

针对每种产品口味使用不同的Android SDK(库存和自定义)

在 WooCommerce 中显示基于自定义表的自定义字段值

将产品变体的自定义字段值显示到WooCommerce中的“自定义产品”选项卡

WordPress/WooCommerce 按自定义日期字段过滤并排除早于当前日期的产品

基于Woocommerce中产品类别的条件自定义结帐字段

woocommerce产品的低库存通知

Woocommerce产品查询库存状态

从 Woocommerce 结帐中的自定义产品字段值中填写选择字段选项

WooCommerce购物车费用根据“自定义产品”输入字段计算

库存产品的计算和转换

在Woocommerce上通过简码有条件地显示自定义库存数量

根据自定义产品属性值过滤Woocommerce产品

将产品自定义字段值保存为WooCommerce中的自定义订单项元

基于 Woocommerce 中的维度自定义字段的自定义购物车项目价格计算

避免在Woocommerce中丢失管理产品自定义字段中的值

在Woocommerce管理员电子邮件通知中显示产品自定义字段值

从WooCommerce中的变化产品获取自定义字段值和最高价格