| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Module\Woocommerce_Addon\Admin; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 |
|
| 7 |
use Booktics\Contracts\Hookable_Service_Contract; |
| 8 |
|
| 9 |
/** |
| 10 |
* Shows the booking behind a WooCommerce order, inside WooCommerce. |
| 11 |
* |
| 12 |
* A store owner working through their orders should not have to open a second |
| 13 |
* dashboard to find out what was actually booked, so the service, date, time |
| 14 |
* and team member appear on the order screen, with a link back to Booktics for |
| 15 |
* anything more. |
| 16 |
* |
| 17 |
* Registered for both order screens: WooCommerce's own orders page when |
| 18 |
* High-Performance Order Storage is on, and the post editor when it is not. |
| 19 |
*/ |
| 20 |
class Order_Metabox implements Hookable_Service_Contract { |
| 21 |
|
| 22 |
/** |
| 23 |
* Register WordPress hooks. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function register() { |
| 28 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ), 30, 2 ); |
| 29 |
|
| 30 |
add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_column' ), 20 ); |
| 31 |
add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'render_column' ), 10, 2 ); |
| 32 |
|
| 33 |
add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_column' ), 20 ); |
| 34 |
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'render_column' ), 10, 2 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Add the booking box to the order screen. |
| 39 |
* |
| 40 |
* @param string $screen_id Current screen id. |
| 41 |
* @param mixed $order Order or post being edited. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function add_meta_box( $screen_id, $order = null ) { |
| 46 |
if ( ! in_array( $screen_id, array( 'woocommerce_page_wc-orders', 'shop_order' ), true ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! $this->booking_of( $order ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
add_meta_box( |
| 55 |
'booktics_order_booking', |
| 56 |
__( 'Booktics booking', 'booktics' ), |
| 57 |
array( $this, 'render_meta_box' ), |
| 58 |
$screen_id, |
| 59 |
'side', |
| 60 |
'high' |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Render the booking details. |
| 66 |
* |
| 67 |
* @param mixed $order Order or post being edited. |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function render_meta_box( $order ) { |
| 72 |
$booking = $this->booking_of( $order ); |
| 73 |
|
| 74 |
if ( ! $booking ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
echo '<ul style="margin:0">'; |
| 79 |
|
| 80 |
foreach ( $booking['rows'] as $label => $value ) { |
| 81 |
printf( |
| 82 |
'<li style="margin-bottom:6px"><strong>%s:</strong> %s</li>', |
| 83 |
esc_html( $label ), |
| 84 |
esc_html( $value ) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
echo '</ul>'; |
| 89 |
|
| 90 |
printf( |
| 91 |
'<p><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a></p>', |
| 92 |
esc_url( admin_url( 'admin.php?page=booktics#/orders' ) ), |
| 93 |
esc_html__( 'Open in Booktics', 'booktics' ) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Add the Booking column to the orders list. |
| 99 |
* |
| 100 |
* @param array $columns Existing columns. |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function add_column( $columns ) { |
| 105 |
$updated = array(); |
| 106 |
|
| 107 |
foreach ( $columns as $key => $label ) { |
| 108 |
$updated[ $key ] = $label; |
| 109 |
|
| 110 |
if ( 'order_status' === $key ) { |
| 111 |
$updated['booktics_booking'] = __( 'Booking', 'booktics' ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! isset( $updated['booktics_booking'] ) ) { |
| 116 |
$updated['booktics_booking'] = __( 'Booking', 'booktics' ); |
| 117 |
} |
| 118 |
|
| 119 |
return $updated; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Render the Booking column. |
| 124 |
* |
| 125 |
* @param string $column Column key. |
| 126 |
* @param mixed $order Order or post id. |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function render_column( $column, $order ) { |
| 131 |
if ( 'booktics_booking' !== $column ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$booking = $this->booking_of( $order ); |
| 136 |
|
| 137 |
if ( ! $booking ) { |
| 138 |
echo '–'; |
| 139 |
|
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
printf( |
| 144 |
'<span class="booktics-order-badge" style="background:#e8f5ee;color:#146c43;padding:2px 8px;border-radius:10px;font-size:11px;">%s</span>', |
| 145 |
esc_html__( 'Booking', 'booktics' ) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Read the booking recorded on an order, if any. |
| 151 |
* |
| 152 |
* @param mixed $order Order object, post object or id. |
| 153 |
* |
| 154 |
* @return array|null |
| 155 |
*/ |
| 156 |
private function booking_of( $order ) { |
| 157 |
if ( ! function_exists( 'wc_get_order' ) ) { |
| 158 |
return null; |
| 159 |
} |
| 160 |
|
| 161 |
if ( $order instanceof \WP_Post ) { |
| 162 |
$order = $order->ID; |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! $order instanceof \WC_Order ) { |
| 166 |
$order = wc_get_order( $order ); |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! $order instanceof \WC_Order ) { |
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
$booktics_order_id = (int) $order->get_meta( '_booktics_order_id' ); |
| 174 |
|
| 175 |
if ( ! $booktics_order_id ) { |
| 176 |
return null; |
| 177 |
} |
| 178 |
|
| 179 |
$rows = array(); |
| 180 |
|
| 181 |
foreach ( $order->get_items() as $item ) { |
| 182 |
if ( ! (int) $item->get_meta( '_booktics_order_id' ) ) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$rows[ __( 'Service', 'booktics' ) ] = $item->get_name(); |
| 187 |
|
| 188 |
foreach ( array( 'Date', 'Time', 'Team member', 'Duration', 'Timezone' ) as $key ) { |
| 189 |
$label = __( $key, 'booktics' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText -- Keys mirror the labels written on the line item. |
| 190 |
$value = $item->get_meta( $label ); |
| 191 |
|
| 192 |
if ( $value ) { |
| 193 |
$rows[ $label ] = $value; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
if ( ! $rows ) { |
| 201 |
return null; |
| 202 |
} |
| 203 |
|
| 204 |
return array( |
| 205 |
'booktics_order_id' => $booktics_order_id, |
| 206 |
'rows' => $rows, |
| 207 |
); |
| 208 |
} |
| 209 |
} |
| 210 |
|