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
<?php
if( ! class_exists( 'prdd_rescheduled_order_class' ) ) {
class prdd_rescheduled_order_class {
function __construct() {
add_filter( 'woocommerce_hidden_order_itemmeta', array( &$this, 'prdd_rescheduled_hidden_order_itemmeta'), 10, 1 );
add_action( 'woocommerce_after_order_itemmeta', array( &$this, 'prdd_button_after_order_meta' ), 10, 3 );
}
public function prdd_rescheduled_hidden_order_itemmeta( $meta_keys ) {
$meta_keys[] = '_prdd_resch_orig_order_id';
$meta_keys[] = '_prdd_resch_rem_bal_order_id';
return $meta_keys;
}
public static function prdd_rescheduled_create_order( $original_order_id, $item ) {
$original_order = wc_get_order( $original_order_id );
$new_remaining_order = wc_create_order( array(
'status' => 'wc-pending',
'customer_id' => $original_order->get_user_id(),
));
$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(),
) );
$item_id = $new_remaining_order->add_product( $item['product'], $item['qty'], array(
'totals' => array(
'subtotal' => $item['amount'],
'total' => $item['amount']
)
));
wc_update_order_item_meta( $item_id, '_prdd_resch_orig_order_id', $original_order_id, '' );
wc_update_order_item( $item_id, array( 'order_item_name' => sprintf( __( 'Additional Payment for %s (Order #%d )' ) ,$item['product']->get_title(), $original_order_id ) ) );
$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', current_time( 'timestamp' ) ),
'post_parent' => $original_order_id
);
wp_update_post( $new_remaining_order_post );
return $new_remaining_order->get_id();
}
public function prdd_button_after_order_meta( $item_id, $item, $product ){
if ( $item[ '_prdd_resch_rem_bal_order_id' ] !== '' && $item[ '_prdd_resch_rem_bal_order_id' ] !== null ){
?>
<a href="<?php echo esc_url( admin_url( 'post.php?post=' . $item['_prdd_resch_rem_bal_order_id'] . '&action=edit' ) ); ?>" class="button button-small">
<?php _e( 'Related Order', 'woocommerce-prdd' ); ?>
</a>
<?php
} elseif ( $item['_prdd_resch_orig_order_id'] !== '' && $item['_prdd_resch_orig_order_id'] !== null ) {
?>
<a href="<?php echo esc_url( admin_url( 'post.php?post=' . $item['_prdd_resch_orig_order_id'] . '&action=edit' ) ); ?>" class="button button-small">
<?php _e( 'Parent Order', 'woocommerce-prdd' ); ?>
</a>
<?php
}
}
}
}
$prdd_rescheduled_order_class = new prdd_rescheduled_order_class();