Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions includes/forms/WC_Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace SCF\Forms;

use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Utilities\OrderUtil;

/**
* Adds ACF metaboxes to the new WooCommerce order screen.
Expand All @@ -22,6 +22,7 @@ class WC_Order {
*/
public function __construct() {
add_action( 'load-woocommerce_page_wc-orders', array( $this, 'initialize' ) );
add_action( 'load-woocommerce_page_wc-orders--shop_subscription', array( $this, 'initialize' ) );
add_action( 'woocommerce_update_order', array( $this, 'save_order' ), 10, 1 );
}

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

$order = ( $post instanceof \WP_Post ) ? wc_get_order( $post->ID ) : $post;
$screen = class_exists( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled()
? wc_get_page_screen_id( 'shop-order' )
: 'shop_order';
$location = 'shop_order';
$order = ( $post instanceof \WP_Post ) ? wc_get_order( $post->ID ) : $post;
$screen = $this->is_hpos_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';

if ( $order instanceof \WC_Subscription ) {
$location = 'shop_subscription';
$screen = function_exists( 'wcs_get_page_screen_id' ) ? wcs_get_page_screen_id( 'shop_subscription' ) : 'shop_subscription';
}

// Get field groups for this screen.
$field_groups = acf_get_field_groups(
array(
'post_id' => $order->get_id(),
'post_type' => 'shop_order',
'post_type' => $location,
)
);

Expand All @@ -70,12 +75,7 @@ public function add_meta_boxes( $post_type, $post ) {
$id = "acf-{$field_group['key']}"; // acf-group_123
$title = $field_group['title']; // Group 1
$context = $field_group['position']; // normal, side, acf_after_title
$priority = 'high'; // high, core, default, low

// Reduce priority for sidebar metaboxes for best position.
if ( 'side' === $context ) {
$priority = 'core';
}
$priority = 'core'; // high, core, default, low

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

/**
* Checks if WooCommerce HPOS is enabled.
*
* @since ACF 6.4.2
*
* @return boolean
*/
public function is_hpos_enabled(): bool {
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
return true;
}

return false;
}

/**
* Saves ACF fields to the current order.
*
Expand All @@ -187,6 +202,10 @@ public function render_meta_box( $post_or_order, $metabox ) {
* @return void
*/
public function save_order( int $order_id ) {
// Bail if not using HPOS to prevent a double-save.
if ( ! $this->is_hpos_enabled() ) {
return;
}
// Remove the action to prevent an infinite loop via $order->save().
remove_action( 'woocommerce_update_order', array( $this, 'save_order' ), 10 );
acf_save_post( 'woo_order_' . $order_id );
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parameters:
- docs
- lang
- wordpress (?)
- includes/forms/WC_Order.php
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of phpstubs that need to be updated before we can include this file.
stellarwp/php-stubs-woocommerce-subscriptions#1

bootstrapFiles:
- vendor/php-stubs/woocommerce-stubs/woocommerce-stubs.php
- bin/phpstan-bootstrap.php
- bin/phpstan-bootstrap.php
Loading