Skip to content

Commit

Permalink
Merge pull request #11 from BeAPI/issue/65830
Browse files Browse the repository at this point in the history
issue/65830 : fix the columns in orders list
  • Loading branch information
stephane-gillot authored Feb 7, 2024
2 parents 0546f84 + 7e5623a commit 58353b8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
2 changes: 1 addition & 1 deletion shoppingfeed-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ function init() {
ShoppingFeedAdvanced::get_instance();
}

\add_action( 'plugins_loaded', __NAMESPACE__ . '\\init' );
\add_action( 'init', __NAMESPACE__ . '\\init', 20 );
49 changes: 39 additions & 10 deletions src/Admin/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ShoppingFeed\ShoppingFeedWCAdvanced\Admin;

// Exit on direct access
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

defined( 'ABSPATH' ) || exit;

class Order {
Expand All @@ -23,31 +25,58 @@ public function save_sfa_tracking_details_metabox( $post_id ) {
return;
}

$order = wc_get_order( $post_id );
if ( false === $order ) {
return;
}
if ( isset( $_POST[ TRACKING_NUMBER_FIELD_SLUG ] ) ) {
update_post_meta(
$post_id,
$order->update_meta_data(
TRACKING_NUMBER_FIELD_SLUG,
sanitize_text_field( wp_unslash( $_POST[ TRACKING_NUMBER_FIELD_SLUG ] ) )
);
}
if ( isset( $_POST[ TRACKING_LINK_FIELD_SLUG ] ) ) {
update_post_meta(
$post_id,
$order->update_meta_data(
TRACKING_LINK_FIELD_SLUG,
sanitize_text_field( wp_unslash( $_POST[ TRACKING_LINK_FIELD_SLUG ] ) )
);
}
if ( isset( $_POST[ TRACKING_NUMBER_FIELD_SLUG ] ) || isset( $_POST[ TRACKING_LINK_FIELD_SLUG ] ) ) {
remove_action( 'save_post', array( $this, 'save_sfa_tracking_details_metabox' ) );
$order->save();
add_action( 'save_post', array( $this, 'save_sfa_tracking_details_metabox' ) );
}
}

public function register_sfa_tracking_details_metabox() {

global $post;
$screen = get_current_screen();
if ( is_null( $screen ) || 'shop_order' !== $screen->post_type ) {
return;
if ( class_exists( 'Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) {
/**
* If WC is using the new tables, returns the screen id or empty
*/
$screen = wc_get_page_screen_id( 'shop-order' );
if ( empty( $screen ) ) {
return;
}
if ( ! isset( $_GET['id'], $_GET['page'] ) || ! is_numeric( $_GET['id'] ) || 'wc-orders' !== $_GET['page']
) {
return;
}
$post_id = (int) $_GET['id'];
} else {
/**
* If not, we use the legacy test
*/
$screen = get_current_screen();
if ( is_null( $screen ) || 'shop_order' !== $screen->post_type ) {
return;
}
global $post;
$post_id = $post->ID;
}

$order = wc_get_order( $post );
$order = wc_get_order( $post_id );

if ( false === $order ) {
return;
}
Expand All @@ -60,7 +89,7 @@ public function register_sfa_tracking_details_metabox() {
'sfa-carrier_fields',
__( 'ShoppingFeed Carrier Details', 'shopping-feed-advanced' ),
array( $this, 'render' ),
'shop_order',
$screen,
'side'
);
}
Expand Down
34 changes: 27 additions & 7 deletions src/Admin/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ShoppingFeed\ShoppingFeedWCAdvanced\Admin;

use ShoppingFeed\ShoppingFeedWC\Query\Query;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

// Exit on direct access
defined( 'ABSPATH' ) || exit;
Expand All @@ -17,8 +18,17 @@ class Orders {

public function __construct() {

add_filter( 'manage_edit-shop_order_columns', [ $this, 'custom_shop_order_column' ] );
add_action( 'manage_shop_order_posts_custom_column', [ $this, 'custom_orders_list_column_content' ], 10, 2 );
if ( class_exists( 'Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && \wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) {
$screen = \wc_get_page_screen_id( 'shop_order' );
add_filter( "manage_{$screen}_columns", [ $this, 'custom_shop_order_column' ] );
add_action( "manage_{$screen}_custom_column", [ $this, 'custom_orders_list_column_content' ], 10, 2 );
} else {
add_filter( 'manage_edit-shop_order_columns', [ $this, 'custom_shop_order_column' ] );
add_action( 'manage_shop_order_posts_custom_column', [
$this,
'custom_orders_list_column_content',
], 10, 2 );
}

$this->channel_name_meta = Query::WC_META_SF_CHANNEL_NAME;
$this->sf_reference_meta = Query::WC_META_SF_REFERENCE;
Expand All @@ -41,14 +51,24 @@ public function custom_shop_order_column( $columns ) {
}

// Adding custom fields meta data for each new column (example)
public function custom_orders_list_column_content( $column, $post_id ) {
if ( $this->channel_name_meta === $column ) {
$sf_reference = get_post_meta( $post_id, $this->channel_name_meta, true );
echo ! empty( $sf_reference ) ? esc_html( $sf_reference ) : '<small>(<em>' . esc_html__( 'None', 'shopping-feed-advanced' ) . '</em>)</small>';
public function custom_orders_list_column_content( $column, $post_ID_or_order_object ) {
// Handle the cases of param being and id or a WC_Order object
$order = false;
if ( $post_ID_or_order_object instanceof \WC_Order ) {
$order = $post_ID_or_order_object;
} elseif ( is_int( $post_ID_or_order_object ) && function_exists( 'wc_get_order' ) ) {
$order = wc_get_order( $post_ID_or_order_object );
}
if ( false === $order ) {
return;
}

if ( $this->channel_name_meta === $column ) {
$sf_name = $order->get_meta( $this->channel_name_meta, true );
echo ! empty( $sf_name ) ? esc_html( $sf_name ) : '<small>(<em>' . esc_html__( 'None', 'shopping-feed-advanced' ) . '</em>)</small>';
}
if ( $this->sf_reference_meta === $column ) {
$sf_reference = get_post_meta( $post_id, $this->sf_reference_meta, true );
$sf_reference = $order->get_meta( $this->sf_reference_meta, true );
echo ! empty( $sf_reference ) ? esc_html( $sf_reference ) : '<small>(<em>' . esc_html__( 'None', 'shopping-feed-advanced' ) . '</em>)</small>';
}
}
Expand Down

0 comments on commit 58353b8

Please sign in to comment.