Woocommerce - Sending custom email from custom order status - wordpress

I'm new to Wordpress and I'm trying to figure out how to send an email when my order status is changed to a specific custom order status.
Here is my code:
function register_awaiting_shipment_order_status() {
register_post_status( 'wc-awaiting-shipment', array(
'label' => 'Shipped',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s) </span>' )
) );
}
add_action( 'init', 'register_awaiting_shipment_order_status' );
// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses( $order_statuses) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-awaiting-shipment'] = 'Shipped';
// WC()->mailer()->emails['wc-awaiting-shipment']->trigger($order_id);
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_awaiting_shipment_to_order_statuses' );
How would I send an email to the customer when their order status is changed to this custom order status ('shipped')?
Thanks in advance

You can use this plugin for your solution.
WooCommerce Order Status Change Notifier
Or
You can go to plugin settings => Email tab and install (enable) notifications that you want.
Hope this will help you

Related

Allow order to be fully editable while using a custom order status in WooCommerce

I have a bug with the edit quantity in an order while using a custom status. The edit arrow seems to have disappeared?
Is there another value I can include while registering a custom order status?
Here is my code:
// Register new status 'To order'
function register_new_on_hold_order_status2() {
register_post_status( 'wc-to-order', array(
'label' => 'To order',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'To order (%s)', 'To order (%s)' )
) );
}
add_action( 'init', 'register_new_on_hold_order_status2' );
// Add to list of WC Order statuses
function add_on_hold_new_to_order_statuses2( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) { // Here we Define after which to be added
$new_order_statuses['wc-to-order'] = 'To order';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_on_hold_new_to_order_statuses2' );
This is not a bug, certain order statuses such as processing do not allow the order to be editable. To change this you can use the wc_order_is_editable hook
So you get:
function filter_wc_order_is_editable( $editable, $order ) {
// Compare
if ( $order->get_status() == 'your-status' ) {
$editable = true;
}
return $editable;
}
add_filter( 'wc_order_is_editable', 'filter_wc_order_is_editable', 10, 2 );
Note: use woocommerce_register_shop_order_post_statuses
opposite init while registering a custom order status as applied in the 2nd part of this answer

how to show a backorder status in woocommerce orders listing

We have a lot of backorders in WooCommerce and we would like to be able to sort all the backorders in the orders table.
Currently, we are filtering the orders using these status: Awaiting shipment, Refunded, Cancelled, Completed, Processing
Please refer to the screenshot
I've searched online and I only found a plugin that create a separate menu link lisitng backorders only.
Is there a filter I could add in functions.php that would do the job ? I don't want to install a plugin just for that.
Right Now i've only been able to add a custom backorder status using this code below, the second step would be to trigger the status when there is a backordered product.
/**
* Register new status
* Tutorial: http://www.sellwithwp.com/woocommerce-custom-order-status-2/
**/
function register_backorders_order_status() {
register_post_status( 'wc-backorders', array(
'label' => 'Backorders',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'backorders <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_backorders_order_status' );
// Add to list of WC Order statuses
function add_backorders_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-backorders'] = 'Backorders';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_backorders_to_order_statuses' );

Add new order status and send email notification woocommerce [duplicate]

This question already has answers here:
Add custom order status and send email on status change in Woocommerce
(1 answer)
Display orders with a custom status for "all" in Woocommerce admin orders list
(1 answer)
Closed 4 years ago.
Add shipped status on order status change and when status changed to shipped email notification will be send to billing email address.
I tried more and more articles, Please help. I need more explanation from this one.
Please use below code in your functions.php
Register Shipped Order Status in WooCommerce
/**
* Add custom status to order list
*/
add_action( 'init', 'register_custom_post_status', 10 );
function register_custom_post_status() {
register_post_status( 'wc-shipped', array(
'label' => _x( 'Shipped', 'Order status', 'woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>', 'woocommerce' )
) );
}
/**
* Add custom status to order page drop down
*/
add_filter( 'wc_order_statuses', 'custom_wc_order_statuses' );
function custom_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-shipped'] = _x( 'Shipped', 'Order status', 'woocommerce' );
return $order_statuses;
}
Here is the code for ending email to custom order status
add_action('woocommerce_order_status_changed', 'shipped_status_custom_notification', 10, 4);
function shipped_status_custom_notification( $order_id, $from_status, $to_status, $order ) {
if( $order->has_status( 'shipped' )) {
// Getting all WC_emails objects
$email_notifications = WC()->mailer()->get_emails();
// Customizing Heading and subject In the WC_email processing Order object
$email_notifications['WC_Email_Customer_Processing_Order']->heading = __('Your Order shipped','woocommerce');
$email_notifications['WC_Email_Customer_Processing_Order']->subject = 'Your {site_title} order shipped receipt from {order_date}';
// Sending the customized email
$email_notifications['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
}
add_action( 'woocommerce_order_status_wc-shipped', array( WC(), 'send_transactional_email' ), 10, 1 );
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_wc-shipped';
return $actions;
}

Wordpress - custom post status is not showing

I've created new post status. Here's code from my custom plugin:
add_action('init', 'new_post_status_add');
function new_post_status_add () {
register_post_status('refused', array(
'label' => _x('Refused', 'post'),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop('Refused <span class="count">(%s)</span>', 'Refused <span class="count">(%s)</span>'),
));
}
But it's not working - not visible in edit form and quick edit form:
What should I do to make status available?
Sorry for delayed reply. I hope my comment would help others.
Custom Post Status is an unsolved issue that has been there for such a long time, or at least since 8yrs ago on Wordpress website here.
Wordpress noted on codex document for function register_post_status():
This function does NOT add the registered post status to the admin panel. This functionality is pending future development. Please refer
to Trac Ticket #12706. Consider the action hook
post_submitbox_misc_actions for adding this parameter.
There is a work around that I could use for one of my projects. I mixed solutions that described on this url and this url.
The summary is that Wordpress doesn't show custom post status automatically. It needs to be manipulated by using your theme's function.php.
First step
Create custom post status, same as you mentioned. just, don't forget that "This function should not be called before the 'init' action."
‌
function j_custom_post_status() {
$args = array(
'label' => _x( 'Refused', 'post'),
'label_count' => _n_noop( 'Refused <span class="count">(%s)</span>', 'Refused (%s)', 'post' ),
'public' => false,
'internal' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => false,
);
register_post_status( 'j_refused', $args );
add_action( 'init', 'j_custom_post_status', 0 );
‌
I use WP Generator to get the code snippet above.
Second step
Tweak Admin Panel to show the custom status in Quick Edit Menu (QE) on posts list page edit.php. We need JQuery's help to do so and we need to add JQuery code to Admin Panel footer.
‌
function j_append_status_list(){
//array of all custom status that you created
$arr_customstatus = array(
'refused',
'other_custom_status',
);
echo '<script>';
//flush
$tmp = '';
//generate string
foreach ($arr_customstatus as $pkey ) {
$tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
}
echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
});";
echo '</script>';
}
add_action('admin_footer-edit.php','j_append_status_list');
‌
Note: The function above doesn't automatically select the status in QE in case the post's status has been modified before. To add the selected="true" to the option, you need to use the global $post and check the current status, same as the the method used in the Third step below.
Third step
Now, we need to add custom post status meta to post edit page too.
function j_custom_status_metabox(){
global $post;
$custom = get_post_custom( $post->ID );
$status = $custom["_status"][0];
// Array of custom status messages
$arr_customstatus = array(
'refused',
'other_custom_status',
);
//Flush
$tmp = '';
foreach( $jstats as $pkey ) {
if( $status == $pkey ){
$tmp.= '<option value="'.$pkey.'" selected="true">'. $pkey .'</option>';
} else {
$tmp.= '<option value="'. $pkey .'">'. $pkey .'</option>';
}
}
echo '<script>';
echo "jQuery(document).ready( function() { jQuery( 'select[name=\"_status\"]' ).append( ". $tmp ." );
});";
echo '</script>';
}
add_action('admin_footer-post.php','j_custom_status_metabox');
add_action('admin_footer-post-new.php','j_custom_status_metabox');
‌
Step four
In case you like to show a message in posts lists based on custom post status, you can add this function too. However, this is optional.
function j_display_status_label( $statuses ) {
global $post;
//Display label on all posts list but not on the custom status list
if( get_query_var( 'post_status' ) != 'refused' ){
if( $post->post_status == 'refused' ){
return array('Refused');
}
}
return $statuses;
}
add_filter( 'display_post_states', 'j_display_status_label' );
‌‌ ‌
Note: The function above doesn't include all future custom status. You need to add foreach loop in case you need.
Try to use this code,
// Register Custom Status
function custom_post_status() {
$args = array(
'label' => _x( 'Refused', 'Status General Name', 'text_domain' ),
'label_count' => _n_noop( 'Refused (%s)', ' (%s)', 'text_domain' ),
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => false,
);
register_post_status( 'refused', $args );
}
add_action( 'init', 'custom_post_status', 0 );
I hope this will work for you.
Try adding PHP_INT_MAX to your j_custom_post_status action
add_action( 'init', 'j_custom_post_status', PHP_INT_MAX );
‌

Custom WooCommerce Gateway

I`m trying to develop a custom payment gateway for WooCommerce plugin, but I have a problem with the checkout page. What I want is to insert a form on the checkout final step page that is submitted automatically after 5 seconds.
My code is:
...
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action('woocommerce_api_wc_' . $this->id, array($this, 'handle_callback'));
}
function handle_callback() {
wp_die('handle_callback');
}
function receipt_page( $order )
{
echo "receipt page";
$this->generate_submit_form_elements( $order );
}
The problem is that "receipt_page" action is not triggered.
Thanks!
oh, its because you forgot to set the has_fields flag to true.
//after setting the id and method_title, set the has_fields to true
$this -> id = 'kiwipay';
$this -> method_title = 'KiwiPay';
$this->has_fields = true; // if you want credit card payment fields to show on the users checkout page
then in the process_payment function put this:
// Payload would look something like this.
$payload = array(
"amount" => $order.get_total(),
"reference" => $order->get_order_number(),
"orderid" => $order->id,
"return_url" => $this->get_return_url($order) //return to thank you page.
);
response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
// Retrieve the body's response if no errors found
$response_body = wp_remote_retrieve_body( $response );
$response_headers = wp_remote_retrieve_headers( $response );
//use this if you need to redirect the user to the payment page of the bank.
$querystring = http_build_query( $payload );
return array(
'result' => 'success',
'redirect' => $environment_url . '?' . $querystring,
);

Resources