8
8
9
9
namespace SCF \Forms ;
10
10
11
- use Automattic \WooCommerce \Internal \ DataStores \ Orders \ CustomOrdersTableController ;
11
+ use Automattic \WooCommerce \Utilities \ OrderUtil ;
12
12
13
13
/**
14
14
* Adds ACF metaboxes to the new WooCommerce order screen.
@@ -22,6 +22,7 @@ class WC_Order {
22
22
*/
23
23
public function __construct () {
24
24
add_action ( 'load-woocommerce_page_wc-orders ' , array ( $ this , 'initialize ' ) );
25
+ add_action ( 'load-woocommerce_page_wc-orders--shop_subscription ' , array ( $ this , 'initialize ' ) );
25
26
add_action ( 'woocommerce_update_order ' , array ( $ this , 'save_order ' ), 10 , 1 );
26
27
}
27
28
@@ -51,16 +52,20 @@ public function add_meta_boxes( $post_type, $post ) {
51
52
// Storage for localized postboxes.
52
53
$ postboxes = array ();
53
54
54
- $ order = ( $ post instanceof \WP_Post ) ? wc_get_order ( $ post ->ID ) : $ post ;
55
- $ screen = class_exists ( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController ' ) && wc_get_container ()->get ( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled ()
56
- ? wc_get_page_screen_id ( 'shop-order ' )
57
- : 'shop_order ' ;
55
+ $ location = 'shop_order ' ;
56
+ $ order = ( $ post instanceof \WP_Post ) ? wc_get_order ( $ post ->ID ) : $ post ;
57
+ $ screen = $ this ->is_hpos_enabled () ? wc_get_page_screen_id ( 'shop-order ' ) : 'shop_order ' ;
58
+
59
+ if ( $ order instanceof \WC_Subscription ) {
60
+ $ location = 'shop_subscription ' ;
61
+ $ screen = function_exists ( 'wcs_get_page_screen_id ' ) ? wcs_get_page_screen_id ( 'shop_subscription ' ) : 'shop_subscription ' ;
62
+ }
58
63
59
64
// Get field groups for this screen.
60
65
$ field_groups = acf_get_field_groups (
61
66
array (
62
67
'post_id ' => $ order ->get_id (),
63
- 'post_type ' => ' shop_order ' ,
68
+ 'post_type ' => $ location ,
64
69
)
65
70
);
66
71
@@ -70,12 +75,7 @@ public function add_meta_boxes( $post_type, $post ) {
70
75
$ id = "acf- {$ field_group ['key ' ]}" ; // acf-group_123
71
76
$ title = $ field_group ['title ' ]; // Group 1
72
77
$ context = $ field_group ['position ' ]; // normal, side, acf_after_title
73
- $ priority = 'high ' ; // high, core, default, low
74
-
75
- // Reduce priority for sidebar metaboxes for best position.
76
- if ( 'side ' === $ context ) {
77
- $ priority = 'core ' ;
78
- }
78
+ $ priority = 'core ' ; // high, core, default, low
79
79
80
80
// Allow field groups assigned to after title to still be rendered.
81
81
if ( 'acf_after_title ' === $ context ) {
@@ -178,6 +178,21 @@ public function render_meta_box( $post_or_order, $metabox ) {
178
178
acf_render_fields ( $ fields , 'woo_order_ ' . $ order ->get_id (), 'div ' , $ field_group ['instruction_placement ' ] );
179
179
}
180
180
181
+ /**
182
+ * Checks if WooCommerce HPOS is enabled.
183
+ *
184
+ * @since ACF 6.4.2
185
+ *
186
+ * @return boolean
187
+ */
188
+ public function is_hpos_enabled (): bool {
189
+ if ( class_exists ( '\Automattic\WooCommerce\Utilities\OrderUtil ' ) && OrderUtil::custom_orders_table_usage_is_enabled () ) {
190
+ return true ;
191
+ }
192
+
193
+ return false ;
194
+ }
195
+
181
196
/**
182
197
* Saves ACF fields to the current order.
183
198
*
@@ -187,6 +202,10 @@ public function render_meta_box( $post_or_order, $metabox ) {
187
202
* @return void
188
203
*/
189
204
public function save_order ( int $ order_id ) {
205
+ // Bail if not using HPOS to prevent a double-save.
206
+ if ( ! $ this ->is_hpos_enabled () ) {
207
+ return ;
208
+ }
190
209
// Remove the action to prevent an infinite loop via $order->save().
191
210
remove_action ( 'woocommerce_update_order ' , array ( $ this , 'save_order ' ), 10 );
192
211
acf_save_post ( 'woo_order_ ' . $ order_id );
0 commit comments