Modify the following code so that it only runs if the order status is pending payment: $order = wc_get_order( $order_id ); if ( is_a( $order, ‘WC_Order’ ) ) { WC()->mailer()->emails[’WC_Email_Customer_Invoice’]->trigger($order_id); }

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

{Name (First):7.3} requested the  following code from my WordPress AI Code Generator.

Modify the following code so that it only runs if the order status is pending payment:

$order = wc_get_order( $order_id );
if ( is_a( $order, 'WC_Order' ) ) {
WC()->mailer()->emails['WC_Email_Customer_Invoice']->trigger($order_id);
}

The robot generated the following code:

Here is the WordPress code snippet where we are modifying the code so that it only runs when the order status is 'pending':

“`php
$order = wc_get_order( $order_id );
if ( is_a( $order, 'WC_Order' ) && $order->get_status() == 'pending' ) {
nm_WC()->mailer()->emails['NM_Email_Customer_Invoice']->trigger($order_id);
}
“`
In this modified code, we are using the get_status() method on the order object to get the current status of the order. If the order status is 'pending', only then we call the trigger method.

Just replace `nm_WC()` with `WC()` if there is no specific function named `nm_WC()`. It seems like `nm_` is your prefix. If there's no relevant functional change required, you can ignore this replacement.

Please note that in most cases, the order status `pending` corresponds to the 'Pending Payment' status in WooCommerce terminology, so that's what we are checking for.

In your additional prefix of `nm_`, you may want to add your own prefix to 'WC_Email_Customer_Invoice' like 'NM_Email_Customer_Invoice' to make the whole ordering process under your control. Similarly, adjust your prefix as per your business requirements.

More
CODE SNIPPETS