Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hook Reference
  • Docs Home

Packages

  • Deposits-for-WooCommerce
    • Bookings
    • Cart
    • Order
    • Other
    • Payment-Plans
      • Views
    • Products
    • Settings

Classes

  • DFW_Deposits
  • DFW_Deposits_For_Bookings
  • DFW_Deposits_Plan
  • DFW_Deposits_Settings
  • DFW_License
  • DFW_Manage_Cart
  • DFW_Manage_Order_Items
  • DFW_Manage_Orders
  • DFW_Manage_Plans
  • DFW_Manage_Products
  • DFW_Payment_Plans_List
  • DFW_Plans_Admin_Settings
  • DFW_Product_Admin_Settings
  • DFW_Scheduled_Plan_Orders
  • DFW_Update_DB
  • EDD_DEPOSITS_Plugin_Updater

Functions

  • dfw_update_po_file
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 
<?php
/**
 * Manage all the orders data 
 * 
 * @author Tyche Softwares
 * @since 1.0
 * @category Classes
 * @package Deposits-for-WooCommerce/Order
 */

class DFW_Manage_Orders {

    /**
     * Constructor function
     * @since 1.o
     */
    public function __construct(){
        add_action( 'init'                                    , array( &$this, 'dfw_register_post_status' ) );
        add_filter( 'wc_order_statuses'                       , array( &$this, 'dfw_add_order_status') );
        add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', array( &$this, 'dfw_valid_order_statuses') );
        add_filter( 'woocommerce_payment_complete_order_status', array( &$this, 'dfw_payment_complete_order_status' ),10,2 );
        add_action( 'woocommerce_order_status_processing'     , array( &$this, 'dfw_process_deposits_order' ), 10, 1 );
        add_action( 'woocommerce_order_status_completed'      , array( &$this, 'dfw_process_deposits_order' ), 10, 1 );
        add_action( 'woocommerce_order_status_partial-payment', array( &$this, 'dfw_process_deposits_order' ), 10, 1 );        
        add_action( 'woocommerce_order_status_on-hold'        , array( &$this, 'dfw_process_deposits_order' ), 10, 1 );
        add_action( 'woocommerce_order_status_pending'        , array( &$this, 'dfw_process_deposits_order' ), 10, 1 );
        add_filter( 'woocommerce_attribute_label'             , array( &$this, 'dfw_attribute_label' ), 10, 2 );
        add_filter( 'woocommerce_hidden_order_itemmeta'       , array( &$this, 'dfw_hidden_order_itemmeta' ) );
        add_action( 'woocommerce_before_order_itemmeta'       , array( &$this, 'dfw_before_order_itemmeta' ), 10, 3 );
        add_action( 'woocommerce_after_order_itemmeta'        , array( &$this, 'dfw_after_order_itemmeta' ), 10,3 ); 
        add_action( 'admin_init'                              , array( $this , 'dfw_order_init_action' ) );
        add_action( 'woocommerce_order_item_meta_end'         , array( &$this, 'dfw_order_item_meta_end' ), 10, 3 );
        add_filter( 'woocommerce_get_order_item_totals'       , array( &$this, 'dfw_get_order_item_totals' ), 10, 2 );
        add_filter( 'request'                                 , array( &$this, 'dfw_request_query' ) );
        add_action( 'woocommerce_order_status_cancelled'      , array( &$this, 'dfw_check_cancelled_order' ), 10, 1 );
        add_filter( 'woocommerce_order_item_name'             , array( &$this, 'dfw_order_item_name'), 10, 2 );
    }


    /**
     * Change the label of Order item meta keys 
     * 
     * @hook woocommerce_attribute_label
     * 
     * @param string $label Label for the meta
     * @param string $meta_key Order item meta key
     * @return string 
     * @since 1.0
     */

    public function dfw_attribute_label( $label, $meta_key ) {
        switch( $meta_key ) {
            case '_full_amount':
                $label = __('Full Amount', 'deposits-for-woocommerce' );
        }

        return $label;
    }



    /**
     * Meta keys which will be hidden under order item 
     * 
     * @hook woocommerce_hidden_order_itemmeta
     * 
     * @param array $meta_keys Order Item Hidden Meta keys
     * @return array $meta_keys
     * @since 1.0
     */
    public function dfw_hidden_order_itemmeta( $meta_keys ) {
        $meta_keys[] = '_has_deposit';
        $meta_keys[] = '_remaining_balance_order_id';
        $meta_keys[] = '_remaining_balance_paid';
        $meta_keys[] = '_original_order_id';
        $meta_keys[] = '_payment_plan';
        $meta_keys[] = '_scheduled_plan';

        return $meta_keys;
    }

    /**
     * Check if the order has deposit enabled
     * 
     * @param int $order Order ID
     * @return bool
     * @since 1.0
     */
    public static function dfw_has_deposit( $order ) {
        if ( is_numeric( $order ) ) {
            $order = wc_get_order( $order );
        }
        foreach( $order->get_items() as $item ) {
            if ( 'line_item' === $item['type'] && ! empty( $item['is_deposit'] ) ) {
                return true;
            }
        }
        return false;
    }

    /**
     * Register a new order status for payment plans
     * 
     * @since 1.0
     */
    public function dfw_register_post_status() {

        register_post_status( 'wc-partial-payment', array(
            'label'                     => _x( 'Partially Paid', 'Order status', 'deposits-for-woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true
        ));

        register_post_status( 'wc-scheduled', array(
            'label'                     => _x( 'Scheduled', 'Order status', 'deposits-for-woocommerce' ),
            'public'                    => false,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => false,
            'show_in_admin_status_list' => true
        ));
    }

    /**
     * Add order status to WooCommerce
     * 
     * @hook wc_order_statuses
     * 
     * @param array $order_statuses Array with all order statuses
     * @return array 
     */
    public function dfw_add_order_status( $order_statuses ){
        $order_statuses['wc-partial-payment'] = _x( 'Partially Paid', 'Order status', 'deposits-for-woocommerce' );
        $order_statuses['wc-scheduled']       = _x( 'Scheduled', 'Order status', 'deposits-for-woocommerce' );
        return $order_statuses;
    }

    /**
     * Statuses that can be completed
     * 
     * @hook woocommerce_valid_order_statuses_for_payment_complete
     * 
     * @param array $statuses Array with all valid order statuses
     * @return array
     * @since 1.0
     */
    public function dfw_valid_order_statuses( $statuses ) {
        $statuses = array_merge( $statuses, array( 'wc-partial-payment', 'wc-scheduled' ) );
        return $statuses;
    }

    /**
     * Complete order status
     * 
     * @hook woocommerce_payment_complete_order_status
     * 
     * @param array $status Order status
     * @param int $order_id Order ID
     * @return array
     * @since 1.0
     */
    public function dfw_payment_complete_order_status( $status, $order_id ){
        $order = wc_get_order( $order_id );
        if( self::dfw_has_deposit( $order ) ) {
            $status = 'partial-payment';
        }

        return $status;
    }

    
    /**
     * Display 'Deposit' or Plan name after the item name based on the deposit type on order page
     * 
     * @hook woocommerce_before_order_itemmeta
     * 
     * @param int $item_id Order Item Id
     * @param $item Order item object
     * @param int $product Product Id
     * @globals mixed $wpdb
     * @since 1.0
     */

    public function dfw_before_order_itemmeta( $item_id, $item, $product ){
        global $wpdb;

        if( ! DFW_Manage_Order_Items::dfw_is_deposit( $item ) ){
            return;
        }

        if( $payment_plan = DFW_Manage_Order_Items::dfw_get_payment_plan( $item ) ) {
            echo ' (' . $payment_plan->get_plan_name() . ')';  
        }else {
            echo ' (' . __( 'Deposit', 'deposits-for-woocommerce' ) . ')';
        }
    }


    /**
     * Get the Child orders in a specific plan
     * 
     * @param array $vars Array containing post data
     * @return array
     * @since 1.0
     */
    public function dfw_request_query( $vars ) {
        global $typenow, $wp_query, $wp_post_statuses;

        if ( 'shop_order' === $typenow && isset( $_GET['post_parent'] ) && $_GET['post_parent'] > 0 ) {
                $vars['post_parent'] = absint( $_GET['post_parent'] );
        }

        return $vars;
    }


    /**
     * Create payment plans subsequent orders
     * 
     * @param int $order_id Order Id
     * @since 1.0
     */
    public function dfw_process_deposits_order( $order_id ) {
        $order      = wc_get_order( $order_id );
        $parent_id  = wp_get_post_parent_id( $order_id );

        foreach ( $order->get_items() as $id => $item ) {
            if( 'line_item' === $item['type'] && !empty( $item['payment_plan'] ) && empty( $item['scheduled_plan'] ) ){

                $plan = new DFW_Deposits_Plan( absint( $item['payment_plan'] ) ); 
                DFW_Scheduled_Plan_Orders::create_orders_for_plans( $plan, $order_id, array(
                            'product' => $item->get_product(),
                            'qty'     => $item['qty'],
                            'price'   => $item['full_amount']

                ) );

                wc_add_order_item_meta( $id, '_scheduled_plan', 'yes' );
            }
        }

        if( $parent_id ) {
            $parent_order = wc_get_order( $parent_id );

            if( $parent_order && $parent_order->has_status( 'partial-payment' ) ) {
                $amount_paid = true;

                foreach ( $parent_order->get_items() as $order_item_id => $item ) {
                    if( DFW_Manage_Order_Items::dfw_is_deposit( $item ) && ! DFW_Manage_Order_Items:: dfw_check_fully_paid( $item, $parent_order ) ) {
                        $amount_paid = false;
                        break;
                    }   
                }

                if( $amount_paid ) {
                    $parent_order->update_status( 'completed', 'deposits-for-woocommerce' );
                }
            }
        }
    }


    /**
     * Cancel child orders if parent order is cancelled
     * 
     * @param int $order_id Parent Order ID
     * @since 1.0
     */
    public function dfw_check_cancelled_order( $order_id ) {
        $order        = wc_get_order( $order_id );
        $child_orders = wc_get_orders( array( 'post_parent'=>$order_id ) );

        foreach ( $child_orders as $child ) {
            if( ! $child->has_status( 'cancelled' ) ) {
                $child->update_status( 'cancelled', 'deposits-for-woocommerce' );
            }
        }
    }


    /**
     * Show information after order item meta
     * 
     * @hook woocommerce_after_order_itemmeta
     * 
     * @param int $item_id Order Item Id
     * @param mixed $item Order Item object
     * @param int $product Product ID
     * @globals $wpdb;
     * @since 1.0
     */
    public function dfw_after_order_itemmeta( $item_id, $item, $product ) {
        global $wpdb;

        if( DFW_Manage_Order_Items::dfw_is_deposit( $item ) ) {
            $order_id          = $wpdb->get_var( $wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) );
            $order             = wc_get_order( $order_id );

            if( $payment_plan = DFW_Manage_Order_Items::dfw_get_payment_plan( $item ) ) {
                echo '<a href="' . admin_url( 'edit.php?post_status=wc-scheduled-payment&post_type=shop_order&post_parent=' . $order_id ) . '" target="_blank" class="button button-small">' . __( 'View Scheduled Payments', 'deposits-for-woocommerce' ) . '</a>';
            } else {

                $remaining_balance = $item[ 'full_amount' ] - $item[ 'line_total' ];
                $remaining_balance_order_id = ! empty( $item['remaining_balance_order_id'] ) ? absint( $item['remaining_balance_order_id'] ) : 0;  
                $remaining_balance_paid =  ! empty( $item['remaining_balance_paid'] );  

                if( $remaining_balance_order_id && ( $remaining_balance_order = wc_get_order( $remaining_balance_order_id) ) ) {
                    echo '<a href="' . admin_url( 'post.php?post=' . absint( $remaining_balance_order_id ) . '&action=edit' ) . '" target="_blank" class="button button-small">' . sprintf( __( 'Remainder - Invoice #%1$s', 'deposits-for-woocommerce' ), $remaining_balance_order->get_order_number() ) . '</a>';
                } elseif( $remaining_balance_paid ) {
                    printf( __( 'The remaining balance of %s (plus tax) for this item was paid offline.', 'deposits-for-woocommerce' ), wc_price( $remaining_balance, array( 'currency' => $order->get_currency() ) ) );
                    echo ' <a href="' . esc_url( wp_nonce_url( add_query_arg( array( 'mark_deposit_unpaid' => $item_id ) ), 'mark_deposit_unpaid', 'mark_deposit_unpaid_nonce' ) ) . '" class="button button-small">' . sprintf( __( 'Unmark as Paid', 'deposits-for-woocommerce' ) ) . '</a>';
                
                } else {
                    ?>
                    <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'invoice_remaining_balance' => $item_id ), admin_url( 'post.php?post=' . $order_id . '&action=edit' ) ), 'invoice_remaining_balance', 'invoice_remaining_balance_nonce' ) ); ?>" class="button button-small"><?php _e( 'Invoice Remaining Balance', 'deposits-for-woocommerce' ); ?></a>

                    <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'mark_deposit_fully_paid' => $item_id ), admin_url( 'post.php?post=' . $order_id . '&action=edit' ) ), 'mark_deposit_fully_paid', 'mark_deposit_fully_paid_nonce') ); ?>" class="button button-small"><?php printf( __( 'Mark Paid (offline)', 'deposits-for-woocommerce' ) ); ?></a>
                    <?php
                }
            } 
        
        } elseif( ! empty( $item['original_order_id'] ) ) {
               
                echo '<a href="' . admin_url( 'post.php?post=' . absint( $item['original_order_id'] ) . '&action=edit' ) . '" target="_blank" class="button button-small">' . __( 'View Original Order', 'deposits-for-woocommerce' ) . '</a>';
        }         
    }

    /**
     * Create new orders for deposits
     * 
     * @param $payment_date
     * @param int $original_order_id Order Id of the original order
     * @param int $payment_number Payment Number
     * @param mixed $item Order item object
     * @param string $status Order status 
     * @since 1.0
     */
    public static function dfw_create_order( $payment_date, $original_order_id, $payment_number, $item, $status='' ) {
        $original_order = wc_get_order( $original_order_id );
        $new_remaining_order = wc_create_order( array( 
            'status'        => $status,
            'customer_id'   => $original_order->get_user_id(),
            //'customer_note' => $original_order->customer_note,
            //'created_via'   => 'wcdeposits'
        ));

        /* ( is_wp_error( $new_remaining_order ) ) {
            $original_order->add_order_note( sprintf( __( 'Error: Unable to create follow up payment (%s)', 'wc-deposits' ), $scheduled_order->get_error_message() ) );
        } */

        $new_remaining_order->set_address( array(

            'first_name'    => $original_order->get_billing_first_name(),
            'last_name'     => $original_order->get_billing_last_name(),
            'company'       => $original_order->get_billing_company(),
            'address_1'     => $original_order->get_billing_address_1(),
            'address_2'     => $original_order->get_billing_address_2(),
            'city'          => $original_order->get_billing_city(),
            'state'         => $original_order->get_billing_state(),
            'postcode'      => $original_order->get_billing_postcode(),
            'country'       => $original_order->get_billing_country(),
            'email'         => $original_order->get_billing_email(),
            'phone'         => $original_order->get_billing_phone()
        ) );

        $new_remaining_order->set_address( array(

            'first_name'    => $original_order->get_shipping_first_name(),
            'last_name'     => $original_order->get_shipping_last_name(),
            'company'       => $original_order->get_shipping_company(),
            'address_1'     => $original_order->get_shipping_address_1(),
            'address_2'     => $original_order->get_shipping_address_2(),
            'city'          => $original_order->get_shipping_city(),
            'state'         => $original_order->get_shipping_state(),
            'postcode'      => $original_order->get_shipping_postcode(),
            'country'       => $original_order->get_shipping_country(),
            //'email'         => $original_order->get_shipping_email(),
            //'phone'         => $original_order->get_shipping_phone()
        ) );

        $item_id = $new_remaining_order->add_product( $item['product'], $item['qty'], array(
            'totals' => array(
                'subtotal' => $item['amount'],
                'total'    => $item['amount']
            )
        ));

        wc_add_order_item_meta( $item_id, '_original_order_id', $original_order_id );
        wc_update_order_item( $item_id, array( 'order_item_name' => sprintf( __( 'Payment #%d for %s' ) , $payment_number, $item['product']->get_title()  ) ) );

        $new_remaining_order->calculate_totals();

        $new_remaining_order_post = array (
            'ID'          => $new_remaining_order->get_id(),
            'post_date'   => date( 'Y-m-d H:i:s', $payment_date ),
            'post_parent' => $original_order_id
        );

        wp_update_post( $new_remaining_order_post );
        return $new_remaining_order->get_id();
    }


    /**
     * Create some actions 
     * 
     * @globals $wpdb
     * @since 1.0
     */

    public function dfw_order_init_action() {
        global $wpdb;

        $item_id = false;
        $action  = false;

        if ( ! empty( $_GET['mark_deposit_unpaid'] ) && isset( $_GET['mark_deposit_unpaid_nonce'] ) && wp_verify_nonce( $_GET['mark_deposit_unpaid_nonce'], 'mark_deposit_unpaid' ) ) {
            $action  = 'mark_deposit_unpaid';
            $item_id = absint( $_GET['mark_deposit_unpaid'] );
        }

        if ( ! empty( $_GET['mark_deposit_fully_paid'] ) && isset( $_GET['mark_deposit_fully_paid_nonce'] ) && wp_verify_nonce( $_GET['mark_deposit_fully_paid_nonce'], 'mark_deposit_fully_paid' ) ) {
            $action  = 'mark_deposit_fully_paid';
            $item_id = absint( $_GET['mark_deposit_fully_paid'] );
        }
        
        if ( ! empty( $_GET['invoice_remaining_balance'] ) && isset( $_GET['invoice_remaining_balance_nonce'] ) && wp_verify_nonce( $_GET['invoice_remaining_balance_nonce'], 'invoice_remaining_balance' ) ) {
            $action  = 'invoice_remaining_balance';
            $item_id  = absint( $_GET['invoice_remaining_balance'] );
        }

        if ( ! $item_id ) {
            return;
        }

        $order_id  = $wpdb->get_var( $wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) );
        $order     = wc_get_order( $order_id );
        $item      = false;

        foreach ( $order->get_items() as $order_item_id => $order_item ) {
            if ( $item_id === $order_item_id ) {
                $item = $order_item;
            }
        }

        
        switch( $action ) {
            case 'mark_deposit_unpaid' :
                wc_delete_order_item_meta( $item_id, '_remaining_balance_paid', 1, true );
                wp_redirect( admin_url( 'post.php?post=' . absint( $order_id ) . '&action=edit' ) );
                exit;
            case 'mark_deposit_fully_paid' :
                wc_add_order_item_meta( $item_id, '_remaining_balance_paid', 1 );
                wp_redirect( admin_url( 'post.php?post=' . absint( $order_id ) . '&action=edit' ) );
                exit;
            case 'invoice_remaining_balance':
                $new_item = array (
                    'product' => $order->get_product_from_item( $item ),
                    'qty'     => $item['qty'],
                    'amount'  => $item['full_amount'] - $order->get_line_total( $item, false )
                );

                $new_remaining_order_id = $this->dfw_create_order( current_time( 'timestamp' ), $order_id, 2, $new_item );
                wc_add_order_item_meta( $item_id, '_remaining_balance_order_id', $new_remaining_order_id );

                $send_email = WC_Emails::instance();
                $send_email->customer_invoice( wc_get_order( $new_remaining_order_id ) );

                wp_redirect( admin_url( 'post.php?post=' . absint( $new_remaining_order_id ) . '&action=edit' ) );
                exit;
        }
    }


    /**
     * Add meta information on My Account orders 
     * 
     * @param int $item_id Order Item ID
     * @param mixed $item Order item object
     * @param WC_Order $order Order object
     * @since 1.0
     */
    public function dfw_order_item_meta_end( $item_id, $item, $order ) {
        if( DFW_Manage_Order_Items::dfw_is_deposit( $item ) && ! DFW_Manage_Order_Items::dfw_get_payment_plan( $item ) ){
            $remaining                  = $item['full_amount'] - $order->get_line_total( $item, true );
            $remaining_balance_order_id = ! empty( $item['remaining_balance_order_id'] ) ? absint( $item['remaining_balance_order_id'] ) : 0;
            $remaining_balance_paid     = ! empty( $item['remaining_balance_paid'] );

            if ( $remaining_balance_order_id && ( $remaining_balance_order = wc_get_order( $remaining_balance_order_id ) ) ) {
                echo '<p class="wc-deposits-order-item-description"><a href="' . $remaining_balance_order->get_view_order_url() . '">' . sprintf( __( 'Remainder - Invoice #%1$s', 'deposits-for-woocommerce' ), $remaining_balance_order->get_order_number() ) . '</a></p>';
            } elseif( $remaining_balance_paid ) {
                printf( '<p class="wc-deposits-order-item-description">' . __( 'The remaining balance of %s for this item was paid offline.', 'deposits-for-woocommerce' ) . '</p>', wc_price( $remaining, array( 'currency' => $order->get_currency() ) ) );
            }
        }
    } 

    
    /**
     * Add Deposit or Plan Name after the item name
     * 
     * @hook woocommerce_order_item_name
     * 
     * @param string $item_name Order item name
     * @param mixed $item Order item object 
     * @return string
     * @since 1.0
     */
    public function dfw_order_item_name( $item_name, $item ){
        if( DFW_Manage_Order_Items::dfw_is_deposit( $item ) ) {
            if( $payment_plan = DFW_Manage_Order_Items::dfw_get_payment_plan( $item ) ) {
                $item_name .= ' (' . $payment_plan->get_plan_name() . ')';  
            } else {
                $item_name .= ' (' . __( 'Deposit', 'deposits-for-woocommerce' ) . ')';
            }
        }

        return $item_name;
    }


    /**
     * Check if the order has deposit selected
     * 
     * @param int $order Order Id
     * @return bool
     * @since 1.0
     */
    public static function has_deposit( $order ) {
        if ( is_numeric( $order ) ) {
            $order = wc_get_order( $order );
        }
        foreach( $order->get_items() as $item ) {
            if ( 'line_item' === $item['type'] && ! empty( $item['has_deposit'] ) ) {
                return true;
            }
        }
        return false;
    }


    /**
     * Add remaining amount in the order item totals info
     * 
     * @hook woocommerce_get_order_item_totals
     * 
     * @param array $total_rows Order Totals rows
     * @param int $order Order Id
     * @return array
     * @since 1.0
     */
    public function dfw_get_order_item_totals( $total_rows, $order ){
        if( $this->has_deposit( $order ) ){

            $remaining = 0;
            $paid      = 0;

            foreach( $order->get_items() as $item ) {
                if( DFW_Manage_Order_Items::dfw_is_deposit( $item ) ) {
                    if( ! DFW_Manage_Order_Items::dfw_get_payment_plan( $item ) ){
                        $remaining_balance_order_id = ! empty( $item['remaining_balance_order_id'] ) ? absint( $item['remaining_balance_order_id'] ) : 0;
                        $remaining_balance_paid     = ! empty( $item['remaining_balance_paid'] );

                        if ( empty( $remaining_balance_order_id ) && ! $remaining_balance_paid ) {
                            $remaining += $item['full_amount'] - ( $order->get_line_subtotal( $item, true ) + $item['line_tax'] );
                        }
                    }
                }
            }

            if( $remaining ) {
                $total_rows['future'] = array(
                    'label' => __( 'Future Payments', 'deposits-for-woocommerce' ),
                    'value' => wc_price( $remaining )
                );
            }
        } 
        return $total_rows;
    }  
}

$dfw_manage_orders = new DFW_Manage_Orders();
API documentation generated by ApiGen