Disable invoice for Failed Payment orders
-
Is there any snippet that disables Invoice for Failed Payment orders?
Please advise. Thank you.
-
Hi! Rather than doing this with a snippet, I would suggest handling this primarily based on the email attachments, which is basically the same thing but less complex.
The PDF is only created automatically when it is attached to an email. So if you attach it to the New Order admin email, this will always be attached, even when the payment fails shortly after that. That’s because WooCommerce sends this as soon as the order is placed, regardless of the status (it wouldn’t know anything about future statuses either of course!).
If you attach it to only Completed or even Processing you’re safe because Failed orders will never reach thoses statuses and those emails will never be sent.
More information: Invoice numbers explained.If you prefer a filter based on status (again, noting that WooCommerce will know nothing about future statuses!):
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_disabled_failed_status_attachment', 10, 4 ); function wpo_wcpdf_disabled_failed_status_attachment( $condition, $order, $email_id, $document_type ) { if ($document_type == 'invoice' && $order->get_status() == 'failed') { return false; } return $condition; }
Let me know if you have any other questions!
EwoutThanks, Ewout for the suggestion and code snippet. So far, I’m attaching the PDF invoice to emails that have order’s under Processing only.
I noticed that the “Download PDF Invoice” button doesn’t on the front-end of My Account >> Order page when the order has a status of “Failed” and “Cancelled”. However, the button appears on the admin-end of WooCommerce >> Order page regardless of the order’s status.
My intentions are to have the WooCommerce >> Order page meta-boxes of PDF invoice removed when the order statuses are “Failed” and “Cancelled”. I guess it isn’t that straightforward since it’ll probably require CSS styling to remove them (which you’ve suggested in another thread).
Oh I see, I thought you were talking about the automatic generation, but now I understand you simply want to hide the button, based on the status. Currently you can only set the y Account visibility via the settings (PDF Invoices > Documents > Invoice) as you already found.
To apply more advanced rules for hiding the admin button you can use a PHP script as from this thread:
https://www.ads-software.com/support/topic/invoice-is-downloadable-on-any-order-status/#post-9187083Hope that helps!
EwoutI’ve tried out the following snippet, but it isn’t behaving correctly.
/* Remove meta boxes for all except Processing and Completed =============================================================== */ add_action( 'do_meta_boxes', 'wpo_wcpdf_remove_meta_box' ); function wpo_wcpdf_remove_meta_box( $order_id ) { // make sure $order is the actual order object $order = new WC_Order( $order_id ); // set allowed order statuses (slugs, so 'on-hold' instead of 'On Hold') $allowed_statuses = array( 'processing', 'completed', 'on-hold' ); // remove meta boxes to not allowed order statuses if ( !in_array($order->get_status(), $allowed_statuses) ) { remove_meta_box( 'wpo_wcpdf-box', 'shop_order', 'side' ); remove_meta_box( 'wpo_wcpdf-data-input-box', 'shop_order', 'normal' ); } }
Even when the Order is under “processing”, the meta boxes are still removed. Strange, isn’t it? Where am I doing wrongly?
This snippet completely removes the meta boxes, and not just the button.
Hi Jason,
Thedo_meta_boxes
hook takes 3 arguments, and the first is $post_type, not $order_id – that’s why your function didn’t works.Try this:
/* Remove meta boxes for all except Processing and Completed =============================================================== */ add_action( 'do_meta_boxes', 'wpo_wcpdf_remove_meta_box', 10, 3 ); function wpo_wcpdf_remove_meta_box( $post_type, $context, $post ) { if( $post_type == 'shop_order' ) { // make sure $order is the actual order object $order = new WC_Order( $post ); // set allowed order statuses (slugs, so 'on-hold' instead of 'On Hold') $allowed_statuses = array( 'processing', 'completed', 'on-hold' ); // remove meta boxes to not allowed order statuses if ( !in_array($order->get_status(), $allowed_statuses) ) { remove_meta_box( 'wpo_wcpdf-box', 'shop_order', 'side' ); remove_meta_box( 'wpo_wcpdf-data-input-box', 'shop_order', 'normal' ); } } }
Ewout
Wow! You’re right, Ewout.
I completely forgot the input arguments fordo_meta_boxes
. Thanks for the reminder.I’ll like to add another check to this snippet further by knowing if a Processing order’s payment been successful. If the payment has been successful, then it will not remove the meta boxes.
I’ve tried
$order->is_paid()
and it doesn’t seem to work. ??$order->is_paid()
only checks the order status, it’s a shorthand that compareswc_get_is_paid_statuses()
to the order status. You could try$order->get_date_paid()
checking if it’s set.Alright. You’re amazing, Ewout.
With
$order->get_date_paid()
, I was thinking to use this to add a [Paid] stamp on the PDF Tax Invoice for any paid order, which it’ll be possible add a [Paid] stamp image to an empty section.What are your thoughts, Ewout?
Sounds good! You can use one of the template action hooks for this:
PDF template action hooksEwout
Alright. I got it. I’ve added the following into a customised Invoice template, just below the
order-data
class TD.<?php if ( $this->order->get_date_paid() ) : ?> <img src="https://oharaflorist.com/images/paid-stamp.png" width="120" height="80" alt="PAID"> <?php endif; ?>
CSS styling was included to the PAID stamp, with top and left margins set to 2mm.
The result is as shown here.
Many thanks.
Great, thanks for sharing!
- The topic ‘Disable invoice for Failed Payment orders’ is closed to new replies.