-
Notifications
You must be signed in to change notification settings - Fork 0
/
elkmipo.php
121 lines (108 loc) · 3.96 KB
/
elkmipo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* @wordpress-plugin
* Plugin Name: ELK Minimum Items per Order for WooCommerce
* Plugin URI: https://github.com/kokiddp/elkmipo
* Description: This simple plugin allows to set a minimum number of items per order
* Version: 1.1.0
* Requires at least: 4.6
* Tested up to: 5.4.1
* Requires PHP: 7.1
* WC requires at least: 3.0.2
* WC tested up to: 4.1.0
* Author: ELK-Lab
* Author URI: https://www.elk-lab.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: elkmipo
* Domain Path: /languages
*/
if ( !defined( 'ABSPATH' ) || !defined( 'WPINC' ) ) {
die;
}
add_action( 'init', 'elkmipo_load_textdomain' );
function elkmipo_load_textdomain() {
load_plugin_textdomain( 'elkmipo', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'admin_init', 'elkmipo_require_woocommerce' );
function elkmipo_require_woocommerce() {
if ( is_admin() && current_user_can( 'activate_plugins' ) ) {
if ( !is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
add_action( 'admin_notices', 'elkmipo_no_woocommerce_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
}
function elkmipo_no_woocommerce_notice(){
?><div class="error"><p><?= __( 'WooCommerce is required in order to use ELK Minimum Items per Order', 'elkmipo' ) ?></p></div><?php
}
add_filter( 'woocommerce_get_sections_products', 'elkmipo_add_settings_section' );
function elkmipo_add_settings_section( $sections ) {
$sections['elkmipo'] = __( 'ELK Minimum Items per Order', 'elkmipo' );
return $sections;
}
add_filter( 'woocommerce_get_settings_products', 'elkmipo_settings_section', 10, 2 );
function elkmipo_settings_section( $settings, $current_section ) {
if ( $current_section == 'elkmipo' ) {
$elkmipo_settings = array();
$elkmipo_settings[] = array(
'name' => __( 'ELK Minimum Items per Order Settings', 'elkmipo' ),
'type' => 'title',
'desc' => __( 'The following options are used to configure ELK Minimum Items per Order', 'elkmipo' ),
'id' => 'elkmipo'
);
$elkmipo_settings[] = array(
'name' => __( 'Minimum items', 'elkmipo' ),
'desc_tip' => __( 'Insert the minimum number of items per order. Leave blank to disable', 'elkmipo' ),
'id' => 'elkmipo_min_items',
'type' => 'number'
);
$elkmipo_settings[] = array(
'name' => __( 'Multiple of', 'elkmipo' ),
'desc_tip' => __( 'Insert the number the items must be multiple of. Leave blank to disable', 'elkmipo' ),
'id' => 'elkmipo_multiple',
'type' => 'number'
);
$elkmipo_settings[] = array(
'type' => 'sectionend',
'id' => 'elkmipo'
);
return $elkmipo_settings;
}
else {
return $settings;
}
}
add_action( 'woocommerce_check_cart_items', 'elkmipo_minimum_order_amount' );
function elkmipo_minimum_order_amount() {
if( is_cart() || is_checkout() ) {
$minimum = intval( get_option( 'elkmipo_min_items' ) != null ? get_option( 'elkmipo_min_items' ) : 1 );
$multiple = intval( get_option( 'elkmipo_multiple' ) != null ? get_option( 'elkmipo_multiple' ) : 1 );
$cart_count = intval( WC()->cart->get_cart_contents_count() );
if ( $cart_count < $minimum ) {
wc_add_notice(
sprintf(
__( 'In your cart there are %1$s items — you must have at least %2$s items in your cart to place your order. <a href="%3$s">Back to Store</a>' , 'elkmipo' ),
$cart_count,
$minimum,
get_permalink( woocommerce_get_page_id( 'shop' ) )
),
'error'
);
}
if ( ( $cart_count % $multiple ) > 0 ) {
wc_add_notice(
sprintf(
__( 'In your cart there are %1$s items — you must have a multiple of %2$s items in your cart to place your order. <a href="%3$s">Back to Store</a>' , 'elkmipo' ),
$cart_count,
$minimum,
get_permalink( woocommerce_get_page_id( 'shop' ) )
),
'error'
);
}
}
}