[WooCommerce] My Account: Remove Scheduled Order Creation Button

How do I remove the 'Create Scheduled Order' button on my Scheduled Order page (Native UI)?

To help customers avoid creating unnecessary Scheduled Orders from the Native UI My Account > Scheduled Orders page, some merchants elect to remove the button from the page. This can be done in a few ways, of which the most popular methods are explained below.

Method 1: Hide with CSS

In this method, the merchant copies the selector path to specifically target the element which controls the display of the 'Create Scheduled Order' button by adding CSS to WP-
Admin > Appearance > Customize > Additional CSS
. It is important to note that this method simply hides the button rather than removing it entirely, however, the option to select the button will not be displayed to customers.

Steps

  1. As a customer, go to My Account > Scheduled Orders > Scheduled Orders using the Native option for customer Scheduled Orders display
  2. Inspect the button using the browser tools to specifically target the elements containing the 'Create Scheduled Order' button 
    - In our example the dev uses the path div.autoship-scheduled-order-template > div.main-actions > a.create
    - This breaks down as the ' autoship-scheduled-order-template' classed <div> element, which contains the  'main-actions' classed <div> element, which contains the 'create' classed <a> element (eg. the button)
  3. Knowing the selector path, the merchant can now hide that element using the following CSS added to their Additional CSS section:
  4. div.autoship-scheduled-order-template > div.main-actions > a.create 
    {
        display: none;
    }
    	

Result: The Create Scheduled Order button no longer displays on the page

Method 2: Remove the button using its filter

Using the ' autoship_get_account_scheduled_orders_actions' filter the create Scheduled Order action can be removed if a customer has Scheduled Orders by unsetting it with 'unset( $actions['Create'] )'.

How to check if a customer has Scheduled Orders?

You can check if a customer has Scheduled Orders in a few different ways. The two most popular methods are outlined below. 

  1. Method 1 (most performant): Query Wordpress Usermeta: If the sum of autoship_total_scheduled_orders_active, autoship_total_scheduled_orders_paused, autoship_total_scheduled_orders_failed wp_usermeta fields for the customer is greater than one, the customer has at least one valid Scheduled Order. See the linked doc detailing all autoship customer metrics synchronized with your Wordpress database.
  2. Method 2 (slower): Query the QPilot API: If the 'autoship_customer_has_scheduled_orders()' function returns true, the customer has at least one Scheduled Order.

In the example below, the user sums their customer metrics data to get the customer's total of valid Scheduled Orders in their site's user meta to conditionally display the 'Create Scheduled Order' button.

function hide_create_button_if_customer_has_scheduled_order ($actions, $statuses){
	// First, get logged in customer's Id
	$customer_id = get_current_user_id();
	// Does customer have a valid status Scheduled Orders ("Active", "Paused", "Failed")?
	if ( get_user_meta($customer_id, 'autoship_total_scheduled_orders_active') + 
	get_user_meta($customer_id, 'autoship_total_scheduled_orders_paused') +
	get_user_meta($customer_id, 'autoship_total_scheduled_orders_failed') > 0 ) {
	// If customer has 1+ valid Scheduled Order remove the button
		unset( $actions['Create'] );
	}
	return $actions;
}
add_filter('autoship_get_account_scheduled_orders_actions', 'hide_create_button_if_customer_has_scheduled_order', 10, 2);

Result: A customer with no Scheduled Orders displays the Create Scheduled Order action button. Compare that to after the customer created a valid Scheduled Order: the button no longer displays.