-
Notifications
You must be signed in to change notification settings - Fork 3
/
woocommerce-full-width-add-to-cart.php
executable file
·418 lines (345 loc) · 12.3 KB
/
woocommerce-full-width-add-to-cart.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/*
* Plugin Name: WooCommerce Stacked Product Layout
* Description: A handy plugin for stacking the add-to-cart form content of complex WooCommerce product types below the main product image and summary. Useful if the add-to-cart section of your products appears very narrow or squeezed.
* Version: 1.2.1
* Author: SomewhereWarm
* Author URI: https://somewherewarm.gr/
*
* Text Domain: woocommerce-full-width-add-to-cart
* Domain Path: /languages/
*
* Requires at least: 4.4
* Tested up to: 4.9
*
* WC requires at least: 2.6
* WC tested up to: 3.4
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Main plugin class.
*/
class WC_Stacked_Product_Layout {
/**
* Plugin version.
* @var string
*/
public static $version = '1.2.0';
/**
* Legacy settings mode.
* @var bool
*/
private static $use_legacy_settings = true;
/**
* Runtime cache for WC product types.
* @var array
*/
private static $product_types_cache;
/**
* Plugin URL getter.
*
* @return string
*/
public static function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* True if using WC < 3.3 settings.
*
* @return bool
*/
private static function using_legacy_settings() {
return self::$use_legacy_settings;
}
/**
* Initialization.
*/
public static function init() {
add_action( 'woocommerce_loaded', array( __CLASS__, 'load' ) );
}
/**
* Load plugin.
*/
public static function load() {
// Use customizer and new option keys when WC > 3.3.
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3.0' ) >= 0 ) {
self::$use_legacy_settings = false;
}
self::update_options();
self::add_hooks();
}
/**
* Check plugin version and update plugin options.
*/
public static function update_options() {
// Check if new options exist -- if not, copy them over from legacy option.
if ( ! self::using_legacy_settings() ) {
$layout_option = get_option( 'wc_spl_product_details_layout' );
if ( ! $layout_option ) {
$legacy_option = get_option( 'wc_fw_add_to_cart_layout_types', array() );
update_option( 'wc_spl_product_details_layout', ! empty( $legacy_option ) ? 'stacked' : 'default' );
update_option( 'wc_spl_product_details_layout_types', ! empty( $legacy_option ) ? $legacy_option : array() );
}
}
}
/**
* Add hooks.
*/
public static function add_hooks() {
// Hook the 'woocommerce_template_single_add_to_cart' function to the 'woocommerce_after_single_product_summary' hook if needed.
add_action( 'woocommerce_before_single_product', array( __CLASS__, 'single_add_to_cart' ) );
if ( self::using_legacy_settings() ) {
// Display options under 'WooCommerce > Settings > Products > Display'.
add_filter( 'woocommerce_product_settings', array( __CLASS__, 'all_settings' ) );
} else {
// Display options in Customizer.
add_action( 'customize_register', array( __CLASS__, 'add_customizer_section' ) );
add_action( 'customize_controls_print_styles', array( __CLASS__, 'add_customizer_styles' ) );
add_action( 'customize_controls_print_scripts', array( __CLASS__, 'add_customizer_scripts' ), 30 );
}
}
/**
* Adds a new 'Product Details' section under the WC customizer options.
*
* @param WP_Customize_Manager $wp_customize
*/
public static function add_customizer_section( $wp_customize ) {
require_once( 'includes/class-wc-spl-customize-multiple-select-control.php' );
$wp_customize->add_section(
'wc_spl_product_details',
array(
'title' => __( 'Product Details', 'woocommerce-full-width-add-to-cart' ),
'priority' => 10,
'panel' => 'woocommerce',
)
);
$wp_customize->add_setting(
'wc_spl_product_details_layout',
array(
'type' => 'option',
'capability' => 'manage_woocommerce',
'sanitize_callback' => array( __CLASS__, 'sanitize_product_layout' ),
'default' => 'default'
)
);
$wp_customize->add_control(
'wc_spl_product_details_layout',
array(
'label' => __( 'Layout', 'woocommerce-full-width-add-to-cart' ),
'description' => __( 'Choose the Stacked option to move the add-to-cart form below the main product image and summary.', 'woocommerce-full-width-add-to-cart' ),
'section' => 'wc_spl_product_details',
'settings' => 'wc_spl_product_details_layout',
'type' => 'select',
'choices' => array(
'default' => __( 'Default', 'woocommerce-full-width-add-to-cart' ),
'stacked' => __( 'Stacked', 'woocommerce-full-width-add-to-cart' )
)
)
);
$wp_customize->add_setting(
'wc_spl_product_details_layout_types',
array(
'type' => 'option',
'capability' => 'manage_woocommerce',
'sanitize_callback' => array( __CLASS__, 'sanitize_stacked_product_types' ),
'default' => ''
)
);
$wp_customize->add_control(
new WC_SPL_Customize_Multiple_Select_Control( $wp_customize, 'wc_spl_product_details_layout_types', array(
'label' => __( 'Restrict To…', 'woocommerce-full-width-add-to-cart' ),
'description' => __( 'Only use this layout with specific product types?', 'woocommerce-full-width-add-to-cart' ),
'section' => 'wc_spl_product_details',
'settings' => 'wc_spl_product_details_layout_types',
'type' => 'wc-spl-multiple-select',
'choices' => self::get_product_types()
) )
);
}
/**
* Customizer styles.
*/
public static function add_customizer_styles() {
?>
<style type="text/css">
#customize-control-wc_spl_product_details_layout_types label, #customize-control-wc_spl_product_details_layout label {
cursor: default;
}
</style>
<?php
}
/**
* Customizer scripts.
*/
public static function add_customizer_scripts() {
?><script type="text/javascript">
jQuery( document ).ready( function( $ ) {
wp.customize.bind( 'ready', function() {
$( document.body ).trigger( 'wc-enhanced-select-init' );
var $layout_select = $( '#customize-control-wc_spl_product_details_layout select' ),
$layout_types = $( '#customize-control-wc_spl_product_details_layout_types' );
$layout_select.on( 'change', function() {
if ( 'stacked' === $( this ).val() ) {
$layout_types.slideDown( 200 );
} else {
$layout_types.hide();
}
return false;
} );
$layout_types.on( 'click', 'label', function() {
return false;
} );
$layout_select.change();
} );
} );
</script><?php
}
/**
* Sanitize the 'Product Details > Layout' option.
*
* @param string $value
* @return string
*/
public static function sanitize_product_layout( $value ) {
$options = array( 'default', 'stacked' );
return in_array( $value, $options, true ) ? $value : 'default';
}
/**
* Sanitize the 'Product Details > Product Types' option.
*
* @param array $value
* @return array
*/
public static function sanitize_stacked_product_types( $value ) {
$value = array_intersect( $value, array_keys( self::get_product_types() ) );
return $value;
}
/**
* Hook the 'woocommerce_template_single_add_to_cart' function to the 'woocommerce_after_single_product_summary' hook if needed.
*/
public static function single_add_to_cart() {
// Unhook 'woocommerce_template_single_add_to_cart' from 'woocommerce_single_product_summary' 30.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// Hook template_single_add_to_cart into 'woocommerce_template_single_add_to_cart' 30 and run 'woocommerce_single_product_summary' through it only if the product type doesn't need to be moved.
add_action( 'woocommerce_single_product_summary', array( __CLASS__, 'template_single_add_to_cart' ), 30 );
// Hook single_add_to_cart_after_summary into 'woocommerce_after_single_product_summary' 5 and run 'woocommerce_single_product_summary' through it if the product type needs to be moved.
add_action( 'woocommerce_after_single_product_summary', array( __CLASS__, 'single_add_to_cart_after_summary' ), 5 );
}
/**
* Indicates if a layout is active.
*
* @since 1.2.0
* @return bool
*/
public static function is_layout_active( $layout ) {
global $product;
$is_active = false;
if ( 'default' === $layout ) {
$is_active = ! self::is_layout_active( 'stacked' );
} elseif ( 'stacked' === $layout ) {
$types = self::get_stacked_types();
$is_active = ! empty( $types ) && is_array( $types ) && $product->is_type( $types );
// Add compatibility for "Form location" option.
if ( $product->is_type( array( 'composite', 'bundle', 'mix-and-match' ) ) && is_callable( array( $product, 'get_add_to_cart_form_location' ) ) && 'after_summary' === $product->get_add_to_cart_form_location() ) {
$is_active = false;
}
}
return $is_active;
}
/**
* Get product types to apply the stacked layout to.
*
* @since 1.2.0
* @return array
*/
private static function get_stacked_types() {
if ( self::using_legacy_settings() ) {
$move_types = get_option( 'wc_fw_add_to_cart_layout_types', array() );
} else {
$move_types = get_option( 'wc_spl_product_details_layout_types', array() );
$layout = get_option( 'wc_spl_product_details_layout', 'default' );
}
if ( 'default' === $layout ) {
$move_types = array();
} elseif ( 'stacked' === $layout ) {
$move_types = empty( $move_types ) ? array_keys( self::get_product_types() ) : $move_types;
}
return apply_filters( 'woocommerce_full_width_add_to_cart_types', $move_types );
}
/**
* When displaying a type that needs to be moved, call 'woocommerce_template_single_add_to_cart'.
* Hooked into 'woocommerce_after_single_product_summary'.
*/
public static function single_add_to_cart_after_summary() {
if ( self::is_layout_active( 'stacked' ) ) {
$classes = apply_filters( 'woocommerce_full_width_add_to_cart_section_classes', array( 'add-to-cart-summary', 'stacked-summary' ) );
$classes = implode( ' ', $classes );
echo '<div class="' . $classes . '">';
woocommerce_template_single_add_to_cart();
echo '</div>';
wp_register_style( 'wc-single-product-stacked', self::plugin_url() . '/assets/css/wc-single-product-stacked.css', false, self::$version );
wp_enqueue_style( 'wc-single-product-stacked' );
}
}
/**
* When displaying a type that doesn't need to be moved, call 'woocommerce_template_single_add_to_cart' as usual.
* Hooked into 'woocommerce_single_product_summary', same position as before.
*/
public static function template_single_add_to_cart() {
if ( self::is_layout_active( 'default' ) ) {
woocommerce_template_single_add_to_cart();
}
}
/**
* Plugin settings added in the WC Product tab under the Display section.
*
* @param array $settings
* @return array
*/
public static function all_settings( $settings ) {
$fw_setting = array(
'id' => 'wc_fw_add_to_cart_layout_types',
'title' => __( 'Force Stacked Layout', 'woocommerce-full-width-add-to-cart' ),
'type' => 'multiselect',
'class' => 'chosen_select',
'css' => 'width: 450px;',
'desc' => '<div>' . __( 'The product types selected here will use a modified product details layout, with the add-to-cart form stacked under the main product image and summary.', 'woocommerce-full-width-add-to-cart' ) . '</div>',
'desc_tip' => true,
'default' => '',
'options' => self::get_product_types(),
'custom_attributes' => array(
'data-placeholder' => __( 'Select some product types…', 'woocommerce-full-width-add-to-cart' )
)
);
$new_settings = array();
foreach ( $settings as $i => $setting ) {
$new_settings[] = $settings[ $i ];
if ( $setting[ 'id' ] === 'woocommerce_enable_ajax_add_to_cart' ) {
$new_settings[] = $fw_setting;
}
}
return $new_settings;
}
/**
* Gets all registered product types.
*
* @return array
*/
public static function get_product_types() {
if ( ! empty( self::$product_types_cache ) ) {
return self::$product_types_cache;
}
$types = array();
$terms = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$types[ $term->slug ] = ucfirst( $term->name );
}
}
self::$product_types_cache = $types;
return $types;
}
}
WC_Stacked_Product_Layout::init();