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

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;
}

Related

How to add phone number masking in woo commerce my account page in wordpress [duplicate]

This question already has an answer here:
Add a mobile phone field on My account > edit account in Woocommerce
(1 answer)
Closed 6 months ago.
I want to add phone number masking on my account page of Woocommerce in wordpress?
Add the below code snippet in your active theme's functions.php file -
// Add field in Account Details
function wc_add_field_edit_account_form() {
woocommerce_form_field(
'user_phone_number',
array(
'type' => 'tel',
'required' => true, // for false do not need this `woocommerce_save_account_details_required_fields` callback
'label' => __( 'Phone Number', 'yourtextdomain' ),
),
get_user_meta( get_current_user_id(), 'user_phone_number', true )
);
}
add_action( 'woocommerce_edit_account_form', 'wc_add_field_edit_account_form' );
// Save field data
function wc_save_account_details( $user_id ) {
update_user_meta( $user_id, 'user_phone_number', wc_clean( $_POST[ 'user_phone_number' ] ) );
}
add_action( 'woocommerce_save_account_details', 'wc_save_account_details' );
// Add field required
function wc_add_account_details_required_fields( $required_fields ){
$required_fields[ 'user_phone_number' ] = __( 'Phone Number', 'yourtextdomain' );
return $required_fields;
}
add_filter( 'woocommerce_save_account_details_required_fields', 'wc_add_account_details_required_fields' );

How do I get the "text" value from a dropdown list as an email notification? [duplicate]

This question already has answers here:
Order custom fields not displayed on WooCommerce email notifications
(2 answers)
Show the result of custom delivery field on the checkout page in WooCommerce frontend, backend & email notifications
(1 answer)
Closed 8 months ago.
Thanks to some help, I've created a dropdown list on the checkout page of my Woocommerce site. It works beautifully but on my email notification, it shows the code and not the value of the selection. Eg: "My Marketer: mariaMarais" instead of "My Marketer: Maria Marais - Natal." How can I change it to show the string and not the code?
// add marketer list
//* Add select field to the checkout page
add_action('woocommerce_before_order_notes', 'dd_add_select_two_checkout_field');
function dd_add_select_two_checkout_field( $checkout ) {
woocommerce_form_field( 'myquestion', array(
'type' => 'select',
'class' => array( 'select2-wrapper' ),
'label' => __( 'Please select your marketer.' ),
'required' => true,
'input_class' => array( 'add-select2' ),
'options' => array( // added extra for effect.
'headoffice' => __( 'Head Office', 'wps' ),
'mariaMarais' => __( 'Maria Marais - Natal', 'wps' )
)
),
$checkout->get_value( 'myquestion' ));
wc_enqueue_js( "$( '.add-select2' ).selectWoo(); ");
}
//* Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'wps_select_checkout_field_update_order_meta');
function wps_select_checkout_field_update_order_meta( $order_id ) {
if ($_POST['myquestion']) update_post_meta( $order_id, 'myquestion', esc_attr($_POST['myquestion']));
}
//* Display field value on the order edition page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'wps_select_checkout_field_display_admin_order_meta', 10, 1 );
function wps_select_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Found by').':</strong> ' . get_post_meta( $order->id, 'myquestion', true ) . '</p>';
}
//* Add selection field value to emails
add_filter('woocommerce_email_order_meta_keys', 'wps_select_order_meta_keys');
function wps_select_order_meta_keys( $keys ) {
$keys['My Marketer:'] = 'myquestion';
return $keys;
}

Woocommerce conditional fields based on product [duplicate]

This question already has answers here:
WooCommerce conditional custom checkout fields
(1 answer)
Conditional custom checkout fields based on product category in Woocommerce
(1 answer)
Closed 10 months ago.
I need to provide conditional required fields on 2 of 3 products in my very specific "store" . 1st product has no added fields. Second one has 1 required. 3rd has the same as the second plus one more required. I have tried plugins as I am right brained but they all cause cart failure. I have cobbled together some code from the inter web that has gotten me passed the cart failure but it presents both of the fields to all of the products (I set the second condition to optional in the code so that it would at least work on a basic level with placeholder text in the second field about requirement). In addition I need these conditional fields to be passed to to the completed order so that they show up in the order tables in admin. here is my rudimentary code.
/**
* checkout field addition License Plate.
*
* #param array $fields List of existing billing fields.
* #return array List of modified billing fields.
*/
function parking_add_checkout_fields( $fields ) {
$fields['billing_FIELD_ID'] = array(
'label' => __( 'License Plate & State' ),
'type' => 'text',
'class' => array( 'form-row-wide' ),
'priority' => 110,
'required' => true,
);
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'parking_add_checkout_fields' );
function js_woocommerce_admin_billing_fields( $fields ) {
$fields['FIELD_ID'] = array(
'label' => __( 'License Plate & State' ),
'show' => true,
);
return $fields;
}
add_filter( 'woocommerce_admin_billing_fields', 'js_woocommerce_admin_billing_fields' );
/* checkout field addition Citation.
*
* #param array $fields List of existing billing fields.
* #return array List of modified billing fields.
*/
function citation_add_checkout_fields( $fields ) {
$fields['billing_FIELD2_ID'] = array(
'label' => __( 'Enter Citation #' ),
'type' => 'text',
'class' => array( 'form-row-wide' ),
'priority' => 112,
'required' => false,
'placeholder' => 'If you are purchasing a Citation Dismissal Permit, enter Citation number here',
);
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'citation_add_checkout_fields' );
$fields['FIELD2_ID'] = array(
'label' => __( 'Enter Citation #' ),
'show' => true,
);
return $fields;
add_filter( 'woocommerce_admin_billing_fields', 'js_woocommerce_admin_billing_fields' );
/**
* Remove optional label
* https://elextensions.com/knowledge-base/remove-optional-text-woocommerce-checkout-fields/
*/
add_filter( 'woocommerce_form_field' , 'elex_remove_checkout_optional_text', 10, 4 );
function elex_remove_checkout_optional_text( $field, $key, $args, $value ) {
if( is_checkout() && ! is_wc_endpoint_url() ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}

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' );

Woocommerce - Sending custom email from custom order status

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

Resources