Skip to content

Instantly share code, notes, and snippets.

@corsonr
Last active July 6, 2023 02:39
Show Gist options
  • Save corsonr/51adfbc1eccbab1a97c182eb91a52a4e to your computer and use it in GitHub Desktop.
Save corsonr/51adfbc1eccbab1a97c182eb91a52a4e to your computer and use it in GitHub Desktop.
WooCommerce - restrict number of orders per day
<?php
/**
* Plugin Name: Restrict Orders per Day for WooCommerce
* Plugin URI: https://remicorson.com
* Description: Put the shop into catalogue mode once number of orders per day is reached.
* Version: 0.1
* Author: Remi Corson, corsonr
* Author URI: https://remicorson.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: woo-restrict-orders-per-day
* Domain Path: /languages
*/
/*
* NOTE: this is basic coding, quickly done to fulfill a need for companies struggling due to COVID-19 bad stuff occuring ATM.
* Feel free to submit improvements.
* Edit the number of orders per day via the WooCommerce > Settings > General meu item.
*/
add_filter( 'woocommerce_general_settings', 'woo_restrict_orders_per_day_settings' );
function woo_restrict_orders_per_day_settings( $settings ) {
$updated_settings = array();
foreach ( $settings as $section ) {
if ( isset( $section['id'] ) && 'general_options' == $section['id'] &&
isset( $section['type'] ) && 'sectionend' == $section['type'] ) {
$updated_settings[] = array(
'name' => __( 'Maximum order day', 'woo-restrict-orders-per-day' ),
'desc_tip' => __( 'Define the number of order you can handle.', 'woo-restrict-orders-per-days' ),
'id' => 'woocommerce_max_orders_per_day',
'type' => 'number',
'css' => 'min-width:300px;',
'std' => '100', // WC < 2.0
'default' => '100', // WC >= 2.0
'desc' => __( 'You must enter a number.', 'woo-restrict-orders-per-day' ),
);
}
$updated_settings[] = $section;
}
return $updated_settings;
}
function get_daily_orders_count( $date = 'now' ) {
if ( 'now' === $date ) {
$date = date( 'Y-m-d' );
$date_string = "> '$date'";
} else {
$date = date( 'Y-m-d', strtotime( $date ) );
$date2 = date( 'Y-m-d', strtotime( $date ) + 86400 );
$date_string = "BETWEEN '$date' AND '$date2'";
}
global $wpdb;
$result = $wpdb->get_var( "
SELECT DISTINCT count(p.ID) FROM {$wpdb->prefix}posts as p
WHERE p.post_type = 'shop_order' AND p.post_date $date_string
AND p.post_status IN ('wc-on-hold','wc-processing','wc-completed')
" );
return $result;
}
add_action( 'init', 'enable_catalog_mode' );
function enable_catalog_mode() {
if ( is_admin() ) {
return;
}
$orders_capacity = get_option( 'woocommerce_max_orders_per_day' );
if ( get_daily_orders_count() >= $orders_capacity ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
wc_add_notice( __( 'We exceeded our capacity and we are sorry to say we can not take new orders for today!', 'woo-restrict-orders-per-days' ), 'notice' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment