-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwoocommerce-quantity-buttons.php
189 lines (169 loc) · 5.6 KB
/
woocommerce-quantity-buttons.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Plugin Name: SMNTCS Quantity Increment Buttons for WooCommerce
* Plugin URI: https://github.com/nielslange/smntcs-quantity-buttons-for-woocommerce
* Description: Display the quantity increment buttons on the WooCommerce product page and the WooCommerce cart page.
* Author: Niels Lange
* Author URI: https://nielslange.de
* Text Domain: smntcs-quantity-buttons-for-woocommerce
* Version: 2.6
* Requires PHP: 5.6
* Requires at least: 5.0
* WC requires at least: 5.0
* WC tested up to: 7.1
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package SMNTCS_Quantity_Increment_Buttons_for_WooCommerce
*/
defined( 'ABSPATH' ) || exit;
/**
* SMNTCS_Quantity_Increment_Buttons_For_WC main class.
*/
class SMNTCS_Quantity_Increment_Buttons_For_WC {
/**
* Initialise plugin.
*
* @return void
* @since 2.0
*/
public static function init() {
add_action( 'admin_notices', array( __class__, 'handle_notice' ) );
add_action( 'wp_enqueue_scripts', array( __class__, 'enqueue_scripts_and_styles' ) );
add_action( 'wp_enqueue_scripts', array( __class__, 'add_theme_support' ), 11 );
add_filter( 'woocommerce_locate_template', array( __class__, 'load_template' ), 1, 3 );
add_action( 'before_woocommerce_init', array( __class__, 'declare_compatibility' ) );
}
/**
* Declare compatibility with custom order tables for WooCommerce.
*
* @return void
* @since 2.4
*/
public static function declare_compatibility() {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
/**
* Get plugin version.
*
* @return string The plugin version number.
* @since 2.0
*/
private static function get_version() {
$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ), false );
return $plugin_data['Version'];
}
/**
* Show warning if WooCommerce is not active or WooCommerce version < 2.3.
*
* @return void
* @since 1.0
*/
public static function handle_notice() {
global $woocommerce;
if ( ! class_exists( 'WooCommerce' ) || version_compare( $woocommerce->version, '3.0', '<' ) ) {
$message = __( 'SMNTCS Quantity Increment Buttons for WooCommerce requires at least WooCommerce 3.0.', 'smntcs-quantity-buttons-for-woocommerce' );
printf( '<div class="notice notice-warning is-dismissible"><p>%s</p></div>', esc_html( $message ) );
}
}
/**
* Enqueue scripts and styles.
*
* @return void
* @since 1.0
*/
public static function enqueue_scripts_and_styles() {
wp_enqueue_script( 'smntcswcqb-script', plugins_url( 'button-handler.js', __FILE__ ), null, self::get_version(), true );
wp_enqueue_style( 'smntcswcqb-style', plugins_url( 'style.css', __FILE__ ), null, self::get_version(), 'screen' );
$show_on_product_page = apply_filters( 'show_on_product_page', true );
$show_on_cart_page = apply_filters( 'show_on_cart_page', true );
if ( false === $show_on_product_page && is_product() ) {
wp_dequeue_script( 'smntcswcqb-script' );
wp_dequeue_style( 'smntcswcqb-style' );
}
if ( false === $show_on_cart_page && is_cart() ) {
wp_dequeue_script( 'smntcswcqb-script' );
wp_dequeue_style( 'smntcswcqb-style' );
}
}
/**
* Load WooCommerce template.
*
* @param string $template The name and path of the template.
* @param string $template_name The name of the template.
* @param string $template_path The path of the template.
* @return string The name and path of the template.
* @since 1.0
*/
public static function load_template( $template, $template_name, $template_path ) {
global $woocommerce;
$show_on_product_page = apply_filters( 'show_on_product_page', true );
if ( false === $show_on_product_page && is_product() ) {
return $template;
}
$show_on_cart_page = apply_filters( 'show_on_cart_page', true );
if ( false === $show_on_cart_page && is_cart() ) {
return $template;
}
$inital_template = $template;
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/template/';
$template_path = ( ! $template_path ) ? $woocommerce->template_url : null;
$template = locate_template( array( $template_path . $template_name, $template_name ) );
if ( ! $template && file_exists( $plugin_path . $template_name ) ) {
$template = $plugin_path . $template_name;
}
if ( ! $template ) {
$template = $inital_template;
}
return $template;
}
/**
* Add theme support.
*
* @return void
* @since 1.0
*/
public static function add_theme_support() {
switch ( get_template() ) {
case 'twentynineteen':
wp_enqueue_style(
'smntcswcqb-twentynineteen-style',
plugins_url( 'assets/css/twentynineteen.css', __FILE__ ),
null,
self::get_version(),
'screen'
);
break;
case 'twentytwenty':
wp_enqueue_style(
'smntcswcqb-twentytwenty-style',
plugins_url( 'assets/css/twentytwenty.css', __FILE__ ),
null,
self::get_version(),
'screen'
);
break;
case 'twentytwentyone':
wp_enqueue_script(
'smntcswcqb-twentytwentyone-script',
plugins_url( 'assets/js/twentytwentyone.js', __FILE__ ),
null,
self::get_version(),
true
);
break;
case 'smntcs-retro':
wp_enqueue_style(
'smntcswcqb-smntcs-retro-style',
plugins_url( 'assets/css/smntcs-retro.css', __FILE__ ),
null,
self::get_version(),
'screen'
);
break;
}
}
}
SMNTCS_Quantity_Increment_Buttons_For_WC::init();