PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 8, 2022

[FIXED] How to calculate Custom order total in WooCommerce edit order?

 January 08, 2022     woocommerce, wordpress     No comments   

Issue

Currently, I'm working on a WooCommerce (5.2.2) project. My client wants to create a custom input field for Advance payment in the Backend (not for customers) so that when my client received an advance payment from the customer (delivery purpose) they can add manually and order total auto adjust from the backend. So I've wright a code and create a custom field in the post meta table and use that meta table value to calculate the total order, it works but I have to refresh twice to get Order's total new value. here is my code and tell me how to fix it -

add_action( 'manage_shop_order_posts_custom_column' ,  array(&$this,'add_custom_column_content'), 11, 2 );

add_action( 'woocommerce_admin_order_totals_after_discount', array(&$this, 'vp_add_sub_total'), 10, 1);

add_action( 'woocommerce_process_shop_order_meta', array(&$this, 'save_order_custom_field_meta_data'), 12, 2 );


// 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' ] );
                }
            }
        }

//Display Advance Payment and calculate
        function vp_add_sub_total( $the_order ) {
            global $post, $the_order;
            if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
                $the_order = wc_get_order( $post->ID );
            }
            ?>
            
            <tr>
            <td class="label">Advance Payment:</td>
            <td width="1%"></td>
            <td class="total"><?php echo wc_price(get_post_meta($post->ID, "_advance_payment", true));?></td>
            </tr>
            
            <?php

            $getTotal = $the_order->get_total();

            $updateTotal = $getTotal - get_post_meta($post->ID, "_advance_payment", true);

            $the_order->set_total($updateTotal);

            $the_order->save();

        }

Note : I create a small plugin for this advance payment.

Video link for reference : https://www.awesomescreenshot.com/video/3589010?key=b26b5951753bfdc8a969b53f526a36d1


Solution

You need to calculate Advance Payment before display. you can use woocommerce_admin_order_item_headers action hook.

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 width="1%"></td>
    <td class="total"><?php echo wc_price(get_post_meta($post->ID, "_advance_payment", true));?></td>
    </tr><?php
}


Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing