/*
 Theme Name:   XStore Child
 Theme URI:    http://8theme.com/
 Description:  XStore Child Theme
 Author:       8theme
 Author URI:   http://8theme.com
 Template:     xstore
 Version:      0.0.1
 Text Domain:  xstore-child
*/

/**
 * Add phone number to to Delivery table header.
 */
function iconic_admin_add_phone_heading() {
	?>
	<th scope="col"><?php echo esc_html__( 'Phone', 'jckwds' ); ?></th>
	<?php
}

/**
 * Add phone number to to Delivery table body.
 *
 * @param Object $reservation Reservation object.
 */
function iconic_admin_add_phone_body( $reservation ) {
	$order = wc_get_order( $reservation->order_id );
	?>
	<td data-colname="<?php echo esc_html__( 'Phone', 'jckwds' ); ?>">
		<?php echo esc_html( $order->get_billing_phone() ); ?>
	</td>
	<?php
}

add_action( 'iconic_wds_admin_deliveries_table_heading', 'iconic_admin_add_phone_heading' );
add_action( 'iconic_wds_admin_deliveries_table_body_cell', 'iconic_admin_add_phone_body'


/**
 * Adds Delivery date and time slot to the "Shipping Method" section of invoices and packing lists
 * 'Show shipping method' should be enabled for invoices!
 *
 * @param string    $shipping      the shipping method text
 * @param string    $document_type the type of document being viewed
 * @param \WC_Order $order         the order object the document is for
 *
 * @return string the updated shipping string
 */
function iconic_pip_add_delivery_date_shipping( $shipping, $document_type, $order ) {
	// if you want to only add this to invoices, you can add another check for document type
	// if ( 'invoice' !== $document_type ) { 
		return $shipping; 
	}

	// bail if Delivery Slots plugin is not active
	if ( ! class_exists( 'Iconic_WDS' ) ) {
		return $shipping;
	}

	$order_id  = is_callable( array( $order, 'get_id' ) ) ? $order->get_id() : $order->id;
	$date      = get_post_meta( $order_id, 'jckwds_date', true );
	$time_slot = get_post_meta( $order_id, 'jckwds_timeslot', true );

	if ( $date ) {
		$shipping .= '<p>';
		$shipping .= sprintf( __( 'We will try our best to deliver your order on: %s', 'iconic' ), $date );

		if ( $time_slot ) {
			$shipping .= sprintf( ' %s %s', __( 'at', 'iconic' ), $time_slot );
		}

		$shipping .= '</p>';
	}

	return $shipping;
}

add_filter( 'wc_pip_document_shipping_method', 'iconic_pip_add_delivery_date_shipping', 10, 3 );