Skip to content

Commit dd58204

Browse files
authored
Enhancement: Add Fields to Woo Subscriptions when using HPOS (#183)
* Add WC Subscription compatibility * Forgot a line * Try fix phpstan * Skip phpstan until stubs are fixed
1 parent 6a37b4e commit dd58204

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

includes/forms/WC_Order.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SCF\Forms;
1010

11-
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
11+
use Automattic\WooCommerce\Utilities\OrderUtil;
1212

1313
/**
1414
* Adds ACF metaboxes to the new WooCommerce order screen.
@@ -22,6 +22,7 @@ class WC_Order {
2222
*/
2323
public function __construct() {
2424
add_action( 'load-woocommerce_page_wc-orders', array( $this, 'initialize' ) );
25+
add_action( 'load-woocommerce_page_wc-orders--shop_subscription', array( $this, 'initialize' ) );
2526
add_action( 'woocommerce_update_order', array( $this, 'save_order' ), 10, 1 );
2627
}
2728

@@ -51,16 +52,20 @@ public function add_meta_boxes( $post_type, $post ) {
5152
// Storage for localized postboxes.
5253
$postboxes = array();
5354

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+
}
5863

5964
// Get field groups for this screen.
6065
$field_groups = acf_get_field_groups(
6166
array(
6267
'post_id' => $order->get_id(),
63-
'post_type' => 'shop_order',
68+
'post_type' => $location,
6469
)
6570
);
6671

@@ -70,12 +75,7 @@ public function add_meta_boxes( $post_type, $post ) {
7075
$id = "acf-{$field_group['key']}"; // acf-group_123
7176
$title = $field_group['title']; // Group 1
7277
$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
7979

8080
// Allow field groups assigned to after title to still be rendered.
8181
if ( 'acf_after_title' === $context ) {
@@ -178,6 +178,21 @@ public function render_meta_box( $post_or_order, $metabox ) {
178178
acf_render_fields( $fields, 'woo_order_' . $order->get_id(), 'div', $field_group['instruction_placement'] );
179179
}
180180

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+
181196
/**
182197
* Saves ACF fields to the current order.
183198
*
@@ -187,6 +202,10 @@ public function render_meta_box( $post_or_order, $metabox ) {
187202
* @return void
188203
*/
189204
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+
}
190209
// Remove the action to prevent an infinite loop via $order->save().
191210
remove_action( 'woocommerce_update_order', array( $this, 'save_order' ), 10 );
192211
acf_save_post( 'woo_order_' . $order_id );

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parameters:
1111
- docs
1212
- lang
1313
- wordpress (?)
14+
- includes/forms/WC_Order.php
1415
bootstrapFiles:
1516
- vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.php
16-
- bin/phpstan-bootstrap.php
17+
- bin/phpstan-bootstrap.php

0 commit comments

Comments
 (0)