Rename Order Status Messages in WooCommerce

https://www.tychesoftwares.com/how-to-rename-order-status-messages-in-woocommerce/

add_filter( 'wc_order_statuses', 'ts_rename_order_status_msg', 20, 1 );
function ts_rename_order_status_msg( $order_statuses ) {
    $order_statuses['wc-completed']  = _x( 'Order Received', 'Order status', 'woocommerce' );
    $order_statuses['wc-processing'] = _x( 'Your Order is being processed', 'Order status', 'woocommerce' );
    $order_statuses['wc-on-hold']    = _x( 'Your Order is on hold', 'Order status', 'woocommerce' );
    $order_statuses['wc-pending']    = _x( 'Your Order is pending', 'Order status', 'woocommerce' );
    return $order_statuses;
}
Rename Order Status Messages in WooCommerce - Order Status Messages changed

You will see that the order status has changed. We used the hook wc_order_statuses here and attached our function ts_rename_order_status_msg to it. The $order_statuses array that we passed as the argument contains the status messages as its first value.

The status messages also change in the front-end, where you check your order status under My account->Orders:

Rename Order Status Messages in WooCommerce - Order Status Messages changed in the front end

There are two other places though where the Woocommerce order status message gets displayed i.e. in the top menu inside the Admin Dashboard, and also in the dropdown for Bulk Actions:

Rename Order Status Messages in WooCommerce - Order Status Messages in the Top Menu and Bulk Actions in the Admin Dashboard

These status messages can be changed too using two other hooks to which we will attach two other functions.

Add the below code to the functions.php file of your child theme to change the status message in the top menu:

foreach( array( 'post', 'shop_order' ) as $hook )
    add_filter( "views_edit-shop_order", 'ts_order_status_top_changed' );
function ts_order_status_top_changed( $views ){
    if( isset( $views['wc-completed'] ) )
        $views['wc-completed'] = str_replace( 'Completed', __( 'Order Received', 'woocommerce'), $views['wc-completed'] );
    if( isset( $views['wc-processing'] ) )
        $views['wc-processing'] = str_replace( 'Processing', __( 'In Process', 'woocommerce'), $views['wc-processing'] );
    if( isset( $views['wc-on-hold'] ) )
        $views['wc-on-hold'] = str_replace( 'On hold', __( 'Order on Hold', 'woocommerce'), $views['wc-on-hold'] );
    if( isset( $views['wc-pending'] ) )
        $views['wc-pending'] = str_replace( 'Pending payment', __( 'Payment Pending', 'woocommerce'), $views['wc-pending'] );
        return $views;
}

You can see that we have used views_edit-shop_order hook here to simply replace the String values that each array key (for different order status messages) contains.

Rename Order Status Messages in WooCommerce - Order Status Message changed in Admin Top Menu

Now, the last place that we have to change the order status messages (if necessary) is the Bulk Actions dropdown that we highlighted above. For this, we will use yet another code snippet and add it to the same functions.php file:

add_filter( 'bulk_actions-edit-shop_order', 'ts_bulk_actions_order_status', 20, 1 );
function ts_bulk_actions_order_status( $actions ) {
    $actions['mark_processing'] = __( 'Mark as In Process', 'woocommerce' );
    $actions['mark_on-hold']    = __( 'Mark as Order on Hold', 'woocommerce' );
    $actions['mark_completed']  = __( 'Mark as Order Received', 'woocommerce' );
    return $actions;
}

Here, the bulk_actions-edit-shop_order hook is being used to change the values of the different actions that are applied to orders.

You can see that the order status messages will have changed in this dropdown:

Rename Order Status Messages in WooCommerce - Bulk Actions Order Status Message changed

Minimum and maximum allowable product quantities to be added in WooCommerce Cart

woocommerce_quantity_input_min : Allows to change the minimum value for the quantity. Default value is 0.
woocommerce_quantity_input_max : Allows to change maximum value for the quantity. Default value is -1.

/*
* Changing the minimum quantity to 2 for all the WooCommerce products
*/

function woocommerce_quantity_input_min_callback( $min, $product ) {
	$min = 2;  
	return $min;
}
add_filter( 'woocommerce_quantity_input_min', 'woocommerce_quantity_input_min_callback', 10, 2 );

/*
* Changing the maximum quantity to 5 for all the WooCommerce products
*/

function woocommerce_quantity_input_max_callback( $max, $product ) {
	$max = 5;  
	return $max;
}
add_filter( 'woocommerce_quantity_input_max', 'woocommerce_quantity_input_max_callback', 10, 2 );

With backend fields:

https://www.tychesoftwares.com/how-to-set-minimum-and-maximum-allowable-product-quantities-to-be-added-in-woocommerce-cart/

Add to Cart Quantity Plus & Minus Buttons

PHP Snippet: Display Plus & Minus Quantity Buttons @ WooCommerce Single Product Page And Cart Page:
https://www.businessbloomer.com/woocommerce-add-plus-minus-buttons-to-add-to-cart-quantity-input

/**
 * @snippet       Plus Minus Quantity Buttons @ WooCommerce Product Page & Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
  
// -------------
// 1. Show plus minus buttons
  
add_action( 'woocommerce_after_quantity_input_field', 'bbloomer_display_quantity_plus' );
  
function bbloomer_display_quantity_plus() {
   echo '<button type="button" class="plus">+</button>';
}
  
add_action( 'woocommerce_before_quantity_input_field', 'bbloomer_display_quantity_minus' );
  
function bbloomer_display_quantity_minus() {
   echo '<button type="button" class="minus">-</button>';
}
  
// -------------
// 2. Trigger update quantity script
  
add_action( 'wp_footer', 'bbloomer_add_cart_quantity_plus_minus' );
  
function bbloomer_add_cart_quantity_plus_minus() {
 
   if ( ! is_product() && ! is_cart() ) return;
    
   wc_enqueue_js( "   
           
      $(document).on( 'click', 'button.plus, button.minus', function() {
  
         var qty = $( this ).parent( '.quantity' ).find( '.qty' );
         var val = parseFloat(qty.val());
         var max = parseFloat(qty.attr( 'max' ));
         var min = parseFloat(qty.attr( 'min' ));
         var step = parseFloat(qty.attr( 'step' ));
 
         if ( $( this ).is( '.plus' ) ) {
            if ( max && ( max <= val ) ) {
               qty.val( max ).change();
            } else {
               qty.val( val + step ).change();
            }
         } else {
            if ( min && ( min >= val ) ) {
               qty.val( min ).change();
            } else if ( val > 1 ) {
               qty.val( val - step ).change();
            }
         }
 
      });
        
   " );
}

Coupons used column – Orders

Add a column with coupons used on admin Orders list in Woocommerce:

function woo_customer_order_coupon_column_for_orders( $columns ) {
    $new_columns = array();
    
    foreach ( $columns as $column_key => $column_label ) {
        if ( 'order_total' === $column_key ) {
            $new_columns['order_coupons'] = __('Coupons', 'woocommerce');
        }
        
        $new_columns[$column_key] = $column_label;
    }
    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'woo_customer_order_coupon_column_for_orders' );

function woo_display_customer_order_coupon_in_column_for_orders( $column ) {
    global $the_order, $post;
    if( $column  == 'order_coupons' ) {
        if( $coupons = $the_order->get_coupon_codes() ) {
            echo implode(', ', $coupons) . ' ('.count($coupons).')';
        } else {
            echo '<small><em>'. __('No coupons') . '</em></small>';
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'woo_display_customer_order_coupon_in_column_for_orders' );

WooCommerce: Order Info

WooCommerce: Easily Get Order Info (total, items, etc) from $order Object

1. You have access to $order

Hooks (do_action and apply_filters) use additional arguments which are passed on to the function. If they allow you to use the “$order” object you’re in business. Here’s how to get all the order information:

// Get Order ID
$order->get_id();
 
// Get Order Totals $0.00
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
 
// Get Order Items
$order->get_items();
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item();
$order->get_item_count();
$order->get_item_subtotal();
$order->get_item_tax();
$order->get_item_total();
$order->get_downloadable_items();
 
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
 
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
 
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
 
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
 
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
 
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
 
// Get Order Status
$order->get_status();
 
// source: https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

2. You have access to $order_id

If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above.

// Get $order object from order ID
 
$order = wc_get_order( $order_id );
 
// Now you have access to (see above)...
 
$order->get_id()
$order->get_formatted_order_total( )
// etc.
// etc.

Woocommerce email

This script will allow you to send a custom email from anywhere within wordpress but using the woocommerce template so that your emails look the same.
https://gist.github.com/craigedmonds/b65c65e02fd40bd381bf023ffef2c80e

function get_custom_email_html( $order, $heading = false, $mailer ) {

	$template = 'emails/my-custom-email-i-want-to-send.php';

	return wc_get_template_html( $template, array(
		'order'         => $order,
		'email_heading' => $heading,
		'sent_to_admin' => false,
		'plain_text'    => false,
		'email'         => $mailer
	) );

}

// load the mailer class
$mailer = WC()->mailer();

//format the email
$recipient = "someone@somewhere.com";
$subject = __("Hi! Here is a custom notification from us!", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/html\r\n";

//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );

Woocommerce – Checkbox checkout

Add privacy policy checkbox checkout:

add_action( 'woocommerce_review_order_before_payment', 'woo_add_checkout_privacy_policy', 9 );
   
function woo_add_checkout_privacy_policy() {
  
    woocommerce_form_field( 'privacy_policy', array(
        'type'          => 'checkbox',
        'class'         => array('form-row terms wc-terms-and-conditions'),
        'label_class'   => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
        'input_class'   => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
        'required'      => true,
        'label'         => __( 'Mit deiner Bestellung erklärst du dich mit unseren Allgemeinen <a href="/agb/" target="_blank">Geschäftsbedingungen Widerrufsbestimmungen</a> sowie <a href="/datenschutz/" target="_blank">Datenschutzbestimmungen</a> einverstanden.', 'error' )
    )); 
      
}
// Show notice if customer does not tick
add_action( 'woocommerce_checkout_process', 'woo_not_approved_privacy' );
  
function woo_not_approved_privacy() {
    if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
        wc_add_notice( __( 'Die Zustimmung zu unseren Datenschutz und Geschäftsbedingungen (AGB) ist erforderlich.' ), 'error' );
    }
}