WooCommerce 预付款计算错误

Posted

技术标签:

【中文标题】WooCommerce 预付款计算错误【英文标题】:WooCommerce advanced payment calculate error 【发布时间】:2021-08-05 20:31:55 【问题描述】:

我在 *** 中找到了一些代码。这些代码可用于计算管理员编辑订单页面上的预付款。代码运行良好。但是此错误消息显示-“在...中遇到的非数字值”。当预付款字段为空时。

谁能给个解决办法..?

add_action( 'woocommerce_admin_order_item_headers', 'calculate_advance_payment', 10, 1);
add_action( 'woocommerce_admin_order_totals_after_discount', 'vp_add_sub_total', 10, 1);
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
add_action( 'woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field' );

// Output a custom editable field in backend edit order pages under general section
function editable_order_custom_field( $order )            

// Get "Delivery Type" from meta data (not item meta data)
$updated_advance_payment = $order->get_meta('_advance_payment');

// Replace "Delivery Type" value by the meta data if it exist
$advancePayment = $updated_advance_payment ? $updated_advance_payment : ( isset($item_value) ? $item_value : '');

// Display the custom editable field
woocommerce_wp_text_input( 
    array(
        'id'            => 'advance_payment',
        'label'         => __("Advance Payment:", "woocommerce"),
        'value'         => $advancePayment,
        'wrapper_class' => 'form-field-wide',
    )
   );


// Save the custom editable field value as order meta data and update order item meta data
function save_order_custom_field_meta_data( $post_id, $post )
if( isset( $_POST[ 'advance_payment' ] ))
    update_post_meta( $post_id, '_advance_payment', sanitize_text_field( $_POST[ 'advance_payment' ] ) );

    // Update the existing item meta data
    if( isset( $_POST[ 'item_id_ref' ] ) )
        wc_update_order_item_meta( $_POST[ 'item_id_ref' ], 'Advance Payment', $_POST[ 'advance_payment' ] );
    



function calculate_advance_payment( $the_order ) 
$getTotal = $the_order->get_total();
$updateTotal = $getTotal - get_post_meta($the_order->get_id(), "_advance_payment", true);
$the_order->set_total($updateTotal);
$the_order->save();


function vp_add_sub_total( $the_order ) 
global $post, $the_order; ?>
<tr>
<td class="label">Advance Payment:</td>
<td ></td>
<td class="total"><?php echo wc_price(get_post_meta($post->ID, "_advance_payment", true));?></td>
</tr><?php

这一行特别说明了问题-

$updateTotal = $getTotal - get_post_meta($the_order->get_id(), "_advance_payment", true);

Post original source

【问题讨论】:

【参考方案1】:

我实际上在原帖中回答了这个问题。这是因为数值在字符串中。我在这段代码中遇到了同样的问题。添加到字符串中的数值时,应使用float(int) 调用。以下是我对该问题的解决方案,它还可以防止该函数多次添加值。包含“if( get_post_meta( $order_id, 'advance_payment', true ) ) return; // Exit if already processed”将阻止calculate_advance_payment()函数在每次页面加载和添加多个条目时运行。

add_action( 'woocommerce_admin_order_item_headers', 'calculate_advance_payment', 10, 1);
add_action( 'woocommerce_admin_order_totals_after_discount', 'vp_add_sub_total', 10, 1);
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
add_action( 'woocommerce_admin_order_data_after_order_details', 'editable_order_custom_field' );

// Output a custom editable field in backend edit order pages under general section
function editable_order_custom_field( $order )            
    
    // Get "Delivery Type" from meta data (not item meta data)
    $updated_advance_payment = $order->get_meta('advance_payment');

    // Replace "Delivery Type" value by the meta data if it exist
    $advanced_payment = $updated_advance_payment ? $updated_advance_payment : ( isset($item_value) ? $item_value : '');

    // Display the custom editable field
    woocommerce_wp_text_input( 
        array(
            'id'            => 'advance_payment',
            'label'         => __("Advance Payment:", "woocommerce"),
            'value'         => $advanced_payment,
            'wrapper_class' => 'form-field-wide',
        )
    );


// Save the custom editable field value as order meta data and update order item meta data  
function save_order_custom_field_meta_data( $post_id, $post )
    //global $order;
    if( isset( $_POST[ 'advance_payment' ] ))
        update_post_meta( $post_id, 'advance_payment', sanitize_text_field( $_POST[ 'advance_payment' ] ) );
        
        // Update the existing item meta data
        if( isset( $_POST[ 'item_id_ref' ] ) )
            wc_update_order_item_meta( $_POST[ 'item_id_ref' ], 'Advance Payment', $_POST[ 'advance_payment' ] );
        
    


function calculate_advance_payment( $order ) 
    $order_id = $order->get_id();
    $getTotal = $order->get_total();
    $advanced_payment = get_post_meta($order_id, 'advance_payment', true);
    $order_total = (float)$getTotal - (float)$advanced_payment;
    $order->set_total( $order_total );
    // Checking if this has already been done avoiding reload
    if( get_post_meta( $order_id, 'advance_payment', true ) ) 
       return; // Exit if already processed
    $order->save();
    


function vp_add_sub_total( $order ) 
    global $post, $order; 
?>
    <tr>
    <td class="label">Advance Payment:</td>
    <td ></td>
    <td class="total"><?php echo wc_price(get_post_meta($post->ID, "advance_payment", true));?></td>
    </tr><?php


My reply to original post

【讨论】:

非常感谢。我很忙。很抱歉迟到的重播。你的回答被接受了。再次感谢。

以上是关于WooCommerce 预付款计算错误的主要内容,如果未能解决你的问题,请参考以下文章

根据 WooCommerce 中的自定义结帐单选按钮和文本字段设置动态费用

Woocommerce:当我下订单以使用银行 API 处理付款时出现运行时错误

woocommerce paypal 高级身份验证错误

Woocommerce 订单在创建后获得付款

WooCommerce 中的“验证错误:PayPal 货币不匹配”

在 WooCommerce 3+ 中将已付款订单标记为“已完成”,将未付款订单标记为“已取消”[重复]