Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support gift cards in admin order refunds template #31414

Merged
merged 7 commits into from Feb 16, 2022

Conversation

xristos3490
Copy link
Member

@xristos3490 xristos3490 commented Dec 10, 2021

Closes #31413

Specification

Please check the issue description for additional details.

Changelog

Dev - Added woocommerce_admin_order_should_render_refunds hook to allow control over the refunds UI within the order editor.

@manospsyx
Copy link
Member

manospsyx commented Dec 16, 2021

@woocommerce/proton see pdqkbG-6O-p2#comment-173

@peterfabian peterfabian requested review from a team and barryhughes and removed request for a team January 27, 2022 16:41
@@ -282,7 +282,7 @@
<?php else : ?>
<span class="description"><?php echo wc_help_tip( __( 'To edit this order change the status back to "Pending payment"', 'woocommerce' ) ); ?> <?php esc_html_e( 'This order is no longer editable.', 'woocommerce' ); ?></span>
<?php endif; ?>
<?php if ( 0 < $order->get_total() - $order->get_total_refunded() || 0 < absint( $order->get_item_count() - $order->get_item_count_refunded() ) ) : ?>
<?php if ( (bool) apply_filters( 'woocommerce_admin_order_should_render_refunds', 0 < $order->get_total() - $order->get_total_refunded() || 0 < absint( $order->get_item_count() - $order->get_item_count_refunded() ), $order->get_id(), $order ) ) : ?>
Copy link
Member

Choose a reason for hiding this comment

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

Where possible we should avoid placing filter hooks inside templates and we should also document any new hooks we add.

I realize the duplication of the logic predates your change, and you were just wrapping it in a filter, but this is actually a pretty nice opportunity to clean it up. We could for example place the hook inside WC_Meta_Box_Order_Items::output() (adding a docblock to explain what it does) and then pass the result in here as a variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review, @barryhughes! Sounds reasonable!

Copy link
Member

@barryhughes barryhughes left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this: seems reasonable but a few small changes are needed.

@xristos3490
Copy link
Member Author

xristos3490 commented Feb 9, 2022

Tests are failing.

We could for example place the hook inside WC_Meta_Box_Order_Items::output() (adding a docblock to explain what it does) and then pass the result in here as a variable.

Adding a filter into the WC_Meta_Box_Order_Items::output() function creates the need to include the same filter into multiple WC_AJAX endpoints due to how each AJAX admin handler renders the order items table. e.g., WC_AJAX::maybe_add_order_item() Line 973 and others.

Instead of writing the same filter in multiple places, we could:

  • Refactor the way we render the html-order-items.php in all accuracies by using a global render function, for example, in the WC_Meta_Box_Order_Items class, which will not rely on global variables or;
  • Leave the filter inline inside the template as is.

Moreover, I think introducing a new variable inside the html-order-items.php, or a dedicated render method for that matter, could potentially break 3rd party extensions using this template to include additional functionality in the order items table.

Another way could be to introduce a global helper function, e.g., woocommerce_admin_should_render_refunds( $order ) that will have this filter in its return statement -- this should play well along with 3rd party code.

In my opinion, and because the template in question is widely used, I would opt for leaving the filter inline. Although, I understand your point that this filter would be used twice in the same template, which is not ideal.

Thoughts?

@barryhughes
Copy link
Member

You're right, it doesn't seem viable in this case and there isn't a particularly clean way to tidy this up without a larger body of work.

Moreover, I think introducing a new variable inside the html-order-items.php, or a dedicated render method for that matter, could potentially break 3rd party extensions using this template to include additional functionality in the order items table.
...
In my opinion, and because the template in question is widely used, I would opt for leaving the filter inline.

It still seems a shame to repeat it unnecessarily by inlining it. Given that all of the other data available to the template remains the same, do you see difficulties if we do something along these lines (ie, place the filter once at the top of the template):

<?php
/**
 * Order items HTML for meta box.
 *
 * @package WooCommerce\Admin
 */

/**
 * Docblock for the new filter.
 */
$render_refunds = (bool) apply_filters( 'woocommerce_admin_order_should_render_refunds', 0 < $order->get_total() - $order->get_total_refunded() || 0 < absint( $order->get_item_count() - $order->get_item_count_refunded() ), $order->get_id(), $order );
?>

<!-- Other template code -->

<?php if ( $render_refunds ) : ?>
    <!-- Refund HTML -->
<?php endif; ?>

<!-- Other template code -->

@xristos3490
Copy link
Member Author

It still seems a shame to repeat it unnecessarily by inlining it.

I feel the same way!

do you see difficulties if we do something along these lines

No. Placing the filter once at the top of the template seems like a good idea at this point!

@barryhughes
Copy link
Member

Excellent, thanks. I tweaked the docblock a little (I believe this will actually ship in 6.4) and once tests pass I'll go ahead and merge. Thanks for the contribution :-)

@barryhughes barryhughes merged commit 91ed566 into trunk Feb 16, 2022
@barryhughes barryhughes deleted the fix/extend-admin-refunds-for-gift-cards branch February 16, 2022 23:35
@github-actions github-actions bot added this to the 6.4.0 milestone Feb 16, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2022

Hi @barryhughes, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the status: needs changelog label
  • Add the status: needs testing instructions label

@barryhughes barryhughes added the release: add changelog Mark all PRs that have not had their changelog entries added. [auto] label Feb 16, 2022
@xristos3490
Copy link
Member Author

Thanks, @barryhughes! 🙇
I'll keep an eye on this to pass this filter/fix into the extension!

@ObliviousHarmony ObliviousHarmony added plugin: woocommerce Issues related to the WooCommerce Core plugin. and removed release: add changelog Mark all PRs that have not had their changelog entries added. [auto] labels Mar 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support gift cards in admin order refunds template
4 participants