-
Notifications
You must be signed in to change notification settings - Fork 34
/
wc-variations-radio-buttons.php
128 lines (100 loc) · 3.72 KB
/
wc-variations-radio-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
<?php
/**
* Plugin Name: WC Variations Radio Buttons
* Plugin URI: https://wordpress.org/plugins/wc-variations-radio-buttons/
* Description: Variations Radio Buttons for WooCommerce. Let your customers choose product variations using radio buttons instead of dropdowns.
* Version: 2.1.0
* Author: 8manos
* Author URI: http://8manos.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* WC requires at least: 3.0
* WC tested up to: 8.8
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Check if WooCommerce is active
if ( is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
class WC_Radio_Buttons {
// plugin version
const VERSION = '2.1.0';
private $plugin_path;
private $plugin_url;
public function __construct() {
add_filter( 'woocommerce_locate_template', array( $this, 'locate_template' ), 10, 3 );
//js scripts
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ), 999 );
// Add HPOS compatibility
add_action('before_woocommerce_init', array($this, 'hposCompatibility'));
}
public function get_plugin_path() {
if ( $this->plugin_path ) {
return $this->plugin_path;
}
return $this->plugin_path = plugin_dir_path( __FILE__ );
}
public function get_plugin_url() {
if ( $this->plugin_url ) {
return $this->plugin_url;
}
return $this->plugin_url = plugin_dir_url( __FILE__ );
}
public function locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) {
$template_path = $woocommerce->template_url;
}
$plugin_path = $this->get_plugin_path() . 'templates/';
// Look within passed path within the theme - this is priority
$template = locate_template( array(
$template_path . $template_name,
$template_name
) );
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) ) {
$template = $plugin_path . $template_name;
}
// Use default template
if ( ! $template ) {
$template = $_template;
}
return $template;
}
function load_scripts() {
// Don't load JS if current product type is bundle to prevent the page from not working
if (!(wc_get_product() && wc_get_product()->is_type('bundle'))) {
wp_deregister_script( 'wc-add-to-cart-variation' );
wp_register_script( 'wc-add-to-cart-variation', $this->get_plugin_url() . 'assets/js/frontend/add-to-cart-variation.js', array( 'jquery', 'wp-util' ), self::VERSION );
}
}
/**
* Mark the plugin as compatible with the HPOS feature
*
* @return void
*/
function hposCompatibility(): void {
if(class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class))
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
}
new WC_Radio_Buttons();
if ( ! function_exists( 'print_attribute_radio' ) ) {
function print_attribute_radio( $checked_value, $value, $label, $name ) {
global $product;
$input_name = 'attribute_' . esc_attr( $name ) ;
$esc_value = esc_attr( $value );
$id = esc_attr( $name . '_v_' . $value . $product->get_id() ); //added product ID at the end of the name to target single products
$checked = checked( $checked_value, $value, false );
$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label, esc_attr( $name ) );
printf(
'<div>
<input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s>
<label for="%3$s">%5$s</label>
</div>',
$input_name, $esc_value, $id, $checked, $filtered_label
);
}
}
}